Skip to the workspace
Pebellslearn by building
Documents and Leaf Queries  ›  Lesson

Many Documents at Once: Bulk, By-Query and Reindex

Bulk index 15 minUse _bulk when you list the documents yourself, the by-query APIs when a query describes them, and _reindex to copy them into a new index.

elasticsearch / Many Documents at Once: Bulk, By-Query and Reindex

Explore each scene at your own pace.

01 · The rule

The rule

Many Documents at Once: Bulk, By-Query and Reindex

The shop imports a supplier's catalogue of 200,000 books. Sending 200,000 separate PUT requests means 200,000 round trips — painfully slow. Elasticsearch has requests that act on many documents in one go.

The rule

Batch the writes you list; target the writes you describe.

When you list the documents yourself, use _bulk. Its body is NDJSON — one JSON object per line — alternating an action line and, for index/create/update, a source line:

POST /_bulk
{ "index": { "_index": "books", "_id": "2" } }
{ "title": "Gardening for Busy People", "price": 18.0 }
{ "index": { "_index": "books", "_id": "3" } }
{ "title": "Rivers of Code", "price": 35.0 }
{ "update": { "_index": "books", "_id": "1" } }
{ "doc": { "price": 10.0 } }
{ "delete": { "_index": "books", "_id": "4" } }
  • delete has no source line.
  • The body must end with a newline.
  • Each action succeeds or fails on its own. The reply has "errors": true if any failed — always check it, and look at each item's status.

Optimising a big load: send batches of a few megabytes (then measure and adjust), use several parallel workers, and for an initial load set "refresh_interval": "-1" and "number_of_replicas": 0, restoring both afterwards.

When you describe the documents with a query instead, use the by-query APIs:

POST /books/_update_by_query
{
  "query": { "term": { "genre": "hobby" } },
  "script": { "source": "ctx._source.price *= 0.9" }
}

POST /books/_delete_by_query
{ "query": { "range": { "published": { "lt": "2000-01-01" } } } }

The first takes 10% off every hobby book; the second removes every book published before 2000.

Reindex copies documents from one index into another — the standard fix after a mapping mistake:

POST /_reindex
{ "source": { "index": "books" }, "dest": { "index": "books_v2" } }

Create books_v2 with the correct mapping first; reindex does not copy mappings or settings.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.