Elasticsearch offers a mapping mechanism to its users. It allows the users to perform mapping on documents and their fields. A mapping is a process of indexing or storing the documents and fields in the database. It defines how the documents and their fields are stored and indexed.
We can define the datatypes for different types of fields, like - string, numbers, geo_point, etc. Therefore, to treat numeric fields as numbers, string fields as full-text or exact-value strings, and date fields as dates, Elasticsearch needs to know the datatype of each field. This information is stored in the mapping. Below is some important point to be noticed about mapping:
The following example will show you a demo of simple mapping, in which we will create an index named books with mapping -
Elasticsearch supports various datatypes for different fields. Let's discuss each datatype in brief:
Core datatype: These are the basic datatypes that are widely used. Some core datatypes are - short, integer, long, float, double, string, byte, Boolean, and date. These datatypes are supported by most of the systems.
Complex datatype: Complex datatypes are user-defined datatypes, which is the collection of core datatypes. The complex datatypes are - array, JSON objects (hierarchical in nature), and nested datatypes.
Geo datatype: Geo datatypes are the special type of datatype, which is used to store the geographical details like the location of a place. For example -
Specialized datatype: These datatypes are specialized datatype that holds the details which are unique such as IP address, token_count for counting the number of tokens in a string, and the auto-complete suggestions.
Every index has more than one type of mapping, which further divides the documents of the index into logical groups or chunks.
Mapping can be different from each other based on some factors such as Fields and Meta Fields.
Field or Properties: Fields are also be considered as properties. In elasticsearch, different mapping has various fields having different datatypes. In an index, fields with the same name but in different mapping types must have the same mapping.
Meta Fields: Meta fields make the information available about mapping as well as other objects associated with it, such as _index, _id, _source, and _type fields. They are responsible for customizing how the metadata associated with the document is treated.
Elasticsearch provides a mechanism to create the mapping automatically. This user-friendly mechanism is referred to as dynamic mapping. It allows the users to post the data directly to any undefined mapping. Elasticsearch then automatically creates the mapping for it.
In most cases, we already know the kind of data that gets saved in the document. So, whenever we create an index, it becomes easy to define fields and their types. This mechanism is called as static mapping, which is created by the user at index creation time.
Mapping parameters are the essential and fundamental factors that define the mapping. They also help to store the crux of the data - fields, and storage. Mapping parameters are responsible for how these details analyzed while being searched for. In Elasticsearch, following are the mapping parameters -
Get mapping details for index
Following command is used to get mapping associated with an index -
Removal of Mapping types
Elasticsearch 7.0.0 and higher versions do not support a _default_ mapping for indexes. However, an index created in Elasticsearch 6.x will continue to work as before. Types are deprecated in APIs in Elasticsearch 7.0 version.