Scrivio Interview library
CodingIntermediate7 min read · updated 12 November 2026

You must change a field from text to keyword in an existing index with 50 million documents and no downtime for search. Describe the steps and the APIs you would use.

An existing field's type cannot be changed in place, so we create a new index with the field mapped as keyword, copy the 50 million documents with the _reindex API using slices for parallelism or by re-running indexing from the source of truth, and keep the application pointed at an alias the whole time. Changes made during the copy are caught up by dual writing or a second incremental pass, and once counts and sample queries match, one _aliases call swaps the alias atomically. The old index stays for a short rollback window and is then deleted.

Why this question is asked

Mapping changes are where search clusters stop feeling like ordinary databases. In a relational database we would run ALTER TABLE; in Elasticsearch or OpenSearch the data was analysed and indexed according to the old type, so changing the type means indexing everything again. With 50 million documents and users searching all day, the job is to do that rebuild without anyone noticing. Interviewers ask for the steps and APIs because they reveal whether we have done this for real. In this answer, we will see why the change cannot be made in place, set up an alias, build and fill the new index, keep it in sync, swap and verify, and plan the rollback.

1Why the Type Cannot Change in Place

A text field's values were analysed into individual terms when each document was indexed, and only those terms are in the inverted index. A keyword field needs the whole original value as one term, plus doc values for sorting and aggregations. Those structures simply do not exist for the 50 million documents, and Lucene segments are immutable. So the engine rejects a mapping update that changes a field's type.

Java
PUT /orders_v1/_mapping
{ "properties": { "status": { "type": "keyword" } } }
// 400 illegal_argument_exception:
// mapper [status] cannot be changed from type [text] to [keyword]

There is one shortcut worth knowing: we may add a new keyword subfield, such as status.raw, to the existing text field, and then run _update_by_query to re-index every document in place so the subfield gets values. That avoids a second index, but the original field stays text forever and queries must use the new name. When the goal is a clean mapping, a new index is the better path.

2Putting an Alias in Front First

An alias is a name that points to one or more indices. If the application already uses an alias, for example orders pointing to orders_v1, we are ready. If it uses the index name directly, the first step is to deploy the application to use an alias, because the swap at the end depends on it.

Java
POST /_aliases
{ "actions": [ { "add": { "index": "orders_v1", "alias": "orders", "is_write_index": true } } ] }
// the application now reads and writes /orders/...

From now on the application never needs to know which physical index it uses. The same alias makes every future mapping change routine.

3Creating and Filling the New Index

We create orders_v2 with the corrected mapping and the same settings, except for two temporary ones that make the bulk copy much faster: no replicas and no periodic refresh.

Java
PUT /orders_v2
{ "settings": { "number_of_shards": 6, "number_of_replicas": 0, "refresh_interval": "-1" },
"mappings": { "dynamic": "strict", "properties": {
"status": { "type": "keyword" },
"customer": { "type": "text" },
"updated_at": { "type": "date" } } } }
POST /_reindex?slices=auto&wait_for_completion=false
{ "source": { "index": "orders_v1", "size": 2000 },
"dest": { "index": "orders_v2", "op_type": "index" } }
// { "task": "oTUltX4IQMOUUVeiohTt8A:12345" }
GET /_tasks/oTUltX4IQMOUUVeiohTt8A:12345
// "status": { "total": 50000000, "created": 31250000, ... }

slices=auto splits the reindex into parallel sub-tasks, usually one per shard of the source, which multiplies throughput. wait_for_completion=false returns a task id instead of holding an HTTP connection open for an hour, and the tasks API shows progress. _reindex reads from _source, so it requires _source to be enabled on the old index. If the source of truth is a database, re-running the normal indexing pipeline into orders_v2 is an equally good option, and sometimes better because it picks up any transformation changes too.

4Keeping the New Index in Sync

The reindex copies a snapshot, but users keep creating and updating orders while it runs. Those changes must reach orders_v2 before the swap. There are two common approaches.

The first is dual writing: before starting the reindex, the application or indexing pipeline writes every change to both orders_v1 and orders_v2. Because the reindex might copy an older version over a newer one, we use external versioning, for example with the database's version number, or run the reindex with op_type create so it never overwrites documents that dual writing already created, together with conflicts set to proceed so those skipped documents are not treated as failures. The second is a catch-up pass: note the time T when the reindex started, and after it finishes, run a second, smaller reindex for documents with updated_at at or after T. Repeat until the remaining gap is small, then briefly pause writes or rely on the pipeline's replay during the swap. Deletes need care in the catch-up approach, because a deleted document leaves nothing to copy; dual writing or a deletion log handles them.

