Elasticsearch – Mapping

In elasticsearch mappings are used to define how documents and its fields should be stored and indexed. The point of doing this is to store and index data in a way that is appropriate for how we want to search our data.

Dynamic Mapping
Mapping can be defined explicitly, which is referred to as explicit mapping. That’s when we tell elasticsearch what to do with our data when we add a new document or update the exists once. Dynamic mapping means no mapping is defined explicitly. When adding new documents elasticsearch will automatically add a mapping for any fields that do not have any mapping already. This is done by inspecting the types of values for our document field.

To check the mappings structure. Reference for advance operations

Meta Fields 
Every document that stored within an elasticsearch cluster has some metadata associated with them, apart from the data field. These fields are called meta field. There are 10 of them as of today. Few of them are important.


  • _index: This field added to the document automatically, and it used by elasticsearch internally. It simply contains the name of the index which the document belongs. This used internally when documents within an index but also can be used externally when searching for documents in multiple indexes.

  • _id: Stores the id of documents.

  • _source: Contains the original JSON object that was passed to the elasticsearch when indexing the document. The field is not indexed there for you cannot search it. But you can retrieve it.

  • _field_names: Contains the names of every field that contains a non-null value. This is used with the query named “exists”, which matches documents that contain a non-null value for the given field.

  • _routing: Elasticsearch uses custom routing to rout documents to shard based on a specified value, then store this value in the _routing meta field. Unless you are dealing with custom routing, you don’t need to deal with this field.

  • _version: Elasticsearch uses versioning of documents internally with a meta field named _version. If you retrieve a document by id this meta field will be a part of the result.

  • _meta: Can store custom data that is not touched in any way by elasticsearch. You can store application specific data.

Adding Mappings to existing indices

We add mappings by using the PUT verb and using the _mapping API. We add a request body, then the property key, there here we add a key for each field that we want to add a mapping for.

If we try to change the above code and decide type will be an integer and try to change it, we will get an error. Because of existing mappings for the fields cannot be updated. The only way we have till now (5.6.*) is to delete the index. So please be very very careful before mapping or adding any modification.

Defining Custom Date Format 

This strict_year meaning we have to specify a full year. There is the strict version of most of the build in date formats.

The above are the other formats.