Lesson 2 of 9 · 27m
Mapping Design: Types, Multi-Fields, and Dynamic Mapping Traps
Choose mappings around query intent, prevent explosive dynamic schemas, and model identifiers, dates, numerics, and text with operational discipline.
Most long-lived search clusters do not collapse because search is hard. They collapse because every application team was allowed to invent fields forever. A mapping is a schema contract, a storage layout decision, and a performance budget at the same time. Treat it as infrastructure design, not as an incidental JSON annotation.
Start with query intent
For every field, ask what operation it must support:
full-text relevance text
exact filter/term agg keyword
numeric range/sort long, integer, double, scaled_float
time range/sort date
relational object shape nested only when truly needed
parent/child relationship join only after exhausting simpler modelsIf no one will search a value, consider excluding it from the index or storing it only in _source. Indexing everything because it might be useful later is how clusters accumulate dead weight.
Dynamic mapping is convenient until it is not
By default, Elasticsearch and OpenSearch infer mappings from incoming JSON. The first document decides the type, sometimes incorrectly:
{"order": "00125", "quantity": "3", "event_time": "2026-11-30T12:04:11Z"}order may become text with a keyword subfield even though it is an opaque identifier. quantity may become text because one legacy producer sent a string. Future numeric documents can then fail indexing. Explicit mappings prevent type roulette:
{
"mappings": {
"dynamic": "strict",
"properties": {
"order": {"type": "keyword"},
"quantity": {"type": "integer"},
"event_time": {"type": "date"}
}
}
}dynamic: strict rejects unexpected fields; dynamic: runtime can be useful for rare exploratory fields, but runtime fields shift cost to query time.
Mapping explosions are operational incidents
If incoming keys include user IDs, hostnames, request paths, or arbitrary labels, dynamic mappings can create thousands of fields. Each field consumes cluster state and memory. Guardrails matter:
{
"settings": {
"index.mapping.total_fields.limit": 1000,
"index.mapping.depth.limit": 20,
"index.mapping.nested_fields.limit": 50
}
}A field named labels.user_12345 is usually a data-modeling error. Prefer arrays of key/value objects or a dedicated flattened field where supported and appropriate.
nested is precise and expensive
Arrays of objects are not independent objects in a flat document. Without nested, this document can falsely match name: alice AND role: admin because values are matched across different objects:
{
"members": [
{"name": "alice", "role": "viewer"},
{"name": "bob", "role": "admin"}
]
}Use nested when intra-object correlation matters. Understand the cost: nested objects become hidden documents, multiplying document count and complicating updates.
Field notes
- Existing field types cannot generally be changed in place; create a new index and reindex with a corrected mapping.
keywordfields used in aggregations can consume significant memory when cardinality is high.textis not sortable in the way teams expect; use a keyword multi-field for exact sorting.- Keep mappings in version control beside index templates. Console-created schemas drift and disappear.
Was this lesson helpful?