Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

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.

Tuesday at the bookshopStep 1 of 3
  1. A supplier file of 5,000 books

    You have the documents → _bulk, in batches.

Drills

Answer fast — trust the rule.

quick drill

In a _bulk body, which action has no source line after it?

quick drill

A _bulk request returns HTTP 200 with "errors": true. What does that mean?

quick drill

Which is a sensible setting during a large initial load?

quick drill

You need to remove every out-of-stock book. Which API fits?

quick drill

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.
  • _bulk is NDJSON, ends with a newline, and succeeds or fails per item.
  • Create the destination with the right mapping before _reindex.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPlan a big load

You must load 200,000 supplier books into a new index, then remove every book published before 2000. Explain which APIs you would use, two settings or habits that speed up the load, and how you would check the load succeeded.