How to Setup Elasticsearch in your Rails App in Production

August 23, 2014

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:

heroku run rails c production --app NAME_OF_MY_APP

If you’re running a staging environment, then you’d type staging instead of production. NAMEOFMY_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

group :production do
  gem 'rails_12factor'  #this is a gem Heroku also asks you to have as part of your Gemfile
  gem 'bonsai-elasticsearch-rails'

Now run bundle install at the command prompt.

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

At the command prompt type:

heroku addons:add bonsai

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

heroku config | grep BONSAI
The BONSAI_URL environment variable:

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.