Reindex an Elasticsearch index
When to reindex
After updating an Elasticsearch index’s schema by changing the fields or document types, you need to reindex the affected index before the new fields and types can be used.
⚠️ Avoid reindexing in production during working hours, except to fix an outage.
Reindexing locks the index for writes, so content is not updated in the search index. Reindexing takes around 2 hours to complete.
See Fix out-of-date search indices if you need to reindex during working hours.
You don’t need to reindex when making a change to the govuk_document_types gem.
Instead, run the Rake task
search:update_supertypes to update documents
in-place. Updating supertypes can be done during working hours.
What the reindexing process does
- Locks the Elasticsearch index to prevent writes to the index while data is being copied
- Creates a new index using the schema defined in the deployed version of search-api
- Copies all the data from the old to the new index
- Compares the old and new data to check for inconsistencies
- If everything looks the same, switches the index alias to the new index
Reindex an Elasticsearch index
1. Run the reindex
To reindex all indices, run the
search:migrate_schema Rake task on search-api:
k exec deploy/search-api -- rake search:migrate_schema
To reindex a specific index, specify the
SEARCH_INDEX environment variable:
k exec deploy/search-api -- rake SEARCH_INDEX=government search:migrate_schema
To monitor progress, you can check how many documents have been copied to the new index:
k exec deploy/search-api -- sh -c 'curl "$ELASTICSEARCH_URI/_cat/indices?v"'
2. Clean up
Reindexing does not delete the old index. This lets us switch back to the old index if there is a serious problem with the new one.
Once you’re confident that the reindexing was successful, delete the old
(unaliased) index using the
search:clean Rake task:
k exec deploy/search-api -- rake search:clean SEARCH_INDEX=alias_of_index_to_clean_up
Avoid leaving old indices around, otherwise we may run into storage limitations and be unable to index new documents.
Failed to switch to new index
The final part of the reindex is to switch Elasticsearch over to the newly created indexes. We’ve noticed recently that this isn’t always successful. It appears that if content is written to the database while the reindex task is running, the task will fail at the end as it detects a difference in the data.
Re-running the reindex task usually fixes this.
To stop the reindexing job
If you need to cancel the reindexing while it’s in progress:
Find the ID of the reindexing task:
k exec deploy/search-api -- sh -c 'curl "$ELASTICSEARCH_URI/_tasks?actions=%2Areindex&pretty"'
Stop the task:
k exec deploy/search-api -- sh -c 'curl -XPOST "$ELASTICSEARCH_URI/_tasks/<task_id>/_cancel"'
Unlock the old index by running the
k exec deploy/search-api -- rake search:unlock SEARCH_INDEX=alias_of_index_to_unlock
To switch back to the old index
If you discover a problem after reindexing and need to switch back to the old
index, run the
search:switch_to_named_index Rake task:
k exec deploy/search-api -- rake search:switch_to_named_index[full_index_name] SEARCH_INDEX=index_alias
full_index_name is the full name of the new index, including the date
and UUID, e.g.
Switching back to an old index means that you’ll lose any content updates that were published while the new index was live. To fix this, re-present the changes from both publishing-api and Whitehall.