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.