Ruby on Rails: Search & autocomplete with Sunspot, Solr and Twitter Typeahead.js (Part #1)

Most websites provide search functionality. You can improve the usablity of your search if it has an autocomplete feature. In this post we will set up a search box with an autocomplete based on the results of the search. We will use the following:

This is the first part of a two part series on integrating these technologies into your rails application.

Search can be implemented in many ways. You can create the corresponding SQL queries to get the results or you can use a search engine designed for this task. Apache Solr is an opensource search engine written in Java. Its major features include powerful full-text search, hit highlighting, faceted search, near real-time indexing and so on. 

In order to easily use Solr in your Rails application you should use the Sunspot gem. It provides an easy to use DSL to describe which fields are searchable and the way you want to search them. Yo will need the following two gems in your Gemfile:

gem 'sunspot_rails'

group :development, :test do
    gem 'sunspot_solr', '2.1.0'

The sunspot_rails gem integrates Sunspont into rails by extending ActiveRecord objects for searchability. The sunspot_solr gem can be used during development to run an Apache Solr instance locally. You can start the Solr engine by:

> rake sunspot:solr:start

Sunspot will handle commits to Solr automatically when you save your models. So lets create our model. We will use a simple article and its author:

> rails g model Author name:text

After this we get the following Author model:

class Author < ActiveRecord::Base
  has_many :articles

To generate our Article model run:

> rails g model Article title:text body:text author:references

The result is:

class Article < ActiveRecord::Base
  belongs_to :author

We want to be able to search by the articles by their title, content and author. The autocomplete will show us the title of the article. We have to define how can the Solr index our Article model:

class Article < ActiveRecord::Base
  belongs_to :author

  searchable do
    text :title, :body
    text :author do

Inside the searchable block the text fields are available in a full text search. There are more field types that can be used to restrict the search for example to a specific category. You can find a detailed description here.

We index the autor’s name inside the Article, so we have to add an after save callback to the Author to reindex the article entites if their author’s name changed.

class Author < ActiveRecord::Base
  has_many :articles
  after_save :update_articles
  def update_articles
    # The author's name can be changed.
    # We are reindexing the articles that belong to
    # this author.
    articles.each { |article| article.index }    

At any time, when you are making changes in the searchable blocks in your models, you should reindex the content of your database in Solr by executing:

> rake sunspot:solr:reindex

Now we can search our Article entities by:

search = do
  fulltext 'ruby'

search.results.each do |article|
  p article.title

We executed a full text search using the ruby keyword. The corresponding Article entities are in the search.result variable. You can execute this code inside your rails console to try it out.

In the next part of the series we will create a controller and our search box with the help of typeahead.js.


Tagged with: , , , ,
Posted in Javascript, Ruby on Rails
4 comments on “Ruby on Rails: Search & autocomplete with Sunspot, Solr and Twitter Typeahead.js (Part #1)
  1. David says:

    Did you ever finish this?

  2. I am regular reader, how are you everybody? This piece of writing posted at this web page is actually

1 Pings/Trackbacks for "Ruby on Rails: Search & autocomplete with Sunspot, Solr and Twitter Typeahead.js (Part #1)"
  1. […] the first part we created our simple models in Rails and added search information with the help of Sunspot. In […]

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>