Introduction to ElasticSearch - Ruby on Rails

Introduction to ElasticSearch:
Elasticsearch is an open-source full-text search, real-time distributed engine. Developed on Java programming language, which enables Elasticsearch to run on different platforms.
Accessible from RESTful web service interface for querying and indexing massive amounts of structured data.
Elasticsearch can also be used as a data store engine.
General Feature:
Elasticsearch is scalable up to petabytes of structured and unstructured data. It can be used as a replacement of document stores like MongoDB where all the data store in a document.
The good thing about ElasticSearch is that, it an open source platform and available under the Apache license version 2.0.
Elasticsearch uses denormalization to improve the search performance.
Elasticsearch Advantages:
Elasticsearch is real time, in other words, after one second the added document is searchable in this engine. It is distributed from the cluster, which makes it easy to scale and integrate into any big organization.
Elasticsearch provides JSON objects as responses, which makes it possible to invoke the Elasticsearch server with a large number of different programming languages.
It also provides the feature of backup the data. Creates full backups easily by using the concept of the gateway.
Installation of ElasticSearch in Linux OS:
To begin, run the following command to import the Elasticsearch public GPG key into APT:
1]wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
2]echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
3] sudo apt update
4] sudo apt install elasticsearch
Open the file for configuration
5] sudo nano /etc/elasticsearch/elasticsearch.yml
Add network.host: localhost
6] sudo systemctl start elasticsearch
7] To enable Elasticsearch to start up every time your server boots:
sudo systemctl enable elasticsearch
8] Test whether your Elasticsearch service is running by sending an HTTP request:
Run command on terminal
curl -X GET "localhost:9200”
Output will get in response:
{ "name" : "ZlJ0k2h", "cluster_name" : "elasticsearch", "cluster_uuid" : "beJf9oPSTbecP7_i8pRVCw", "version" : { "number" : "6.4.2", "build_flavor" : "default", "build_type" : "deb", "build_hash" : "04711c2", "build_date" : "2018-09-26T13:34:09.098244Z", "build_snapshot" : false, "lucene_version" : "7.4.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
Domain where we can use the ElasticSearch:
We can use the ElasticSearch where we have a huge amount of datasets and search data easily. Blog search document, E-commerce product sites, Geosearch data feature are the some domain where we can use the ElasticSearch.
Indexing in Elasticsearch:
Let’s say I have created an article in database
article#1 -> “global article"
article#2 -> “rails article"
article#3 -> “ruby on rails"
An inverted index for these records will be like this:
global -> article#1
article -> article#1, article#2
ruby-> article#3
In the inverted index,article#1 article#2 are the indices for ElasticSearch data. Now, searching for the word “article” uses the inverted index and return article#1 and article#2 directly.
Integration of Elasticsearch in Rails:
For integrating ElasticSearch, I created one rails application where simply implemented Article creation with fields Article title and content of the article. Also added one search form where I can search the text related to Articles.
To start using ElasticSearch add gems in Gemfile:
gem 'elasticsearch-model'
gem 'elasticsearch-rails’
Then we have to make the elastic search in the model we want to implement global search like below:
require 'elasticsearch/model'
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['title^10', 'text']
}
}
}
)
end
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, analyzer: 'english'
indexes :text, analyzer: 'english'
end
end
end
Article.__elasticsearch__.client.indices.delete index: Article.index_name rescue nil
# Create the new index with the new mapping
Article.__elasticsearch__.client.indices.create \
index: Article.index_name,
body: { settings: Article.settings.to_hash, mappings: Article.mappings.to_hash }
# Index all article records from the DB to Elasticsearch
Article.import
To create the indices in a model where a search will be execute ElasticSearch provide the method:
Article.__elasticsearch__.client.indices.create
To delete the indices use
Article.__elasticsearch__.client.indices.delete
There are many features provided by the ElasticSearch like keyword Analyser to search any text data using the keyword.
Comments
Leave a comment: