solr sunspot – 搜索belongs_to association

我有一个属于多个其他表的结构模型。

class Fabric  5 text :description text :composition do composition.name end text :collection do collection.name end text :style do style.name end text :origin do origin.name end text :texture do texture.name end text :supplier do supplier.name end end end 

我已经设置了所有反向关联(Has_many)等。但是我似乎无法通过全文搜索来查询所有这些关联表的名称字段。

任何帮助将不胜感激。

  @search = Fabric.search do fulltext params[:search] end @fabrics = @search.results 

罗斯

您需要在全文中传递块以指定要搜索的字段。

 @search = Fabric.search do fulltext params[:search] do fields(:collection, :style, :origin) end ..... end 

以下是您在可搜索块中的索引方式。 索尔在文件方面思考。 它并不关心它是否是一种联想。

 searchable do text :collection do collection.text end end 

然后重新索引。

请查看更多详细信息https://github.com/sunspot/sunspot#full-text

https://github.com/sunspot/sunspot#setting-up-objects

如果某些关联可能为零,请不要忘记测试,否则在重建索引时会出现错误

 text :collection do collection.name if collection end 

看起来您在模型更新后没有重新编制索引数据。

运行此命令以重新索引:

 bundle exec rake sunspot:solr:reindex