Elasticsearch "mapper ... cannot be changed" (illegal_argument_exception) in CI
A field mapping type in Elasticsearch is immutable once set. Trying to change text to keyword, or long to integer, on an existing index raises illegal_argument_exception. In CI this happens when a leftover index from a previous run collides with a new mapping definition.
What this error means
A mapping update fails with "illegal_argument_exception ... mapper [field] cannot be changed from type [X] to [Y]". The index already exists from an earlier run.
{"error":{"type":"illegal_argument_exception","reason":"mapper [price] cannot be changed
from type [long] to [double]"},"status":400}Common causes
Field mapping types are immutable
Elasticsearch does not allow changing the type of an existing mapped field; you must reindex into a new mapping instead.
A leftover index from a previous CI run
Persistent state or a reused volume kept the old index and mapping, so the new mapping definition conflicts with it.
How to fix it
Start each CI run from a clean index
- Delete the index at the start of setup so mappings are recreated fresh.
- Recreate it with the intended mapping.
- Ensure the service container does not persist a volume across runs.
curl -X DELETE "http://localhost:9200/products"
curl -X PUT "http://localhost:9200/products" -H 'Content-Type: application/json' -d '
{"mappings":{"properties":{"price":{"type":"double"}}}}'Reindex into a new mapping when data must survive
If you need the data, create a new index with the corrected mapping and reindex the old one into it, then swap an alias.
How to prevent it
- Use ephemeral, unnamed volumes so no index survives between runs.
- Delete and recreate test indices in a setup step.
- Treat field mapping types as fixed; plan a reindex for real type changes.