sunspot_rails the Sunspot library for Solr search.
Now its very easy to integrate solar search using sunspot_rails gem in rails 3 for fast searching.
Please follow the below steps to use solar search.
1) first install the gemgem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development
2) Run the bundle command to install the gems.
3) Run
rails generate sunspot_rails:install
to create the configuration file for the solar search.
4) Now you need to start the solar server
bundle exec rake sunspot:solr:start
5) If you have some exiting data in the data base please execute the command
rake sunspot:reindex
6) Now we are done with our setup.we need to configure our model so that search will work.
let say we have one event model in our application like
class Event < ActiveRecord::Base
attr_accessible :content, :title
end
Now if we want a full text search on the Event model. We just need to add one block in that model
class Event < ActiveRecord::Base
attr_accessible :content, :title
searchable do
text :content, :title
end
end
In the controller where we want to fetch the events based on search parameter we need to write
@search = Event.search do
fulltext params[:search]
end
@events = @search.results
There are lots of searching option available in the Solr search. please check the WIKI documentation for further details.
https://github.com/sunspot/sunspot/wiki
Comments
Post a Comment