How to update the mapping in Elasticsearch to change the field datatype and change the type of analyzers in string How to update the mapping in Elasticsearch to change the field datatype and change the type of analyzers in string elasticsearch elasticsearch

How to update the mapping in Elasticsearch to change the field datatype and change the type of analyzers in string


You cannot change existing data types mapping. As Elastic docs say:

Although you can add to an existing mapping, you can’t change existing field mappings. If a mapping already exists for a field, data from that field has probably been indexed. If you were to change the field mapping, the indexed data would be wrong and would not be properly searchable.

We can update a mapping to add a new field, but we can’t change an existing field from analyzed to not_analyzed.

Your only option is to create a new index with the new mapping and reindex the data from the old index to the new one.


No, you cannot change a single field definition.

If you want to change the field definition for a single field in a single type, you have little option but to reindex all of the documents in your index.


Why can't you change mappings? This article Changing Mapping with Zero Downtime explains,

In order to make your data searchable, your database needs to know what type of data each field contains and how it should be indexed.

If you switch a field type from e.g. a string to a date, all of the data for that field that you already have indexed becomes useless. One way or another, you need to reindex that field.

This applies not just to Elasticsearch, but to any database that uses indices for searching. And if it isn't using indices then it is sacrificing speed for flexibility.


What happens when you index a document with an incorrect field type?

A conversion will be attempted. If no valid conversion exists, an exception is thrown.

Elasticsearch: The Definitive Guide has a note regarding an example, a string is entered but long is expected. A conversion will be attempted. But an exception will still be thrown if no valid conversion exists.

[...] if the field is already mapped as type long, then ES will try to convert the string into a long, and throw an exception if it can’t.


Can I have the document indexed anyway, ignoring the malformed fields?

Yes. ES5 provides a ignore_malformed mapping parameter. Elasticsearch Reference explains that,

Trying to index the wrong datatype into a field throws an exception by default, and rejects the whole document. The ignore_malformed parameter, if set to true, allows the exception to be ignored. The malformed field is not indexed, but other fields in the document are processed normally.