1

有没有办法将 ActiveRecord 语法和方法与resultsSunspot 提供的对象集成?

例如Post.where(...).search{fulltext 'pizza'}.results不按 mywhere(...)子句过滤结果,Post.search{fulltext 'pizza'}.results.where(...)返回NoMethodError: undefined method 'where' for #<Sunspot::Search::PaginatedCollection:0x007fe9b388bd88>

如果不为所有内容编制索引,有没有办法将我的 Sunspot 搜索与 ActiveRecord 查询相协调?我的一些 AR 过滤器过于复杂,无法简单地用 Sunspot 的首选语法重写。

4

1 回答 1

0

据我所知,您必须为条件语句索引字段。

Post.search do
 with(:blog_id, 1)
 fulltext("pizza")
end

一个混乱的解决方案是在查看结果时添加条件语句。

search.each_hit_with_result do |hit, result|
  if result.post_id == 2
    #stuff
  end
end

您还可以创建条件索引。 http://mikepackdev.com/blog_posts/19-conditional-indexing-with-sunspot

于 2013-08-23T16:36:57.520 回答