liveBook · Manning

Mapping

As explained in Elasticsearch in Action, Second Edition

An Elasticsearch mapping specifies document fields, their data types, and their indexing and handling rules.

A mapping is an Elasticsearch schema: it describes a document’s fields, data types, and indexing rules. Elasticsearch consults those rules while indexing and searching, so text, dates, numbers, and other values can be analyzed, stored, and retrieved appropriately. section 4.1 section 4.1.1

For a movie or product index, mapping makes the data model intentional. A title can be treated as text for full-text search, a release date as a date for date operations, and a price as a numeric value for sorting and aggregations. Accurate field definitions support search, filtering, sorting, and aggregation. section 4.0 section 4.1

Choose how the schema is established:

  • Use dynamic mapping when Elasticsearch should infer field types from incoming documents. It helps begin indexing quickly, but inferred definitions can be incorrect. section 4.0 section 4.3
  • Use explicit mapping when the data model is known. Define fields and types while creating an index, then use the _mapping endpoint to add attributes as the model grows. section 4.3 section 4.3.1
  • Define inner JSON objects with subfield properties. Elasticsearch deduces an object type for an inner object unless it is specified otherwise. section 4.3.1
  • Give a field multiple types when it needs different search behavior, such as text plus keyword for full-text and exact-value queries. section 4.4

Create an employee schema before indexing

This request creates an employees index and declares searchable names, numeric ages, exact email values, and an address object with its own fields. Defining the schema up front puts the application, rather than type inference, in control of how values are handled. section 4.3.1 section 4.0

Listing 4.4 Creating the schema up front
PUT employees
{
  "mappings": {
    "properties": {
      "name":{ "type": "text"},         #1
      "age": {"type": "integer"},       #2
      "email": {"type": "keyword"},     #3
      "address":{                       #4
        "properties": {                 #5
          "street":{ "type":"text" },
          "country":{ "type":"text" }
        }
      }
    }
  }
}

Inspect the complete PUT request to see the mappings and properties wrappers, field types, and nested address fields together. section 4.3.1

Treat mappings as part of index design alongside settings and aliases. Settings tune shards and replicas, while mappings define the field schema used to index and query data efficiently. When the model is known, define mappings before production indexing rather than relying on dynamic inference. section 6.0 section 4.8

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains dynamic and explicit schemas, creation and updates, nested properties, and selected indexing structures; defers detailed data-type treatment.
Examples
Uses news articles, employees, students, cars, movies, and e-commerce products as schema-definition examples.
What it assumes
Introduces JSON fields and data types, while offering a relational-database analogy for readers who know it.
What you can do after
Apply it via Elasticsearch create-index and _mapping APIs to define fields and add new attributes.
How it connects
Connects mapping with dynamic mapping, explicit mapping, data types, text analysis, indexing, aggregations, sorting, filtering, and nested objects.

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.