AI Hints
Home›Elasticsearch Tutorial›Versions, Bulk and Refresh
Elasticsearch · Lesson 17 of 120

The Bulk API

Many writes, one request.

⏱ 2 min read▶ Video at 15:27🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Trailhead has thousands of products. Sending one request per product is slow. The bulk API sends many actions in a single request.

  • Thousands of products
  • One by one is slow
  • Bulk: one request

Three actions in one

The body is new line delimited JSON. Each action line, like index or delete, is followed by its document. Here we index two products, and delete one. Run it. Errors false means every action worked, and items shows each result.

Dev Tools Consoletext
POST _bulk?filter_path=errors,items.*.result
{ "index": { "_index": "products", "_id": "2" } }
{ "name": "Storm Rain Jacket", "price": 129.99 }
{ "index": { "_index": "products", "_id": "3" } }
{ "name": "Trail Hiking Boots", "price": 149.99 }
{ "delete": { "_index": "products", "_id": "1" } }
Output
{
  "errors": false,
  "items": [
    {"index": {"result": "created"}},
    {"index": {"result": "created"}},
    {"delete": {"result": "deleted"}}
  ]
}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

PUT products/_doc/1?refresh=true
{ "name": "Basecamp 2-Person Tent", "price": 237.99, "stock": 5 }

Bulk tips

Each line must be on one line, with no pretty printing. Send batches of a few thousand documents, not millions. Always check the errors flag. One bad document doesn't stop the others.

keyvalue
Formatone JSON object per line
Batch sizea few thousand docs
Checkerrors: true/false
Failuresper document

Summary

The bulk API does many index, create, update and delete actions in one request, and reports each one. Next, counting, and when new documents become searchable.

Key takeaways
  • POST _bulk
  • Action + doc
  • Check errors
Next up: Counting and Refresh — near real-time