Sunspot / Solr full text search - how to index Rails associations

橙三吉。 提交于 2019-12-05 06:51:18

That's exactly how to do it. Solr is essentially document-oriented, so any data that comes from associations in your database is flattened down into your document. An :include option is just mild sugar which ends up doing the same thing that you do here.

Sure:

searchable do
  string :sort_contact_name do
    contacts.map { |contact| contact.last_name }.sort.first
  end
end

Then you can sort by the :sort_contact_name field. Note that I had to reduce the set of contact names to a single name, as Solr can only sort on fields that have a single value per document (which makes sense when you think about it).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!