Bri*_*tul 5 ruby sphinx ruby-on-rails acts-as-taggable thinking-sphinx
几天前我在我的ruby on rails 2.3.2上安装了Sphinx和Thinking-Sphinx,并且基本搜索效果很好.这意味着,没有任何条件.现在,我想用一些条件过滤搜索.
我有Announcement模型,索引如下:
define_index do
indexes title, :as => :title, :sortable => true
indexes description, :as => :description, :sortable => true
end
Run Code Online (Sandbox Code Playgroud)
也许我错了,但我注意到只有当我将:sortable => true语法添加到这些属性时,我才能在搜索中使用它们作为条件.否则它什么都找不到.
现在,我还使用acts_as_taggable_on插件,它生成了我的两个数据库表:标签和标签.我没有标签模型,我只是acts_as_taggable_on :tags, :categories在我的公告模型.
我现在要做的是按标签过滤.所以,我尝试添加到我的索引 has tags(:id), :as => :tag_ids没有运气,indexes tags(:id), :as => :tag_ids但也没有用.
我如何构建索引,以便我可以这样做:
Announcement.search 'some word', :conditions => {:tag_ids => some_id}
Run Code Online (Sandbox Code Playgroud)
而且,这之间的不同has和indexes
谢谢,Brian
Jai*_*yer 10
让我反过来回答你的问题. indexes whatever需要一个字符串,这是sphinx将搜索您提供的文本.
另一方面,has whatever不将此内容添加到可搜索的字段中.它主要是数字,因为你在sphinx已经完成搜索后使用这些东西进行排序和过滤.
最后,我相信你想要has tags(:id), :as => :tag_ids在你的模型和 :with => {:tag_ids => some_id}搜索中而不是使用:conditions.条件用于已编制索引的文本字段,作为对特定字段而不是所有索引字段执行文本搜索的方法.With用于使用您指定的属性过滤结果has whatever.