Mappings in Elasticsearch
In this article, we will see how mappings are used in Elasticsearch and its pros and cons.
Mapping is the process that defines how a document, which contains fields is stored and indexed in Elasticsearch. Basically, it defines the schema of the document and datatype for fields in the document.
Syntax to check index mappings: GET /index_name/_mappings
There are two types of mappings used in Elasticsearch
This type of mapping allows us to choose how to define the mapping definition, such as provide an option to name our columns their data types, whether string value should be treated as keyword or full-text fields, numeric value to integer or long, format of date values and many more.
This type of mapping dynamically maps fields based on their value. Just by indexing a document, fields get mapped and also we can add a new column to our index. Based on the document field value Elasticsearch dynamically detects its data type.
Now let’s move to the implementation of mapping
Problem statement: Make use of explicit and dynamic mapping to create an index called employee having fields first_name and last_name.
Solution1: By making use of explicit mapping
Solution2: By making use of dynamic mapping
Pros and Cons of mappings
1) As per our requirement provide naming conventions to fields and their data types.
2) Can also add columns from dynamic mapping if “dynamic” property is not set to false or strict.
3) Save disk space with optimized mappings when storing many documents.
4) Avoids surprises and unexpected results.
1) Manual way to add mappings.
2) Require time, effort, and analysis while mapping columns.
1) Convenient and quick method.
2) Dynamically maps fields based on the document being indexed.
1) Consume disk space. (example: for string values maps to both text and keyword typically only one is needed)
2) Brings surprises and unexpected results.
3) Not optimized mapping.
Thank you for the read. Do clap if find useful.🙂