Elasticsearch: Bulk Inserting Examples

Last updated:

Bulk inserting is a way to add multiple documents to Elasticsearch in a single request or API call.

This is mainly done for performance purposes - opening and closing a connection is usually expensive so you only do it once for multiple documents.

Examples work for Elasticsearch versions 1.x, 2.x and probably later ones too

For these examples, let's assume you have an index called "myIndex" and a type called "person" having name and age attributes.

Simplest possible bulk insert with 2 documents

Don't forget the extra newline after the last document!

Note that the URL used contains both the index and the type. This is the case when all documents belong to the same type and will be inserted into the same index.

POST http://path.to.your.cluster/myIndex/person/_bulk
{ "index":{} }
{ "name":"john doe","age":25 }
{ "index":{} }
{ "name":"mary smith","age":32 }

The structure is more or less like this: one line with the action you want to perform (in this case, "index") and a line with the actual document. Repeat as many times as you want, and don't forget the extra newline at the end.

Inserting documents belonging to different types and indices in the same request

If the documents you wish to insert don't all belong to the same type and index, you need to add the index and type in the action line, before each actual document:

POST http://path.to.your.cluster/_bulk
{ "index":{ "_index": "myIndex", "_type": "person" } }
{ "name":"john doe","age":25 }
{ "index":{ "_index": "myOtherIndex", "_type": "dog" } }
{ "name":"fido","breed":"chihuahua" }

Manually specifying the ID for each inserted document

In addition to the index name and the type, you can also provide the id for each document in a bulk insert request:

POST http://path.to.your.cluster/myIndex/person/_bulk
{ "index":{} }
{ "name":"john doe","age":25 }
{ "index":{ "_id": "8a78dhkujg" } }
{ "name":"mary smith","age":32 }


See also

Dialogue & Discussion