Re-Indexing in elastic search
Recently working on my current project had faced a scenario where a particular field of an index is not able to add or update data. After a long finding able to find or detect that there is an issue related to data type in an index for a particular field (or key).
Being a database developer for such a scenario, we always get a hint to alter the data type. Blindly altering data type is not the solution let’s research more on this and bring up some of our findings.
In Elasticsearch to add or alter a field in an index, we have an API _mapping. This API will help us to check defined mapping on an index whether it's static mapping or dynamic mapping.
Want to learn more about mapping and its types in elastic search please check my previous article.
Mappings in Elasticsearch
In this article, we will see how mappings are used in Elasticsearch and its pros and cons.
Let’s consider practically how re-indexing is implemented and what problems we face during re-indexing.
Consider an index order with static mapping.
And add some records to this index.
- In most scenarios, the elastic search will not allow us directly to update the data type of existing fields either it will make existing data invalid or will throw an error.
- If still, we want to update the field of an index then follow these mentioned steps:
(i) create a new index with the updated data type of field in an index.
(ii) Re-index the data from the old index to the new index.
(iii) delete the old index.
(iv) Re-name the new index with the mapped index name. We can re-name using _alias API in elastic search.
Note: This method requires minimal downtime for re-indexing data.
This approach is useful when altering data type from int to text or int to keyword or both scenarios.
Thanks for the read. Do clap if find it useful😊.
“Keep learning and keep sharing knowledge”