liveBook · Manning

Dynamic mapping

As explained in Elasticsearch in Action, Second Edition

Dynamic mapping is Elasticsearch behavior that automatically derives an index mapping from the fields and values of newly indexed documents.

Dynamic mapping lets Elasticsearch create field mappings when it first indexes a document. Instead of defining a schema first, you send JSON and Elasticsearch inspects each value: strings become text fields, matching date strings can become date fields, and numeric values can become numeric fields. The resulting mapping is then consulted for later documents. section 4.1.2 section 4.2

Start with a movie document

Indexing this movie without a predeclared mapping causes Elasticsearch to derive title as text, rating as float, and release_year as date from the supplied values. A text title supports full-text queries; Elasticsearch also creates title.keyword as a keyword multi-field for exact-value operations. section 4.1.2

Listing 4.1 Indexing a document with ID 1 into a movies index
PUT movies/_doc/1
{
  "title":"Godfather",           #1
  "rating":4.9,                  #2
  "release_year":"1972/08/01"    #3
}

Use a simple first document to see dynamic mapping establish field types from JSON values. section 4.1.2

Type inference follows value shapes, not application intent. A value such as 4.9 is mapped as float, while 4 is mapped as long. Date detection recognizes yyyy-MM-dd and yyyy/MM/dd during dynamic mapping; a value such as dd/MM/yyyy can instead become text. Once a field has the wrong type, sorting, filtering, and aggregations may not be available as intended. section 4.2.1

Watch for these practical limitations:

  • Quoted numbers are strings. An age value written as "12" is dynamically mapped as text, so ordinary sorting on age fails. section 4.2.2
  • Use the automatically created keyword subfield, such as age.keyword, when sorting exact string values instead of enabling fielddata on text fields. section 4.2.2
  • Array fields take their type from the first element; mixed-type arrays are not allowed. section 4.6.3

Sort a dynamically mapped string value

If student ages arrive as quoted values, sort on age.keyword rather than age. The keyword field preserves an exact value and supports the sort, whereas enabling fielddata on text can consume heap memory and cause costly computation. section 4.2.2

Dynamic mapping is useful for development and testing, where documents can be explored quickly. For a known production data model, define mappings before indexing so dates, numeric measurements, and searchable fields receive deliberate types. Automatic index creation also uses dynamic mapping, so review or disable that path when tighter operational control is needed. section 4.2 section 4.8 section 6.2

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains value-based type inference, date rules, multi-fields, array behavior, and sorting failures; defers detailed core data types.
Examples
Uses movie title, rating, and release date fields, plus quoted student ages and age.keyword sorting.
What it assumes
Introduces mapping from first-document indexing while expecting basic awareness of core data types.
What you can do after
Evaluate/understand when inferred field types support querying and sorting and when explicit mappings are safer.
How it connects
Connects mapping to text, keyword, date, float, long, multi-fields, fielddata, arrays, and explicit mapping.

Also explained in

Compare how 2 books explain dynamic mapping →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.