Tuesday, 20 October 2015

Elasticsearch: _bulk : Sending multiple requests

In previous post, I explained how to retrieve multiple documents using _mget endpoint. In this post, I am going to explain _bulk endpoint, used to perform multiple operations at a time.

Following is the syntax for _bulk request body.

{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...

“action” can be create, index, update, or delete.
Action
Description
create
Create document, if it is not exist.
index
Create new document, or replace existing document.
update
Perform partial update on a document.
delete
Delete a document
“metadata” is used to identify a document, it contains “_index”, “_type”, “_id”.
POST /_bulk
{"delete" : {"_index": "xyz", "_type": "employees", "_id" : "1"}}
{"create" : {"_index": "xyz", "_type": "products", "_id" : "2"}}
{"items_available" : "23"}
{"index" : {"_index" : "xyz", "_type":"employees", "_id" : "6"}}
{"firstName" : "Ranganath", "lastName": "ranga"}
{"update" : {"_index": "xyz", "_type": "employees", "_id" : "6"}}
{"doc" : {"age" : "30"}}


Following is the response for above request.

{
   "took": 3,
   "errors": false,
   "items": [
      {
         "delete": {
            "_index": "xyz",
            "_type": "employees",
            "_id": "1",
            "_version": 5,
            "status": 200,
            "found": true
         }
      },
      {
         "create": {
            "_index": "xyz",
            "_type": "products",
            "_id": "2",
            "_version": 1,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "xyz",
            "_type": "employees",
            "_id": "6",
            "_version": 1,
            "status": 201
         }
      },
      {
         "update": {
            "_index": "xyz",
            "_type": "employees",
            "_id": "6",
            "_version": 2,
            "status": 200
         }
      }
   ]
}

Note:
1.   Every line in _bulk request body must end with new line (including last line).
2.   Request body is not required for delete operation.
3.   When we submit number of requests at a time, each sub request handled independently. So failure of one sub-request don’t impact other request.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment