Sphinx - 何时使用“has”和“索引”;对于字段

发布于 2024-08-18 06:21:29 字数 875 浏览 6 评论 0原文

几天前,我在 ruby​​ on Rails 2.3.2 上安装了 Sphinx 和 Thinking-Sphinx,基本搜索效果很好。这意味着,没有任何条件。现在,我想用一些条件过滤搜索。

我有公告模型,索引如下所示:

  define_index do
    indexes title, :as => :title, :sortable => true
    indexes description, :as => :description, :sortable => true
  end

也许我错了,但我注意到只有当我添加 :sortable =>; true 语法这些属性,我可以将它们用作搜索条件。否则什么也找不到。

现在,我还使用acts_as_taggable_on插件,它生成了我的两个数据库表:标签和标记。我没有标签模型,我的公告模型中只有 acts_as_taggable_on :tags, :categories

我现在想做的是按标签过滤。因此,我尝试添加到我的索引 has Tags(:id), :as => :tag_ids 没有运气,并且还索引标签(:id), :as => :tag_ids 但它也不起作用。

我怎样才能建立索引,这样我就可以做这样的事情:

Announcement.search 'some word', :conditions => {:tag_ids => some_id}

而且,这也是 hasindexes 之间的区别

谢谢, 布莱恩

I installed Sphinx and Thinking-Sphinx some days ago on my ruby on rails 2.3.2, and basic searching works great. This means, without any conditions. Now, I want to filter the search with some conditions.

I have the Announcement model, and the index looks as follows:

  define_index do
    indexes title, :as => :title, :sortable => true
    indexes description, :as => :description, :sortable => true
  end

Maybe I'm wrong, but I noticed that only when I added the :sortable => true syntax to these attributes, I could use them as conditions in my search. Otherwise it won't find anything.

Now, I'm also using acts_as_taggable_on plugin, which generated my two db tables: tags and taggings. I don't have a model for tags, I just have acts_as_taggable_on :tags, :categories at my Announcements model.

What I'd like to do now is to filter by tags. So, I tried adding to my index has tags(:id), :as => :tag_ids with no luck, and also indexes tags(:id), :as => :tag_ids but it didn't work either.

How can I build the indexes so I can do something like this:

Announcement.search 'some word', :conditions => {:tag_ids => some_id}

And also, which is the different between has and indexes

Thanks,
Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-08-25 06:21:29

让我反过来回答你的问题。 索引任何需要字符串的内容,这就是 sphinx 将搜索您提供的文本的内容。

另一方面,has another 不会将此内容添加到可搜索字段中。它主要需要数字,因为在 sphinx 已经完成搜索之后,您可以使用这些东西进行排序和过滤。

最后,我相信您想要 has Tags(:id), :as => :tag_ids 在你的模型中,并且 :with =>; {:tag_ids =>; some_id} 在您的搜索中而不是使用 :conditions。条件用于已索引的文本字段,作为对特定字段而不是所有索引字段执行文本搜索的一种方式。 With 用于使用您通过 haswhat 指定的属性来过滤结果。

Let me answer your questions in reverse. indexes whatever expects a string, this is what sphinx will search for the text you provide.

On the other hand, has whatever does NOT add this content to the searchable fields. It expects primarily numbers, because you use this stuff for sorting and filtering after sphinx has already done the search.

Finally, I believe you want has tags(:id), :as => :tag_ids in your model, and :with => {:tag_ids => some_id} in your search instead of using :conditions. Conditions are used on text fields that you have indexed, as a way to perform text searches on specific fields instead of all indexed fields. With is used to filter results using the attributes you've specified with has whatever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文