Steps to add ‘Elasticsearch’ to Rails App

ElasticSearch to Rail Apps1
Social sharing

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.

Step#1
Install elasticsearch in your server

$curl -L -O -k https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.0.Beta1.zip
$unzip elasticsearch-0.90.0.Beta1.zip
$mv elasticsearch-0.90.0.Beta1 elasticsearch

Install the Elasticsearch Service Wrapper

$curl -L -k http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | 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.

Step#2
Add the following gem in your gemfile

gem 'tire'

Run the bundle install

Step#3
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
end1
 
<strong>Setp#4</strong>
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

Step#5
Add the following in your model

def self.search(params)
tire.search(load: true) do
query { string params[:query]} if params[:query].present?
end
end

Step#6
Add the following in your controller

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

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

http://localhost:9200/_aliases?prett

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

http://192.168.2.100:9200/articles/_search?q=news

Conclusion:

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.

Your recently viewed posts:

Jayadev Das - Post Author

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 ‘About 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!

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box