这个问题之前已经被问过几次并得到了回答,但似乎没有一个建议对我有效。
我有一个用户模型和一个Micropost模型
一个用户有多个微博,一个微博有一个用户。
我正在尝试使用Sunspot同时搜索用户模型和微博模型。
我所需要的就是索引模型的正确语法。
我试过这个:
class User < ActiveRecord::Base
searchable do
text (:full_name)
text (:last_name)
text (:first_name)
text (:email)
text (:job_title)
text (:city)
text (:country)
text (:address)
text (:tag_list)
text (:content ) { micropost.content }
end
end基于
sunspot solr how to search multiple models correctly? All examples online fail
但这不管用。我所需要的就是搜索上面微博的内容属性。因此,如果一个人搜索一个用户,他们得到一个用户,如果他们搜索一个出现在micropost.content中的特定短语,他们就会得到包含该短语的微博。
据我所知,文档对此没有帮助。
发布于 2015-04-27 01:30:50
您的用户模型应如下所示:
class User < ActiveRecord::Base
searchable do
text (:full_name)
text (:last_name)
text (:first_name)
text (:email)
text (:job_title)
text (:city)
text (:country)
text (:address)
text (:tag_list)
end
end您的MICROPOST模型应如下所示:
class Micropost < ActiveRecord::Base
searchable do
text (:content)
end
end然后,在search_controller.rb文件中:
@search = Sunspot.search(User, Micropost) do |query|
query.fulltext params[:quick_search]
end
@results = @search.results然后为每个结果创建一个循环:
@results.each do |result|
if result.is_a?(User)
//do something with the result
end
if result.is_a?(Micropost)
//do something with the result
end
endhttps://stackoverflow.com/questions/29880837
复制相似问题