Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - do not map the fields by default

While indexing a document, Elasticsearch will automatically create the mapping for the missing fields (inside document)

Is it possible to configure (or) is there a configuration where we can instruct the elasticsearch NOT to create missing fields, instead ignore.

Basically, we use Java POJO, we use the instance of same POJO to index the document (by converting this instance to json using GSON library) and also use some of the fields in this POJO for some external purpose.

So when we set those fields used for external purpose, but send the document to Elasticsearch, these additional fields are also saved. We want to avoid that.

like image 398
Kumar D Avatar asked Jun 27 '14 09:06

Kumar D


People also ask

How do I turn off dynamic mapping in Elasticsearch?

You can disable dynamic mapping, both at the document and at the object level. Setting the dynamic parameter to false ignores new fields, and strict rejects the document if Elasticsearch encounters an unknown field. Use the update mapping API to update the dynamic setting on existing fields.

How do I remove a map from Elasticsearch?

Elasticsearch is a powerful search engine that makes it easy to delete mapping. To delete mapping, simply use the _delete_mapping API. This will remove the mapping from the index and all of the data associated with it.

What is field mapping in Elasticsearch?

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Each document is a collection of fields, which each have their own data type. When mapping your data, you create a mapping definition, which contains a list of fields that are pertinent to the document.

Why do we need to use mapping on an Elasticsearch index?

Mapping determines how a document and its fields are indexed and stored by defining the type of each field. It contains a list of the names and types of fields in an index. Depending on its type, each field is indexed and stored differently in Elasticsearch.


1 Answers

Yes, it is possible to disable the dynamic mapping feature within Elasticsearch so that mappings are not created dynamically when new fields are ingested. From the documentation:

Dynamic mapping

When Elasticsearch encounters a previously unknown field in a document, it uses dynamic mapping to determine the datatype for the field and automatically adds the new field to the type mapping.

Sometimes this is the desired behaviour and sometimes it isn’t. Perhaps you don’t know what fields will be added to your documents later on, but you want them to be indexed automatically. Perhaps you just want to ignore them. Or — especially if you are using Elasticsearch as a primary datastore — perhaps you want unknown fields to throw an exception to alert you to the problem.

Fortunately, you can control this behaviour with the dynamic setting, which accepts the following options:

  • true - Add new fields dynamically — the default

  • false - Ignore new fields

  • strict - Throw an exception if an unknown field is encountered

https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html

like image 90
John Petrone Avatar answered Oct 05 '22 12:10

John Petrone