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" } }deletehas no source line.- The body must end with a newline.
- Each action succeeds or fails on its own. The reply has
"errors": trueif 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.
The shortcut
Ask "do I have the documents, or a rule?" Have them in hand → _bulk. Have a rule → _update_by_query or _delete_by_query. Need a new home → _reindex.
A supplier file of 5,000 books
You have the documents →
_bulk, in batches.
Drills
Answer fast — trust the rule.
In a _bulk body, which action has no source line after it?
A _bulk request returns HTTP 200 with "errors": true. What does that mean?
Which is a sensible setting during a large initial load?
You need to remove every out-of-stock book. Which API fits?
After _reindex from books to a brand-new books_v2 that you did not create first, what mapping does books_v2 get?
Remember
- Batch the writes you list; target the writes you describe.
_bulkis NDJSON, ends with a newline, and succeeds or fails per item.- Create the destination with the right mapping before
_reindex.
