Mapping and the dynamic mapping trap
Dynamic mapping is convenient exactly until the first document guesses wrong.
Open this lesson in the learning hubKey points
- Without an explicit mapping the first document to arrive decides the type of every field it carries.
- A string that looks like
2026-08-03is mapped as a date, so a later value of unknown fails. - A field type cannot be changed in place - correcting it means a new index and a full reindex.
index.mapping.total_fields.limitdefaults to 1000, and user-supplied keys will reach it.- Set
dynamic: strictto reject unknown fields, orfalseto store without indexing. - An array of objects loses its pairing across objects unless the field is typed as
nested.
Example
PUT /events
{
"mappings": {
"dynamic": "strict",
"properties": {
"status_code": { "type": "short" },
"created_at": { "type": "date" },
"user_id": { "type": "keyword" },
"message": { "type": "text" },
"metadata": { "type": "flattened" }
}
}
}
Define the mapping before the first document, and always read and write through an alias.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Elasticsearch Course course, and every lesson in it is listed on the Elasticsearch Course contents page.