Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Cluster Anatomy and Mappings  ›  Lesson

Dynamic Mapping — Elasticsearch Guesses the Schema

Dynamic 12 minWhen a new field arrives, dynamic mapping picks its type from the first value it sees, and that type then cannot change.

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/_mapping

The 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?"

Book 3 arrives first with a surpriseStep 1 of 3
  1. 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.

quick drill

A new field first arrives as "in_stock": true. What type is mapped?

quick drill

What does a new string field like "author": "Mira Das" get by default?

quick drill

pages is mapped as long. A later document sends "pages": "many". What happens?

quick drill

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 numbers long; decimals float.
  • dynamic: true adds, false ignores, strict rejects.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPredict the mapping

A new index receives its first document: {"title": "Rivers of Code", "pages": 480, "price": 35.5, "published": "2023-01-20", "rating": "4.5"}. Predict the type of each field and explain which guess causes trouble later and why.