Step throughZero-downtime reindex with dual writes
AppAlias ordersorders_v1orders_v2
write changes to v1
also write changes to v2
_reindex snapshot, sliced
searches keep using the alias
routes reads to v1
set replicas 1, refresh 1s
_aliases: remove v1, add v2 (atomic)
searches now served by v2
routes reads to v2

9 messages in order. Play them, or step through them one at a time.

5Verifying and Swapping the Alias

Before the swap, we restore replicas and the refresh interval on orders_v2 and wait for the cluster to be green for that index. Then we compare: document counts, counts per status value, and a set of real queries run against both indices, checking hit counts and top results. Queries that relied on the old text behaviour, such as a match on 'Shipped' finding 'SHIPPED', now need a term query or a normalizer, so the application change for the new type should be deployed and tested against orders_v2 first.

Java
GET /orders_v1/_count // { "count": 50000412 }
GET /orders_v2/_count // { "count": 50000412 }
POST /_aliases
{ "actions": [
{ "remove": { "index": "orders_v1", "alias": "orders" } },
{ "add": { "index": "orders_v2", "alias": "orders", "is_write_index": true } } ] }
// { "acknowledged": true } -> both actions applied together

The _aliases call is atomic: there is no moment when the alias points to neither index or to both for writes. Searches in flight finish on v1, and new ones go to v2. After the swap, we watch error rates and latency, and stop dual writes to v1 once we are confident.

6Rollback and Clean-Up

We keep orders_v1 for a short time, perhaps a few days, so that if a problem appears we can swap the alias back with the same atomic call. During that window, if we stopped writing to v1, a rollback would need the recent changes replayed, which is a reason to keep dual writes running for a day or two. When the new index has proved itself, we delete orders_v1 to free disk and heap. With 50 million documents, the extra index during the migration also needs disk space, so we check free disk before starting: the new index may be about the same size as the old one, and the cluster's disk watermarks must not be reached.

7Follow-up questions

How long does reindexing 50 million documents take?

It depends on document size, mapping and hardware, but tens of thousands of documents per second with slicing, no replicas and no refresh is common, so roughly 30 minutes to a few hours. A test on a sample of a few million documents gives a real estimate.

Can we throttle the reindex so search is not affected?

Yes. The requests_per_second parameter throttles _reindex, and it can be changed while running with the _rethrottle API. Running it at a quiet time and watching search latency is wise.

What if the application cannot dual write?

Use the catch-up approach based on an updated_at field and a deletion log, or replay changes from the source of truth, such as a change data capture stream, into the new index until it is current.

8Conclusion

In this answer, we changed a field from text to keyword without search downtime. Because types cannot change in place, we put the application behind an alias, created orders_v2 with the new mapping and fast bulk settings, filled it with a sliced _reindex or from the source of truth, kept it current with dual writes or catch-up passes, verified counts and real queries, swapped the alias atomically with _aliases, and kept the old index briefly for rollback before deleting it.

What to say in the interviewAbout 90 seconds aloud

Open with

You cannot change a field's type in an existing index, because the data was already analysed and indexed as text and segments are immutable, so the mapping update is rejected.

Then, if they want more

The approach is a new index behind an alias. First I make sure the application reads and writes through an alias, say orders pointing at orders_v1. Then I create orders_v2 with status mapped as keyword, and for the bulk copy I set replicas to zero and refresh_interval to minus one.

I copy the data with the _reindex API with slices auto and wait_for_completion false, and follow the task through the tasks API, or I re-run indexing from the database if that is the source of truth.

Writes keep happening, so I either dual write to both indices from before the reindex starts, using versioning so the copy cannot overwrite newer data, or I run catch-up reindexes for documents updated since the start time, and handle deletes.

When it is done, I restore replicas and refresh, wait for green, compare counts and run a sample of real queries against both, with the application already updated to use term queries on the keyword field. Then a single _aliases request removes v1 and adds v2 atomically.

I keep v1 for a few days so I can swap back, and then delete it.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Elasticsearch documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You must change a field from text to keyword in an existing index with 50 million...": https://scrivio-interview-library.netlify.app/databases/search-engines/bd45b37d11/