How to fix a wrong mapping without downtime
A mapping cannot be changed in place. The procedure we teach teams: aliases, a new index, a throttled reindex and an atomic switch.
- BITServices team
- #opensearch
- #elasticsearch
Sooner or later every team that runs Elasticsearch or OpenSearch finds a field with the wrong mapping. A text field that should have been a keyword. A nested field that did not need to be nested. An analyzer that does not match how people search. This post is the procedure we teach teams for fixing it without downtime.
You cannot change a mapping in place
You can add new fields to an existing index, but you cannot change the type or analyzer of a field that already exists. The data is already indexed one way, and changing the mapping would make it inconsistent.
So the fix is always the same: create a new index with the right mapping, copy the data into it, and switch over. The trick is to do it so the application does not notice.
1. Make sure the application uses an alias
If the application reads and writes an index name directly, start here. Create an alias and point the application at the alias instead of the index:
POST _aliases
{
"actions": [
{ "add": { "index": "orders-v1", "alias": "orders" } }
]
}
From now on the index behind orders can change without touching the application. Do this for every index, even the ones that look fine today.
2. Create the new index
Create orders-v2 with the corrected mapping and the same settings. Check the mapping twice. This is the moment to also fix anything else you have been meaning to fix, because every reindex costs time.
3. Reindex
POST _reindex?slices=auto&requests_per_second=2000&wait_for_completion=false
{
"source": { "index": "orders-v1" },
"dest": { "index": "orders-v2" }
}
slices=autosplits the work into parallel parts, one per shard.requests_per_secondthrottles it, so the reindex does not slow down search for your users. Start low and raise it while you watch the cluster.wait_for_completion=falseruns it as a task. Follow it withGET _tasks/<task id>.
For a large index, set number_of_replicas to 0 and refresh_interval to -1 on the new index while it fills, then set them back. It makes the reindex much faster.
4. Handle writes during the reindex
Documents written to the old index during the reindex are not in the new one. There are two common ways to deal with this:
- write to both indices during the move, then reindex once more with a query that only takes documents changed since the start
- pause writes for the short final step, if the business allows it
5. Check before you switch
- compare document counts with
GET _cat/count/orders-v1andGET _cat/count/orders-v2 - run your most common queries against both and compare the results
- look at the slow log on the new index
6. Switch the alias
The switch is one atomic call. The application never sees a moment without an index:
POST _aliases
{
"actions": [
{ "remove": { "index": "orders-v1", "alias": "orders" } },
{ "add": { "index": "orders-v2", "alias": "orders" } }
]
}
Keep the old index for a few days in case you need to switch back, then delete it.
What to watch afterwards
- shard size. Keep shards roughly between 10 GB and 50 GB.
- heap usage on the data nodes, and how often garbage collection runs.
- the slow log. New slow queries usually point to a mapping or query that needs work.
- shards per node. Too many small shards cost memory too.
Most mapping problems start as decisions that looked fine when the data was small. A team that knows this procedure can fix them early, while the index is still small and the reindex takes minutes, not days.