ElasticSearch is a fast growing technology built on Lucene. The main scope of ElasticSearch is to be a search engine. It also provides a lot of features that allow you to use it for data storage and data analysis. ElasticSearch has many innovative features like: JSON/REST-based api and natively distributed in a node/cluster.

ElasticSearch can be set up on a physical or virtual server depending on RAM, CPU and disk space.

Terms to Understand:


  • Each instance we install on physical or virtual server is known as a node.
  • Combination of node creates cluster.
  • On node startup, the node searches for other cluster members and checks its index and shard status.
  • Node configuration can be done through elasticsearch.yml
  • Replication of node can be done using master node and secondary nodes
  • If master node dies, a master-eligible node is elected to be the new master node.

Index (plural Indices)

  • Index is main data container in node.


  • In an index, data is grouped into data types called mappings.


  • A mapping describes how the records are composed called field.


  • Every record must be stored as a json object.


  • To manage huge volume of records, it splits an index into multiple shards.
  • Every record is stored in a shard. It’s algorithm is based on record ID.
  • So to update any record, it hits only shard that contains your object.

The following scheme compares ElasticSearch structure with SQL and MongoDB:

ElasticSearch SQL MonogoDB
Index (Indices) Database Database
Shard Shard Shard
Mapping/Type Table Collection
Field Field Field
Object (JSON Object) Record (Tuples) Record (BSON Object)



You need to download required ElasticSearch package from the following url: https://www.elastic.co/downloads/elasticsearch for your operating system. Once installation process completes, ElasticSearch will be ready to use. In this article I have used Ubuntu server.

Elasticsearch server Installation:

Start Server

Start Server

Default Page

Install plugin:

ElasticSearch provides facility to install plugin, to make our work seamless. We will use Marvel for our testing rest apis. Using the following command we can install marvel in ElasticSearch.

sudo /usr/share/elasticsearch/bin/plugin -i elasticsearch/marvel/latest

After successful installation, we can use marvel by using this url: https://localhost:9200/_plugin/marvel

The default page looks like this.

Marvel plugin default home page

Marvel plugin default home page

We will use sense interface of marvel to test our apis. Use of sense is recommended because of its auto suggest feature. Sense is reachable using the following url. https://localhost:9200/_plugin/marvel/sense/index.html

Marvel sense default home page


Marvel sense default home page 

use of sense

Example 1: use of sense
Now, we’ll go through basic example of book library.
Create New Index:
Creating new index is very simple. You just need to use PUT method with index name. If request performs perfectly then it’ll reply as acknowledged true as shown below in the image below.
Create New Index

Create with mappings:

Mapping helps search engine to map data. This also includes which fields are searchable and which are tokenized.

Create with mappings

Create new document:

As shown in below image, create new document using post method. In url we must specify index and type here, library/books. On successful execution it returns created =true with its id. On creation it will set version as 1.

Create new document

Get document:

It is simple GET request with index, type and document id. It returns data of specified id.

Get document

Update document:

On update of any document it’ll increase version counter. ElasticSearch api returns ‘created = true’ only when new data is inserted. When we update data that returns “created = false” but updates version number.

Update document

Delete document:

To delete any document just use delete request with index, type and document_id.

Delete document

Search document:

Search document

Search by all fields

Search by all fields

Search by specific field

Delete Index :

Delete Index

How to integrate with PHP?

By using curl, you can use ElasticSearch with your favorite programming language. Here is the example of simple curl request with ElasticSearch.

How to integrate with PHP?

ElasticSearch Clients:

ElasticSearch provides clients for Java, Javascript, Groovy, .NET, PHP, Perl, Python, Ruby.

PHP Sample Script:

You can find PHP client api on github: https://github.com/elastic/elasticsearch-php


require './vendor/autoload.php';
$client = new ElasticsearchClient();
$info = $client->info();

          [status] => 200
          [name] => Steve Rogers
          [cluster_name] => elasticsearch
          [version] => Array
                    [number] => 1.6.0
                    [build_hash] => cdd3ac4dde4f69524ec0a14de3828cb95bbb86d0
                    [build_timestamp] => 2015-06-09T13:36:34Z
                    [build_snapshot] =>
                    [lucene_version] => 4.10.4
          [tagline] => You Know, for Search

Use case:

Autosuggest functionality, ElasticSearch v/s MySQL.

By using this we want to check performance of both systems. Both are having 30 Lacs records. To improve ElasticSearch performance we have used filter, analyzer and to improve MySQL performance we have used full text search with optimized query. And at the end, output was quite amazing. Speed of ElasticSearch was half of speed of MySQL.

Use case

Avatar for Multidots
Author : Multidots
Multidots is an enterprise WordPress digital agency and one of the WordPress VIP partner agency. The companies in performance marketing, content marketing, and content publishing in North America, Asia and Europe trust Multidots’ globally distributed team for their complex and large scale WordPress implementation and multi-platform integration.


1 Comment

Leave a Reply

How can we help you?

Submit your Business Enquiry here and we will get in touch shortly.

Contact Us