Rails: Sunspot text searching with model associations, using :through

这一生的挚爱 提交于 2019-12-06 12:06:11

You will need to make additional indexes for the associated models in the calling model to make this happen. For example:

class Business < ActiveRecord::Base
 attr_accessible :name
 has_many :services, :through => :professionals

 searchable  do
  text :name #name in business column
  text :services do  # this one for full text search
     services.map(&:service).compact.join(" ")
  end
  string :services , :multiple => true do #this one for exact searches
     services.map(&:service).compact
  end
 end
end 

After that you can do queries like:

Bussines.search do 
  with(:services, "some_service")
end.execute.results

Now you no longer have to do join on mysql tables to fetch data. You can just fetch data from the solr. This is one of biggest advantages of solr.

I hope this makes it clear. Fell free to drop a comment if you need more details.

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