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.