Saturday 4 February 2017

Elasticsearch with Rails 5


In this article, we will introduce you to Elasticsearch and show you how to install, configure and start using it with ruby on rails.

Elasticsearch is a platform for distributed search and analysis of data in real time. Its popularity is due to its ease of use, powerful features, and scalability.

Elasticsearch supports RESTful operations. This means that you can use HTTP methods (GET, POST, PUT, DELETE, etc.).

If it is your first time with Elasticsearch and you don't have Elasticsearch install in your system for this you can follow digital ocean documentation or you can simply install by

Update your ubuntu package

    $ sudo apt-get update

Download the latest Elasticsearch version, which is 2.3.1 at the time of writing.

    $ wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.3.1/elasticsearch-2.3.1.deb

Then install it with dpkg.

    $ sudo dpkg -i elasticsearch-2.3.1.deb

To make sure Elasticsearch starts and stops automatically with the server, add its init script to the default runlevels

    $ sudo systemctl enable elasticsearch.service

Now that Elasticsearch and its Java dependencies have been installed, it is time to configure Elasticsearch. The Elasticsearch configuration files are in the /etc/elasticsearch directory.

To start editing the main elasticsearch.yml configuration file with nano or your favorite text editor.

    $ sudo nano /etc/elasticsearch/elasticsearch.yml

Remove the # character at the beginning of the lines for cluster.name and node.name to uncomment them, and then update their values

    . . .
    cluster.name: mycluster1
    node.name: "My First Node"
    . . .

These the minimum settings you can start with using Elasticsearch. However, it's recommended to continue reading the configuration part for more thorough understanding and fine-tuning of Elasticsearch.


Now, we will integrate elasticsearch with rails.

To get started, grab the latest Ruby on Rails and add following below gems to your Gemfile.

    gem 'elasticsearch-rails'
    gem 'elasticsearch-model'

Open the model file at app/models/user.rb, and add the Elasticsearch includes. This will allow us to use the Elasticsearch methods on our model, and will also enable the Elasticsearch callbacks for events such as creating, updating, and destroying our model objects, which will keep the Elasticsearch index up to date.

    class User < ActiveRecord::Base
        include Elasticsearch::Model
        include Elasticsearch::Model::Callbacks
    end

Now, we have to call the import method on the User model. This is needed because our search index is empty and we need to populate it with our current data. Any subsequent changes to the User table will automatically be added to the index by the Elasticsearch callbacks, so you wont need to run the import method again, unless changes are made outside of the Rails app.

So, Open up the rails console and run

    User.import

Now, you can start searching the User model. By default this will search all the columns in this model.

    User.search("abc").records

1 comment:

  1. It was really a nice article and i was really impressed by reading this Ruby on Rails Online Course India

    ReplyDelete