Mapping defines the structure of documents. Eg. Fields and their data types. Mapping is also used to configure how values are indexes. Similar to table’s schema in a realtional database, mapping provides the schema for the document stored in Elasticsearch.
object
data type, but maintains object relationshipkeyword
field are anlyzed with the keyword analyzerkeyword
fields are used for exact matching, aggregation, and sortingPUT /reviews
{
"mappings": {
"properties": {
"rating": {"type": "float"},
"content": {"type": "text"},
"product_id": {"type": "integer"},
"author": {
"properties": {
"first_name": { "type": "text"},
"last_name": { "type": "text"},
"email": { "type": "keyword"}
}
}
}
}
}
PUT /reviews/_doc/1
{
"rating": 4.5,
"content": "Best Review",
"product_id": 123,
"author": {
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@gmail.com"
}
}
GET /reviews/_mapping
GET /reviews/_search
{
"query": {"match_all": {}}
}
long
values internallyformat
- Used to customize the format for date
fields
properties
- Defines nested fields for object and nested fields
coerce
- Used to enable or disable coercion of value
doc_values
-
norms
- Normalization factors used for relevance scoring.Useful for fields that won’t be used for relevance
index
- Disable indexing for a field. Values are still stored within _source
null_value
- NULL values cannot be indexed or searched
copy_to
- Used to copy multiple field values into a “group field”
Updating existing mapping with new type requires reindexing
POST /_reindex
{
"source": {
"index": "reviews"
},
"dest": {
"index": "reviews_new"
}
}
aliases
PUT /reviews/_mapping
{
"properties": {
"comment": {
"type": "alias",
"path": "content"
}
}
}
PUT /_template/access-logs
{
"index_patterns": ["access-logs-*"],
"settings": {
"number_of_shards": 2,
"index.mapping.coerce": false
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"url.original": {
"type": "keyword"
},
"http.request.referrer": {
"type": "keyword"
},
"http.response.status_code": {
"type": "long"
}
}
}
}
PUT /access-logs-2020-01-01
GET /access-logs-2020-01-01/
You can combine both Explicit as well as Dynamic Mapping
Using the match and unmatch is useful for applying naming convention as way for applying field mappings.
path_match
Parameter
path_unmatch
Parameter
Index templates apply mapping and index settings for matching indices. This happens when indices are created and their names match a pattern
Dynamic templates are evaluated when new fields are encountered. This specified field mapping is added if the template’s conditions match
Index template define fixed mappings; dynamic templates are.. dynamic
dynamic
to strict
, not false
. Avoid surprises and unexpected resultstext
and keyword
text
mapping - If you need full-text searcheskeyword
mapping - Aggregation, Sorting, or filtering on exact valuesdoc_values
to false, if you don’t need sorting, aggregation and scriptingnorms
to false if you don’t need relevance scoringindex
to false if you don’t need to filter on valuesWords that are filtered out during text analysis. Common words such as “a”, “the”, “the”, “at etc. They provide little to no value for relevance scoring.