Let's Connect

Subscribe by Email

Your email:

Idyllic Software Blog

Current Articles | RSS Feed RSS Feed

FullText Search With Sunspot

  
  
  
Recently I had to implement the full text search functionality in my Ruby on Rails project. Had couple of options in my hand, as there are some good gems available in the Ruby on Rails community.

I have listed some of them below:

1. sunspot + solr            (https://github.com/outoftime/sunspot)
2. acts_as_indexed      (https://github.com/dougal/acts_as_indexed)
3. thinking-sphinx        (https://github.com/freelancing-god/thinking-sphinx)
4. tire + elasticsearch   (https://github.com/karmi/tire)

All of the options listed above are widely used and it isn't good to make any comparisons on their usage and performance. I wil show you how I integrated Sunspot in my rails project.

Sunspot is a Ruby library for expressive, powerful interaction with the Solr search engine. So when integrating it with the rails project the only thing we require is the sunspot_rails gem as it also comes with the Solr libraries and we don't have to explicitly install it. The key strengths of Sunspot would be the extensibility and to scale up to larger data better.

1. Installing sunspot_rails gem.

Include the sunspot_rails gem in your gem file.
gem 'sunspot_rails'
and run bundle install .

2. Generating the sunspot.yml file.
rails generate sunspot_rails:install
3. Start the Solr server.
rake sunspot:solr:start
to stop the server
rake sunspot:solr:stop
4. Add the searchable block to the model we want to search on.

Inside the block we have to define the attributes to search by.

So inside the model we would have something like this:
class Item < ActiveRecord::Base
belongs_to :category
searchable do
text :name
text :description, :stored => true
integer :category_id
float :price
end
end
5. Reindex the existing records in the database.
rake sunspot:reindex
This command would reindex the existing records in the database. We don't have to worry about the new records that have been added because Sunspot automatically indexed them for us.

6. Setting the search variable inside the controller method.
def index
search = Item.search do
fulltext params[:search_query]
paginate :page => params[:page], :per_page => 5
end
@items = search.results
end
Here inside the search block we can use different methods to perform the search. For example, the fulltext method with the params parameter.

Here search.results would return the array of all the items.

7. Inside the index.html.erb
:get do %>
8. Implementing the Faceting search

This is one of the most powerful features of Solr that helps to filter the search criteria according to certain conditions. For example, if we want to list all the items according to the categories they fall into.

For implementing the faceted search we need to make some changes in our index method and view, which will make our method and view to look something like this:

inside controller
def index
search = Item.search do
fulltext params[:search_query]
paginate :page => params[:page], :per_page => 5
facet(:category_id)
with(:category_id, params[:category]) if params[:category].present?
end
@items = search.results
@search=[]
search.facet(:category_id).rows.each do |facet|
@search << Category.fetch_category_name(facet.value)
end
end
inside view

Categories

    • search.id %>
(defining fetch_category_name method inside the category model)
class Category < ActiveRecord::Base
has_many :items
def self.fetch_category_name(value)
find_by_id(value)
end
end
9. Starting the Solr server in Production.

If you are in production, it's recommended that you don't use the rake task to start the Solr server. Instead you can fire this command which will start a daemon process.
sunspot-solr start -- -p 8983 -d data/solr/myapp

Now if you check your sunspot.yml file that you generated inside the /config directory, you will find this 8983 port defined and the other configuration settings.

There are lot more options that we can add to the  search functinality with Sunspot Solr and I would recommend to take a look at the Sunspot documentation.

Are you looking to develop a search driven application? Our Ruby on Rails development team can help. Give us a shout.

Comments

Currently, there are no comments. Be the first to post one!
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics