Dynamic Mapping — Elasticsearch Guesses the Schema
You can store a book without ever telling Elasticsearch what a "price" or a "published date" is. It looks at the first value it sees and writes down a guess. That written-down schema is the mapping, and the guessing is dynamic mapping.
The rule
The first value a field ever sees decides its type — for good.
A mapping lists each field and its type, and the type decides how the value is stored and searched. When a document arrives with a field the mapping does not know, Elasticsearch infers a type from the JSON value:
| JSON value | Mapped as |
|---|---|
"The Quiet Garden" (a string) |
text, plus a keyword sub-field |
"2019-04-02" (a date-looking string) |
date |
320 (a whole number) |
long |
12.5 (a decimal) |
float |
true |
boolean |
{ "city": "Pune" } |
object, with its own inner fields |
[ "a", "b" ] |
the type of its elements — arrays need no type of their own |
Try it on an index that does not exist yet:
PUT /books/_doc/1
{ "title": "The Quiet Garden", "price": 12.5, "pages": 320, "published": "2019-04-02" }
GET /books/_mappingThe index is created on the fly, and the mapping shows title as text with a title.keyword sub-field, price as float, pages as long, and published as date.
Once a field has a type, it cannot change. If book 2 arrives with "pages": "about two hundred", it is rejected: a string cannot be stored in a long field.
You control how strict the guessing is with the dynamic setting:
true(default) — new fields are added to the mapping.false— new fields are kept in the stored document but not indexed, so you cannot search them.strict— a document with an unknown field is rejected.
The shortcut
For each new field, look only at its first JSON value and read the table. Then ask: "will any later value disagree?"
The document
{ "title": "Rivers of Code", "isbn": 9781234567897, "rating": "4.5" }
That is the trap: dynamic mapping is a helpful draft, not a design.
Drills
Answer fast — trust the rule.
A new field first arrives as "in_stock": true. What type is mapped?
What does a new string field like "author": "Mira Das" get by default?
pages is mapped as long. A later document sends "pages": "many". What happens?
With "dynamic": "false", a document arrives with an unknown field notes. What happens?
Remember
- The first value a field ever sees decides its type — for good.
- Strings become
text+keyword; whole numberslong; decimalsfloat. dynamic: true adds, false ignores, strict rejects.
