Sub topic 11
Reindexing and changing the schema safely
This chapter assumes you know what an index mapping is (see the chapter on index design) and how the sync pipeline feeds the index from the source of truth. Here we cover how to change a mapping or analyzer without downtime.
Why you must reindex
Most mapping changes cannot be applied to an existing index. Once documents are indexed, each field's type and analyzer are baked into the stored index structures. You cannot change category from text to keyword in place. Changing an analyzer, for example adding stemming, has the same limit: existing terms were produced by the old analyzer, so they do not match what the new analyzer would produce for queries.
The fix is to build a new index with the new mapping, copy the data into it, and switch traffic over. This process is called reindexing.
Use an alias, not an index name
If the application queries products_v1 directly, switching to products_v2 means a code deploy. Instead, the application queries an alias, a stable name that points to one or more real indexes. Moving the alias is a single atomic operation, so readers never see a half-built index.
POST /_aliases
{
"actions": [
{ "remove": { "index": "products_v1", "alias": "products" } },
{ "add": { "index": "products_v2", "alias": "products" } }
]
}Both actions run as one step. Queries to products use v1 before the call and v2 after it.
The zero-downtime procedure
Follow these steps:
- Create
products_v2with the new mapping. Keepproducts_v1live. - Backfill: copy all documents from the source of truth (or from
v1) intov2. - Keep
v2current while the backfill runs, by writing live changes to both indexes. - Verify
v2: compare document counts and run sample queries against both. - Swap the alias to
v2. - Keep
v1for a while as a rollback target, then delete it.
Backfill from the source of truth
Prefer rebuilding from the database over copying from the old index. The old index only holds fields it was mapped to store, and it may contain stale or wrong data. Rebuilding also lets you add new derived fields.
The external versioning from the sync chapter keeps the backfill safe. A backfill reads a row at one moment, and a live update may arrive before the backfill writes that row. With version_type=external, the index rejects the older write.
PUT /products_v2/_doc/42?version_type=external&version=7
{
"name": "Trail running shoes",
"category": "footwear",
"price": 89.0,
"in_stock": true
}If a newer version of document 42 is already in v2, this request fails with a version conflict. Treat that as success and move on.
Common errors
- Missing deletes. Rows deleted during the backfill can be left in
v2. Dual-write deletes as well as updates, or reconcile counts by ID before the swap. - Skipping verification. A swap with an empty or partial index looks like "all products vanished." Check counts first.
- Deleting
v1immediately. Keep it untilv2has served real traffic without problems. - Resource contention. A fast backfill competes with live queries. Throttle it and run it off-peak.
Exercise
Your team changes the name analyzer to add stemming. Why can't you apply this to the existing index, and what are the steps to roll it out without downtime?
Show answer
Existing terms in the index were produced by the old analyzer, so queries analyzed with the new one will not match them consistently. Create a new index with the new analyzer, backfill it from the source of truth while dual-writing live changes (using external versions so older writes lose), verify counts and sample queries, atomically swap the alias to the new index, then keep the old index as a rollback target before deleting it.