Search code examples
elasticsearchelasticsearch-5reindex

Elasticsearch reindex items in existing type mapping


I added a new property to a type mapping and I need to reindex all existing items of that type in order to use the new property.

Which API should I use to do this?


Solution

  • If you are adding a new field, which it never existed before in your index you don't need to reindex, you only have to add the new field by using PUT Mapping API http://nocf-www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

    Documents you created before updating the mapping with the new field will not contain this new field, so searches or aggregations won't take into account this field, it will work as a missing field.

    If you need that this new field is considered in searches on old documents using the default value of the type of the new field, then you need to reindex. For instance, if your new field is type integer and you explicitly need that this field be included in the old documents with zero value (default value) because you want to count how many documents have this new field = 0 then you need reindex, but most of the use cases we can consider missing fields as default value, so no need to reindex.

    There is no way in ElastiSearch (ES) to add a new field in the mapping and old indexes be updated automatically, even with the default value for that new index due to the nature of how ES store data. ES uses immutable segments to store the indexes so when you update a document, ES doesn't update physically the fields which changed, but it creates a new copy of the old document updated with the new data and mark as deleted the old one, so even when you update a simple field in a document, you get a new version of the document and the old one is marked as deleted