October 10, 2013

Adding search and autocomplete to a Rails app with Elasticsearch

This post will show a fast and easy way to add search to your Rails application. We will use Elasticsearch, an open source search engine, and Searchkick, an easy-to-use gem that integrates Elasticsearch with Rails.

Installing Elasticsearch

To get started, you need to install Elasticsearch. Mac users can use Homebrew for that - simply run brew install elasticsearch and follow the instructions. Alternatively, you can refer to the Elasticsearch setup guide.

Example Application

The code for an example application we'll be working with can be found on Github. If you want to follow the changes step-by-step on your machine, you can start with the before-search branch.

After importing some books, we end up with a basic listing of books with their covers, paginated:

Bookstore before search

Adding Search

With that as a base application, we can start working on adding search. The first step is adding searchkick to the Gemfile:

# Gemfile
gem "searchkick"

After running bundle install, we can configure the Book model to be searchable:

# app/models/book.rb
class Book < ActiveRecord::Base
  # ...

Next, we need to build the index for the Book class so that the books we already have in the database will be added to Elasticsearch. When new records are created rebuilding isn't necessary, but you have to do it whenever you change the searchkick method in your model:

rake searchkick:reindex CLASS=Book

After that, we can try the search in the rails console:

2.0.0-p247 :001 > results = Book.search("ruby")
 => #&lt;Searchkick::Results:0x007ff5a1bca158 ...
2.0.0-p247 :002 > results.map(&:title)
  Book Load (0.5ms)  SELECT "books".* FROM "books" WHERE "books"."id" IN (10, 9, 11)
 => ["Programming Ruby", "The Ruby Way", "Ruby On Rails Bible"]

Now that our search is working, we can integrate it with the books controller:

# app/controllers/books_controller.rb
class BooksController < ApplicationController
  def index
    if params[:query].present?
      @books = Book.search(params[:query], page: params[:page])
      @books = Book.all.page params[:page]

  # ...

And add the search form in the view:

# app/views/books/index.html.slim
= form_tag books_path, class: "form-inline", method: :get do
    = text_field_tag :query, params[:query], class: "form-control"
  = submit_tag "Search", class: "btn btn-primary"
  - if params[:query].present?
    = link_to "clear", books_path

With that change, we can search our bookstore using the form we just added:

Bookstore simple search

You can check the code up to this point at the simple-search branch on Github.


One of the most useful features of any search engine is autocomplete. Luckily for us, searchkick provides a really easy way to do that. On the browser side, we can use the awesome typeahead.js library from Twitter.

We'll add this library in the application layout (because it's faster), but for production apps you should add it to your Asset Pipeline. You could use Bower for that, and we posted a guide for using Bower with Rails a while ago.

# app/views/layouts/application.html.slim
= javascript_include_tag "//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"

Next, we change the searchkick setup in our Book model to include autocomplete:

# app/models/book.rb
class Book < ActiveRecord::Base
  searchkick autocomplete: ['title']
  # ...

As mentioned before, this change requires rebuilding the index, so we run

rake searchkick:reindex CLASS=Book

To confirm that the autocomplete is working, let's try it in the Rails console:

2.0.0-p247 :001 > Book.search("the", autocomplete: true).map(&:title)
  Search (7.5ms)  {"query":{"multi_match":{"fields":["title.autocomplete"],"query":"the","analyzer":"searchkick_autocomplete_search"}},"size":100000,"from":0,"fields":[]}
  Book Load (0.8ms)  SELECT "books".* FROM "books" WHERE "books"."id" IN (9, 12, 5)
 => ["The Ruby Way", "The Art of Rails", "The  art of computer programming"]

Since it's working as expected, we can add an action to our books controller that will return the autocomplete results:

# app/controllers/books_controller.rb
class BooksController < ApplicationController
  def autocomplete
    render json: Book.search(params[:query], autocomplete: true, limit: 10).map(&:title)

Remember to add a route for this new action:

# config/routes.rb
Bookstore::Application.routes.draw do
  resources :books, only: :index do
    collection do
      post :import
      get :autocomplete # <= add this line
  root to: 'books#index'

Now, we can add autocomplete to the search form. First, let's add some nicer styling:

# app/views/books/index.html.slim
= form_tag books_path, class: "form-inline", method: :get do
    - if params[:query].present?
        = link_to "clear", books_path, class: "btn btn-default"
    = text_field_tag :query, params[:query], class: "form-control", id: "book_search", autocomplete: "off"
      = submit_tag "Search", class: "btn btn-primary"

Note that we added the book_search id to the query input. All that's left is wiring up the javascript to fetch the autocomplete results:

# app/assets/javascripts/books.js.coffee
$ ->
    name: "book"
    remote: "/books/autocomplete?query=%QUERY"

We're missing some styling for typeahead, which you can download from here and put in app/assets/stylesheets/typeahead.css.

The result should look like this:

Bookstore autocomplete

Deploying Elasticsearch

If you're deploying your app on your own infrastructure, you can use this chef cookbook. There is an official Elasticsearch guide for deploying using chef-solo.

For cloud deployments, you can use bonsai, found or searchly.

If you're using Shelly Cloud, adding Elasticsearch is as easy as adding one line to your Cloudfile.

Further improvements

The example given in this post is very basic. Adding functionality is left as an excercise for the reader, but I suggest looking into facets and suggestions. Both of those features will improve the usability of your app and delight your users.

Read one of our other articles - Why GlusterFS should not be integrated with OpenStack

Sign up for our newsletter and get updates
about Shelly Cloud on your email:

Platform for hosting Ruby and Ruby on Rails apps, focusing on the developers happiness. With us you'll soon forget about server administration and having to wake up at 3 a.m. again.