Steps to add ‘Elasticsearch’ to Rails App

Steps to add ‘Elasticsearch’ to Rails App

2 Minutes Read

ElasticSearch-to-Rail-AppsWhat is Elasticsearch?

Searching on websites is important from a content discovery and user usability perspective. It allows readers to control the way they look for content instead of navigating through menus. Elasticsearch is a distributed open source search server based on Apache Lucene, much like Solr. It allows for real-time searching and the ability to scale easily through replicas. It has a REST API and communicates over JSON. We will use a gem called Tire to communicate with it.

What is the Problem if you don’t use a search engine?

If your application requires a full text search, then it is better to add a search engine like elasticsearch to the application. As normal queries to the database for the full text search might slow down your performance. You will end up with complex queries.

Tire is an elasticsearch ruby client. It can be used in any Ruby project, but it also has some nice model functionality to make it easy to integrate into a Rails application. It is actively maintained, has a large amount of users, and also integrates nicely with Rails and ActiveModel.

Install elasticsearch in your server

$curl -L -O -k
$mv elasticsearch-0.90.0.Beta1 elasticsearch

Install the Elasticsearch Service Wrapper

$curl -L -k | tar -xz
$mv *servicewrapper*/service elasticsearch/bin/
$elasticsearch/bin/service/elasticsearch install
$ln -s `readlink -f elasticsearch/bin/service/elasticsearch` /usr/local/bin/rcelasticsearch (optional use )
$rcelasticsearch start or /etc/init.d/elasticsearch start

By default the server on port 9200 ex: http://localhost:9200
We can communicate to the server manually if we want through the JSON REST API.

Add the following gem in your gemfile

gem 'tire'

Run the bundle install

To make the Article searchable with Tire, just include below lines of code in your model

class Article < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
To create index for elasticsearch server run the below command
1$ rake environment tire:import:all

You can also force-import the data by deleting the index first

$ rake environment tire:import CLASS=’Article’ FORCE=true

Add the following in your model

def true) do
query { string params[:query]} if params[:query].present?

Add the following in your controller

def index
if params[:query].present?
@articles = => params[:page], :per_page => 12)
@articles = Article.all
Step#7 Add the search form>
<%= form_tag articles_path, method: :get do %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
<% end %>

To know the which model are indexed you can test by running the followings in your browser


To test the elasticsearch run the followings in your browser which will return the data in JSON format


Andolasoft is one stop shop for Ruby on Rails app development. We Design, develop & deploy Rails application in cloud for start-ups & established businesses. If you look for any type of assistance, please contact us.

Like this blog? I’d love to hear about your thoughts on this. Thanks for sharing your comments.

Jayadev Das

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

1 Comment
  • Avatar
    Misha Tatinets
    Posted at 00:52h, 02 June Reply

    tire gem is outdated way to implement of elasticsearch. it is retired now according to developer.

Post A Comment

Exit pop up

Before You Go...!

Subscribe to Andolasoft Blog

Stay up to date with the latest techniques of web & mobile app development service tips and our SaaS and open source product news.
Holler Box