ElasticSearch – How to Interact with ElasticSearch using JSON over HTTP ?

Problem Statement

You need to communicate with ElasticSearch to perform the search or the CRUD operations.

Solution

You can interact with ElasticSearch using the RESTful API over port 9200.

Additionally , ElasticSearch has the official clients for various programming languages like .NET , Java , Ruby etc.

The format of the request of ElasticSearch is as specified below.

<VERB> <PROTOCOL> ://<HOST>/<PATH>?<QUERYSTRING>

Additionally , it can contain the request body.

1. The VERB can be GET , POST , PUT , HEAD , DELETE.

2. Protocol can be http or https

3. HOST refers to the host name of the elasticsearch cluster . Eg : localhost

4. PORT – This refers to the port number where the ElasticSearch is running. The default port number where ElasticSearch runs in 9200.

5. QUERYSTRING is the optional query string parameters used for the request URL.

6. The body contains the JSON encoded request body (if needed).

When testing the ElasticSearch API , you might want a graphical client like Fiddler or Chrome Sense plug-in to try,

Chrome plug-in called “Sense” is a excellent tool to test ElasticSearch API. It provides a simple user interface for using the ElasticSearch REST API. Some of the features include autocomplete for queries , copy and paste the request in curl format etc. 

You can search for the Sense plugin from your Google Chrome browser and install it.

Once you have installed the Sense plugin , you will see its icon in the upper right corner in Google Chrome. Just tap on the icon and you are ready to start.

image

The Query can be entered in the left sidebar. The response will be shown in the content area.

This is the query in curl which displays the number of documents in the ElasticSearch cluster

curl -XGET 'http://localhost:9200/_count?pretty' -d ' 
{   
    "query": 
    {        
        "match_all": {}    
        
    }   
}

The response of the above query is

{
   "count": 10,
   "_shards": {
      "total": 15,
      "successful": 15,
      "failed": 0
   }
}