How to Setup Elasticsearch in your Rails App in Production

Step 1 – Install elasticsearch in your Rails Application in Production

If you haven’t setup elasticsearch in your Rails application in development, this article tells you how to do so in Linux

Step 2 – Create the search indices on your Heroku production application

At the command prompt, type the following:

1
2
heroku run rails c production --app NAME_OF_MY_APP
3

If you’re running a staging environment, then you’d type *staging* instead of production. NAME_OF_MY_APP is the name of your Rails application running on Heroku.

Step 3 – Install the bonsai-elasticsearch-rails gem in your Gemfile and run bundle install

1
group :production do
2
  gem ‘rails_12factor’  #this is a gem Heroku also asks you to have as part of your Gemfile
3
  gem ‘bonsai-elasticsearch-rails’
4
end
5

Now run bundle install at the command prompt.

Step 4 – Add the bonsai Heroku add-on to your application

At the command prompt type:

1
2
heroku addons:add bonsai
3

If everything has gone smoothly, you should see the BONSAI_URL envrionment variable when you type the following at the command prompt:

1
2
heroku config | grep BONSAI
3

The BONSAI_URL environment variable:

1
2
BONSAI_URL     => http://ql9lsrn8:img5ndnsbtaahloy@redwood-94865.us-east-1.bonsai.io/
3

When I did this, I actually ran into an issue with the BONSAI_URL not being initialized. I ended up contacting customer support and according to their customer support team, I had run into an edge case and they had to do something on their end to fix it. But they had the solution turned around within 24 hours of my initial contact.

Now, your rails application should be up and running with elasticsearch.

How To Setup Elasticsearch In Your Rails Application In Development

Step 1 – Install elasticsearch

If you haven’t installed elasticsearch on your local development machine, this article tells you how to do so in Linux

Step 2 – Add the right ruby gems for the Ruby elasticsearch client in your Gemfile and bundle install

1
2
gem 'elasticsearch-rails'
3
 
4
gem 'elasticsearch-model'
5
 
6
gem 'elasticsearch-extensions'
7

Step 3 – Configure indexes in your Rails model(s)

01
02
class User < ActiveRecord::Base
03
  include Elasticsearch::Model
04
  include Elasticsearch::Model::Callbacks
05
 
06
  settings index: { number_of_shards: 2 } do
07
    mappings dynamic: 'false' do
08
      indexes :name, analyzer: 'english', index_options: 'offsets'
09
      indexes :email, analyzer: 'english', index_options: 'offsets'
10
      indexes :hobbies, analyzer: 'english', index_options: 'offsets'
11
    end
12
  end
13
 
14
  def self.search_users(search_words)
15
    User.import
16
    response = User.search(
17
      size: 20, 
18
      query: {
19
            match: {
20
                "_all" => {
21
                  "query" => search_words,
22
                  "operator" => "or"
23
                }
24
            }
25
        }
26
    )
27
    response.records
28
  end
29
end
30

Step 4 – Create / refresh the index

Start up the rails console in development mode and issue the following
commands:

1
2
User.__elasticsearch__.create_index! force: true
3
User.__elasticsearch__.refresh_index!
4

These commands will setup a search index with your desired configuration in the User model above. If you ever change your settings, you’ll need to reissue these commands. I hope to come back and update these instructions with a more automated process soon.

Choosing the query type

In the User model, I called the search method on the User model to return 20 results (see the size: 20 setting). The _all keyword tells elasticsearch to match all documents using the query search_words and “or” logic. For example if search_words is “Joe Smith”, elasticsearch searches for documents with the words Joe OR Smith. The query I use above should provide you what you need for simple searches.

How To Install Elasticsearch On Linux for Rails Development

What is elasticsearch? Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library. How to install it on Linux Mint 16 and Ubuntu 14.04 Step 1 - Download

Continue Reading …

How To Squash Your Last 2 Git Commits

Why Squash Commits? If you’ve been working with Git for a while you might start to wonder why you can’t combine that last trivial “hello world” type commit with your previously more meaningful commit. Fortunately, you don’t have to wonder as Git

Continue Reading …

Installing Phantomjs on Linux Mint 16

Just wrote a protip on Coderwall about how to install Phantomjs on Linux Mint 16...

Continue Reading …

Setting Up Spring + RSpec For Testing Your Rails 4 Application

What is spring? Spring is an application preloader. That means it boots up your rails application and keeps it running in the background. Why is this good? It means you don’t have to boot it up every time you need to run a test

Continue Reading …

Avoiding the ‘oCol is undefined’ Error with jQuery-datatables-rails

The ‘oCol is undefined’ Error Recently I installed jQuery-datatables-rails in a Rails 4 project. With the Firebug plugin in Firefox, I received an error ‘oCol is undefined’. With Google Chrome via the console, I received an error ‘Cannot

Continue Reading …

Integrating Fat Free CRM Into Your Rails Application

What is Fat FREE CRM? Fat Free CRM is an open-source customer relationship management piece of software that lets you perform the basic marketing activities integral to the sales cycle. Tracking leads, managing campaigns, and contact lists are

Continue Reading …

How to Pass Javascript Functions Without Them Firing

In Javascript, functions are first-class citizens. Implications? This means you can pass functions around in Javascript like they were variables. Here’s some example code to illustrate the concept. //setup prototype function

Continue Reading …

Why You As a Client Want a Rails Codebase With Tests

Would you ever stop changing the oil on your car? I wouldn’t. It’s part of the maintenance that leads to a longer life span for my car and ensures it runs smoothly. In fact, because the automobile industry has done such a good job of educating

Continue Reading …