Course contents

Mapping and field types

Design an explicit catalog mapping and understand type choices.

5 sectionsElasticsearch 9.4.3Not read yet

What you will learn

  • Explicit schema rather than guesses
  • Strings, numbers and dates
  • Absence and evolution

Design an explicit catalog mapping and understand type choices.

How it works
  • Input contract
  • Types and representations
  • Query capabilities

Design an explicit catalog mapping and understand type choices.

The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.

01 / 05

Explicit schema rather than guesses

Dynamic mapping infers types from early values. Useful for exploration, it can misrepresent money, identifiers and dates. Explicit mapping makes the contract deliberate. dynamic=strict rejects unknown fields; dynamic=false keeps them in _source without dynamically indexing them. Choose according to how much control you have over producers.

Check your understanding
02 / 05

Strings, numbers and dates

Product names use text; brands and SKUs use keyword. Prices can use integer minor units or scaled_float with a defined factor; rounding must follow business rules. Dates have parsing formats and time-zone interpretation. A numeric-looking SKU is not necessarily a number: leading zeros and identity may matter more than arithmetic.

Check your understanding
03 / 05

Absence and evolution

There is no separate array type: a field can contain several compatible values. null and empty arrays normally produce no indexed value, while an empty string may. exists checks indexed presence, not just a JSON key. index=false, doc_values and enabled affect different capabilities. Changing an existing type usually requires a new index and reindex; coerce is not input validation.

Create a teaching index with explicit types before loading data.

PUT /course-products
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name": {
        "type": "text"
      },
      "sku": {
        "type": "keyword"
      },
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      },
      "description": {
        "type": "text"
      },
      "brand": {
        "type": "keyword"
      },
      "rating": {
        "type": "float"
      },
      "in_stock": {
        "type": "boolean"
      },
      "product_id": {
        "type": "keyword"
      }
    }
  }
}

How to read this result

Create a teaching index with explicit types before loading data.

Check your understanding
04 / 05

Catalog reasoning example

For a price of 19.995 with two-decimal requirements, define business rounding before choosing a storage type. Keep SKU “00042” as a string despite its digits. rating may be optional while price is mandatory at ingestion. dynamic=strict is not complete business validation: a document with known fields but no price still needs validation. Mapping defines representation and retrieval capabilities, not every business invariant.

An unknown field under dynamic=strict

PUT /course-products/_doc/rejected
{
  "unknown_field": "unexpected"
}
Elasticsearch response
Shortened response · JSON
{
  "error": {
    "type": "strict_dynamic_mapping_exception"
  },
  "status": 400
}

How to read this result

After creating a strict mapping, an unknown field is rejected. This is a shortened error fragment; the document was not indexed.

Check your understanding
05 / 05

Preparing the teaching dataset

New course-products examples use this mapping. After creating it, index the document below. This is separate from the trainer’s public 300-product dataset: brand is keyword here, while exercises use brand.keyword. Run administrative operations in your own teaching environment; the trainer remains read-only. Teaching IDs and values are not exercise answers.

The first teaching product

PUT /course-products/_doc/sku-1?refresh=wait_for
{
  "name": "Running headphones",
  "description": "Wireless headphones for running",
  "sku": "sku-1",
  "brand": "Sony",
  "price": 99,
  "rating": 4,
  "in_stock": true,
  "product_id": "sku-1"
}

How to read this result

The first write reports created; a repeated write to the same ID reports updated. Add products to test boundaries and alternatives.

Check your understanding

Check your understanding

Answer before expanding each explanation.

1. How should SKU 0012 be stored?

As keyword when identity and leading zeros matter.

2. What does dynamic=strict do?

Rejects unknown fields in that mapping scope.

3. Are null and an empty string equivalent?

No: an empty string may be indexed.

4. Can keyword be changed to text in place?

An existing field type normally requires a new index and reindex.

5. How do you choose a price type?

Define precision, range and rounding, then choose integer minor units or scaled_float.

Key takeaways

  • As keyword when identity and leading zeros matter.
  • Rejects unknown fields in that mapping scope.
  • No: an empty string may be indexed.
  • An existing field type normally requires a new index and reindex.
  • Define precision, range and rounding, then choose integer minor units or scaled_float.

Official reference

Ready to put this into practice?

Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.