Reputation: 2170
This question has been asked several times before and answered but none of the suggestions seem to work in my case.
I have a User model and Micropost model
A user has many microposts, a micropost has one user.
I am trying to search across the user model and micropost model at the same time using Sunspot.
All I need is the correct syntax for indexing the models.
I tried this:
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
Based on
sunspot solr how to search multiple models correctly? All examples online fail
but this doesn't work. All I need is to search the content attribute of the micropost above. So if a person searches for a user, they get a user, if they search for a specific phrase that occurs in a micropost.content, they get the micropost(s) with that phrase.
The documentation doesn't help on this as far as I can see.
Upvotes: 1
Views: 132
Reputation: 5905
Your USER model should be like:
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
Your MICROPOST model should be like:
class Micropost < ActiveRecord::Base
searchable do
text (:content)
end
end
Then, on your search_controller.rb file:
@search = Sunspot.search(User, Micropost) do |query|
query.fulltext params[:quick_search]
end
@results = @search.results
then for each result create a loop:
@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
end
Upvotes: 1