AI Hints
Home›Elasticsearch Tutorial›Templates and Strict Mappings
Elasticsearch · Lesson 50 of 120

Strict Mappings

No surprise fields.

⏱ 2 min read▶ Video at 46:37🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

A bug in the importer sends prise instead of price. With dynamic mapping, Elasticsearch happily adds a new field called prise. You won't notice until searches start missing data.

  • Typo: prise
  • New field appears
  • Silent bug

dynamic: strict

Setting dynamic to strict says, reject any field that isn't in the mapping. The first document is fine. The second has the typo. Run it. The typo is rejected with a clear error, instead of a silent new field.

Dev Tools Consoletext
PUT products
{ "mappings": { "dynamic": "strict", "properties": {
    "name":  { "type": "text" },
    "price": { "type": "float" } } } }

POST products/_doc?filter_path=result
{ "name": "Pocket Camping Stove", "price": 49.99 }

POST products/_doc
{ "name": "Pocket Camping Stove", "prise": 49.99 }
Output
# PUT products 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

# POST products/_doc 201 Created
{"result": "created"}

# POST products/_doc 400 Bad Request
{
  "error": {
    "type": "strict_dynamic_mapping_exception",
    "reason": "[1:44] mapping set to strict, dynamic introduction of [prise] within
    [_doc] is not allowed"
  },
  "status": 400
}

dynamic settings

True is the default, and adds new fields. Runtime adds them as runtime fields, which aren't indexed. False keeps unknown fields in the source, but doesn't index them. And strict rejects the document.

keyvalue
trueadd new fields (default)
runtimeadd as runtime fields
falsekeep in _source, don't index
strictreject the document

Summary

Dynamic strict rejects unknown fields, so typos fail loudly. For important data, use strict. Next, practise designing the catalog mapping.

Key takeaways
  • dynamic: strict
  • Typos fail
  • Loud errors
Next up: Practice: Design the Catalog Mapping — every field, the right type