Mapping tells Elasticsearch how to understand and store your data. Dynamic mapping guesses the data type automatically, while explicit mapping means you tell Elasticsearch exactly what each field is.
0
0
Dynamic vs explicit mapping in Elasticsearch
Introduction
When you want Elasticsearch to automatically add new fields without manual setup.
When you know the exact data types and want to control how data is indexed.
When you want to avoid mistakes from wrong automatic guesses.
When you want to optimize search performance by defining field types clearly.
When you want to prevent unwanted fields from being added to your index.
Syntax
Elasticsearch
PUT /my_index
{
"mappings": {
"dynamic": true | false | "strict",
"properties": {
"field_name": { "type": "text" | "keyword" | "date" | ... }
}
}
}dynamic can be true (default), false, or strict.
properties defines explicit fields and their types.
Examples
Dynamic mapping is enabled, so new fields are added automatically.
Elasticsearch
PUT /my_index
{
"mappings": {
"dynamic": true
}
}Dynamic mapping is off. Only
name and age fields are allowed.Elasticsearch
PUT /my_index
{
"mappings": {
"dynamic": false,
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" }
}
}
}Strict mode rejects any new fields not defined explicitly.
Elasticsearch
PUT /my_index
{
"mappings": {
"dynamic": "strict",
"properties": {
"email": { "type": "keyword" }
}
}
}Sample Program
This creates an index with explicit mapping. The pages field is not defined and dynamic mapping is strict, so it causes an error.
Elasticsearch
PUT /library
{
"mappings": {
"dynamic": "strict",
"properties": {
"title": { "type": "text" },
"author": { "type": "keyword" },
"published_year": { "type": "integer" }
}
}
}
POST /library/_doc/1
{
"title": "Learn Elasticsearch",
"author": "Jane Doe",
"published_year": 2023,
"pages": 250
}OutputSuccess
Important Notes
Dynamic mapping is convenient but can cause unexpected fields to be added.
Explicit mapping gives you control and can improve search speed and accuracy.
Use dynamic: strict to prevent any new fields from being added by mistake.
Summary
Dynamic mapping lets Elasticsearch guess field types automatically.
Explicit mapping means you define each field and its type yourself.
Choosing between them depends on how much control and safety you want.