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 use Elasticsearch version 1.5

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!

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.

Dialogue & Discussion