CRUD Operations on Elastic Search Using Rest client

Connect with

elastic search
This post is demonstrating about how to do the CRUD (create index, get content from index, Update index, and delete indexed data) operations using Rest client. You can do this via fiddler or Google’s Advance Rest Client plugin embedded in side the Crome browser.

If you wanted to do via Fiddler (if fiddler installed in you system), steps are as:

  • open fiddler
  • Click on Composer which is on top right of fiddler
  • Provide the URL and select appropriate HTTP Method
  • provide JSON in Request Body
  • Click on Execute button which is on top right corner of fiddler.

CRUD Operations as:

  1. how to add content in elastic index?
  2. how to search data from elastic index?
  3. how to update data in elastic index and
  4. how to delete document from Elastic data.
  5. how to delete type from Elastic data.
  6. how to delete index from Elastic data.

Prerequisite : Sample JSON

Use following json data as sample , here json with nested documents.

{ 
   "name":"ranjeet",
    "dob":"2000-12-15", 
    "body": "good looking" ,
    "email": "ranjeet.kr@gmail.com",
	"address": {
	  "city":"dwarka",
	  "state":"New Delhi",
	  "pin":"110059"
	}
}

{ 
   "name":"rob",
    "dob":"1980-01-15", 
    "body": "good looking, slim fit" ,
    "email": "rob@gmail.com",
	"address": {
	  "city":"rohini",
	  "state":"New Delhi",
	  "pin":"110059"
	}
}

Create Index in Elastic using restClient

Service Input

  1. Service URL : http://localhost:9200/myindex/mytype
  2. HTTP Method: POST
  3. payload JSON
{ 
   "name":"ranjeet",
    "dob":"2000-12-15", 
    "body": "good looking" ,
    "email": "ranjeet.kr@gmail.com",
	"address": {
	  "city":"dwarka",
	  "state":"New Delhi",
	  "pin":"110059"
	}
}

Service Output:

  1. HTTP response code: 201
  2. JSON output
{
  "_index": "myindex",
  "_type": "mytype",
  "_id": "AVc7tOTnQTyMXgOVj2M7",
  "_version": 1,
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

Here, Http code 201, it means content json added in elastic index.

Get Indexed Data in Elastic using Rest Client

I have created only one document and try to get all by following query. By default elastic returns only 10 document but here I have added only 1 document so it return in pretty print format.

Service Input:

  1. HTTP Method: GET
  2. Service URL: http://localhost:9200/myindex/mytype/_search?pretty=true&q=*:*

Service Output:

  1. HTTP status : 200 Ok
  2. JSON Output:
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "myindex",
        "_type": "mytype",
        "_id": "AVc7tOTnQTyMXgOVj2M7",
        "_score": 1,
        "_source": {
          "name": "ranjeet",
          "dob": "2000-12-15",
          "body": "good looking",
          "email": "ranjeet.kr@gmail.com",
          "address": {
            "city": "dwarka",
            "state": "New Delhi",
            "pin": "110059"
          }
        }
      }
    ]
  }
}

if document successfully added by previous step, you should get similar like above else something else json output.

If document not added successfully , you should get similar like follows.

{
  "error": {
    "root_cause": [
      {
        "type": "index_not_found_exception",
        "reason": "no such index",
        "index": "myindex",
        "resource.type": "index_or_alias",
        "resource.id": "myindex"
      }
    ],
    "type": "index_not_found_exception",
    "reason": "no such index",
    "index": "myindex",
    "resource.type": "index_or_alias",
    "resource.id": "myindex"
  },
  "status": 404
}

Delete a document By Doc Id using rest Client

Delete a single document by posting HTTP DELETE request to : http://[your_host]:9200/[your_index_name_here]/[your_type_here]/[your_doc_id]
e.g

Service Input:

  1. Service URL : http://localhost:9200/myindex/mytype/AVc7yauYQTyMXgOVj2NC
  2. HTTP Method: DELETE

Note: here, in the service URL last param in the path paramete is document id i.e. _id value from output

Service Output

  1. HTTP status : 200 OK
  2. Output json:
{
  "found": true,
  "_index": "myindex",
  "_type": "mytype",
  "_id": "AVc7yauYQTyMXgOVj2NC",
  "_version": 2,
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

Delete a type in Elastic using rest Client

delete type by posting delete to http://[your_host]:9200/[your_index_name_here]/[your_type_here]

for Example:
Service Input:

  1. Service URL : http://localhost:9200/myindex/mytype
  2. HTTP Method: DELETE

Note: here, “mytype” is a type in elastic which comes under index name ( i.e. myindex) here.

Service Output

  1. HTTP status : 200 OK

200 Ok

Delete Index in Elastic

Delete Index name by posting HTTP DELETE request to URL: http://[your_host]:9200/[your_index_name_here]
when you delete index , it means you are going to delete all of type and document under that index name.

for Example:
Service Input:

  1. Service URL : http://localhost:9200/myindex
  2. HTTP Method: DELETE

Note: here, “myindex” is the name of elastic index.

Service Output

  1. HTTP status : 200 OK
  2. Output json:
{
"acknowledged": true
}

To Get last 10 records over here, service URL :
http://localhost:9200/myindex/mytype/_search?pretty=true&q=*:*

Check “stats” of Elastic search by URL: http://localhost:9200/_stats/

Write your comments here for any type of suggestion.


Connect with

1 thought on “CRUD Operations on Elastic Search Using Rest client

  1. Pingback: Mike

Leave a Reply

Your email address will not be published. Required fields are marked *