Reputation: 6585
In my Rails app I'm using Sunspot to index a few different models. I then have a global search form which returns mixed results. This is working fine:
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress) do
fulltext params[:q]
paginate :per_page => 10
end
I would like to add an additional model, say Project, to this search. The Project model has quite a bit that is indexed:
class Project < ActiveRecord::Base
searchable do
string :category do
category.name.downcase if category = self.category
end
string :client do
client.name.downcase if client = self.client
end
string :status
text :tracking_number
text :description
integer :category_id, :references => Category
integer :client_id, :references => Client
integer :tag_ids, :multiple => true, :references => Tag
time :created_at, :trie => true
time :updated_at, :trie => true
time :received_at, :trie => true
time :completed_at, :trie => true
end
end
How can I modify my original Sunspot.search
call to add searching for Project records by just the tracking_number
field and not the description
field?
Upvotes: 4
Views: 2972
Reputation: 11706
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress, Project) do
fulltext params[:q] do
fields(:tracking_number, :other_fields_outside_your_project_model)
end
paginate :per_page => 10
end
This will do full text search on tracking_number field and any other fields you specify, particularly in your Person, EmailAddress, PhoneNumber, and PhysicalAddress models.
Upvotes: 4
Reputation: 6660
Did you try something like :
Sunspot.search(Post) do
keywords 'great pizza', :fields => [:title, :body]
end
You can make one request for each model and then concat your results in only one list. I think you can't make it on one search.
Upvotes: 0
Reputation: 6660
I think you have to define your tracking_number as a text field and not a string field. Full text search only on "text fields".
Did you try this :
text:tracking_number
And your sunspot search looks like :
Sunspot.search(Person, EmailAddress, PhoneNumber, PhysicalAddress, Project) do
fulltext params[:q]
paginate :per_page => 10
end
See you
Upvotes: 0