Acts_as_taggable_on 标签名称作为 Thinking Sphinx 属性
我的模型使用 Thinking Sphinx 建立了索引,我想通过模型的标签(由acts_as_taggable_on 提供)过滤搜索结果。我阅读了上一个问题,这使我的搜索能够使用<代码>:条件=> { :标签=> 'Comedy' } 作为过滤查询。
这不是一个包罗万象的解决方案,因为默认情况下所有文本都会在 Sphinx 的索引字段中搜索。例如,当我搜索 Model.search :conditions => { :标签=> “喜剧”},带有黑色喜剧
标签的结果也会出现。我发现使用属性而不是字段是一种解决方案,但在搜索 Model.search :with => 时我似乎无法获得有效结果{ :标签=> “喜剧”}
和我的 define_index
块如下所示:
define_index
indexes title, :sortable => true
has category_tags(:name), :as => :tags
end
请注意,我是在上面链接的问题中提供的先前答案的基础上构建的。回答者详细介绍了上下文中的索引标签——这就是使用category_tags方法的原因。
My model is indexed with Thinking Sphinx and I am wanting to filter search results by the model's tags—provided by acts_as_taggable_on. I read this previous question, which enabled my searches to use :conditions => { :tags => 'Comedy' }
as a filtering query.
This is not a catch-all solution, since by default all text is searched in Sphinx's indexed fields. For example, when I search Model.search :conditions => { :tags => "Comedy" }
, results with the tag Black Comedy
also appear. I see that using attributes instead of fields is a solution, but I cannot seem to get valid results when searching Model.search :with => { :tags => "Comedy" }
and my define_index
block looks like this:
define_index
indexes title, :sortable => true
has category_tags(:name), :as => :tags
end
Note that I am building upon the previous answer provided in the question linked above. The answerer details indexing tags within context—thus the reason for the category_tags method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将索引更改为:
注意:category_tags.name 引用 your_table_name.column_name
并在转换为 int 后搜索标签:
有关更多信息,请参阅常见问题: http://freelancing-god.github.com/ts/en/common_issues.html
Change your index to:
Note: category_tags.name refers to your_table_name.column_name
And search the tag after converting to int:
See common questions for more: http://freelancing-god.github.com/ts/en/common_issues.html