Mapping defines the types, formats etc. for different fields in an Elasticsearch document. Mapping also defines various ways in which fields / types behave in different situations (e.g. dynamic mapping allows creation of types dynamically). Mpping may also denote how a document’s metadata associated (e.g. _index, _type, _id, and _source ) is treated.
Mappings are defined for each type and with ES 6.2 there can be only one type per index. Therefore mappings are defined per index.
Field Types
Field datatypes can be:
-
a simple type like text, keyword, date, long, double, boolean or ip.
-
a type which supports the hierarchical nature of JSON such as object or nested.
-
or a specialised type like geo_point, geo_shape, or completion.
Example:
PUT index1/_mapping/_doc
{
"properties": {
"name": {
"type": "text"
}
}
}
You can create sub fields with different types for the same field:
PUT index1/_mapping/_doc
{
"properties": {
"name": {
"type": "text",
"fields": {
"keyword_typed" : {
"type" : "keyword"
}
}
}
}
}
Note
-
You then refer to the field as name.keyword_typed. Usually it is named as keyword itself, but I have used a different name to denote that it can be any name.
-
Keyword fields are similar to text, but are not analyzed and hence only searchable by their exact value. It is generally used for structured content such as email addresses, hostnames, status codes, zip codes or tags.
Defining Mappings
Dynamic Mapping
With dynamic mapping (enabled by defaut) we do not have to pre-define mappings, Elasticsearch will guess the most appropriate mapping type / types for your fields.
By default, for text fields, elasticsearch assigns "text" type, but also add a sub field called "keyword" of type "keyword".
See examples for dynamic mapping here.
Explicit Mapping
We can define our own mappings either while creating an index, or you can add fields to an existing index with the PUT mapping API.
See examples for explicit mapping here.
Important Points on Mappings
-
Existing field mappings cannot be updated in general. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index.
-
You can add mappings for new fields that does not already have mappings.
-
You can add sub fields for existing fields with different types.
-
-
The following settings allow you to limit the number of field mappings that can be created manually or dynamically:
-
index.mapping.total_fields.limit
-
index.mapping.depth.limit
-
index.mapping.nested_fields.limit
-
- heartin's blog
- Log in or register to post comments
Recent comments