0
0
Elasticsearchquery~5 mins

Bulk API for batch operations in Elasticsearch

Choose your learning style9 modes available
Introduction

The Bulk API lets you send many actions like adding or deleting documents in one request. This saves time and makes your work faster.

When you want to add many documents to Elasticsearch at once.
When you need to update multiple documents quickly.
When you want to delete many documents in one go.
When you want to reduce the number of requests to Elasticsearch to improve speed.
When you are importing a large dataset into Elasticsearch.
Syntax
Elasticsearch
{ action_and_meta_data }
{ optional_source }
{ action_and_meta_data }
{ optional_source }
...

Each action is on its own line in JSON format.

Actions include index, create, update, and delete.

Examples
This example adds a document with ID 1 and deletes a document with ID 2 in the "test" index.
Elasticsearch
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
This example updates the document with ID 1 by changing "field1" to "new value".
Elasticsearch
{ "update" : { "_index" : "test", "_id" : "1" } }
{ "doc" : { "field1" : "new value" } }
Sample Program

This bulk request adds two products with IDs 1 and 2, and deletes the product with ID 3 from the "products" index.

Elasticsearch
POST _bulk
{ "index" : { "_index" : "products", "_id" : "1" } }
{ "name" : "Apple", "price" : 1.2 }
{ "index" : { "_index" : "products", "_id" : "2" } }
{ "name" : "Banana", "price" : 0.5 }
{ "delete" : { "_index" : "products", "_id" : "3" } }
OutputSuccess
Important Notes

Make sure each JSON line ends with a newline character.

If any action fails, the response will show errors but still process other actions.

Use the Bulk API to improve performance when working with many documents.

Summary

The Bulk API lets you do many actions in one request to save time.

Actions include adding, updating, and deleting documents.

Each action is written as JSON lines, alternating metadata and data.