CRUD Operations on Elastic Search Using Rest client

Connect with

CRUD Operations on Elastic Search Using Rest clientLearn CRUD Operations on Elastic Search Using Rest client. CRUD (create index, read/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.

1. CRUD operations on elasticsearch using rest client

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.

2. what CRUD operations on elasticsearch

I have considered following operation under CRUD Operations on elastic search using rest client or postman or fiddler:

  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.

3. Pre-requisite: 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"
	}
}

4. 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, means content JSON added in an elastic index.

5. Get Indexed Data in Elastic using Rest Client

I have created only one document and try to get it all by the following the query. By default elastic returns only 10 documents but here I have added only 1 document so it returns 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 the document successfully added by the previous step, you should get similar like above else something else JSON output.

If the 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
}

6. Delete a document By Doc Id using rest Client

Delete is one of the CRUD operations on elastic saearch. 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
  }
}

7. 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

8. 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/

9. Reference

elastic.co for more details.
I hope you enjoyed this post CRUD operations in elasticSearch, visit ElasticSearch tutorial for more details.
Write your suggestions here to improve this post. Happy Learning! 🙂


Connect with

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

  1. Pingback: Mike

Leave a Comment

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