Full text search with Sunspot in RailsFull text search with Sunspot in Rails

Have you ever been came across a situation, when you have to examine all of the words in every stored document in Rails?

Wait! here comes the solution and, that is, none other than Sunspot. Sunspot is basically a Ruby Library that is built on top of the RSolr library – A simple, extensible Ruby client for Apache Solr.

Recently, I had great experience of using Sunspot for full text search in one of my rails application. In Ruby on Rails, it is really easy to integrate full text search functionality, using Sunspot::Rails. I just followed set of steps, and it’s done. So, I thought sharing it with you.

For seamless integration of Sunspot search in your Rails application, you first need to install Sunspot::Rails.

Installation in Rails 2

$ sudo gem install sunspot_rails

Next step is to run the generator to create config/sunspot.yml file. For that, run following command

$ script/generate sunspot

To get the Sunspot rake tasks, you’ll need to require the tasks from your app’s Rakefile:

Installation in Rails 3

Add following line to your Gemfile to specify sunspot_rails gem

gem ‘sunspot_rails’, ‘1.2.1’

Now, it’s time to update your bundle using the command below

$ bundle install

To run Sunspot generator, use

$ rails g sunspot_rails:install

The above steps will install Sunspot and RSolr as dependencies.

To Configure Solr that is a standalone HTTP Server use following command

$ rake sunspot:solr:start

The above command creates solr/ directory in Rails root consisting of Solr’ configurations and data files for Solr index. To change the configuration, you can check solr/conf file.

Full text search is basically a combination of two tasks:

  1. Indexing
  2. Searching/ Querying

Lets go through each of them one by one :

Defining an Index:

Suppose, I have a Book model with title, description, published and updated_at fields. Book model belongs to Author

Note here that, text fields are used for full-text search, while, other fields are just used for filtering / restricting / sorting. The :default_boost parameter means that, words matching the :title field should be considered twice as relevant as, words matching the :description field.

For reindexing Book model’s records, You can use like Book.reindex!. For full reindex, you have to use rake sunspot:reindex

Searching / Querying

class BooksController < ApplicationController
  def search_results
    search = Book.search do
      keywords 'Rails Programming'
      with(:author_id).any_of [1, 2, 4, 8]
      paginate :page => page_number, :per_page => 30
      order_by :updated_at, :asc
    end
    @results = search.results
  end
end
That’s it! It’s up and running with full text search functionality in rails.

Here are benefits of using Sunspot:

  • It supports text,string,time,boolean and float fields.
  • It easily integrates geolocation indexing.
  • It uses flexible DSL for querying.
  • It playes nice with WillPaginate Plugin.

Friends ! Do you want to examine all of the words in every stored document in Rails ?? Wait ! here comes the solution and that is none other than Sunspot. Sunspot is basically a Ruby Library that is built on top of the RSolr library – A simple, extensible Ruby client for Apache Solr.

Recently I had great experience of using Sunspot for full text search in one of my rails application. In Ruby on Rails , its so easy to integrate full text search functionality using Sunspot::Rails. I just followed set of steps and its done so I thought sharing that here.

Installation

For seamless integration of Sunspot search in your Rails application, you first need to install Sunspot::Rails.

If you are using Rails 2 then use following commands.

$ sudo gem install sunspot_rails