Elasticsearch Mastering Dynamic Templates in Elasticsearch

By Opster Team

Updated: Nov 5, 2023

| 2 min read

Quick links

Overview

Dynamic templates in Elasticsearch are a powerful tool that allows you to define custom mappings that can be applied to dynamically added fields. They provide a flexible way to control how Elasticsearch will treat new fields that are not yet defined in the mapping.

Dynamic templates are defined within the mappings type and can be specified at the index creation time or can be updated later using the PUT mapping API. They are particularly useful when you have a large number of fields with similar names or types, and you want to apply the same mapping to all of them.

Let’s delve into the details of how to use dynamic templates in Elasticsearch.

Creating Dynamic Templates

To create a dynamic template, you need to use the “dynamic_templates” section in your mapping. Here is an example:

json
PUT /my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      }
    ]
  }
}

In this example, any new field that is of type “long” will be mapped as an “integer”. The “match_mapping_type” parameter is used to match the data type of the new field.

Matching Field Names

You can also match field names using the “match” or “unmatch” parameters. For example:

json
PUT /my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keyword": {
          "match_mapping_type": "string",
          "match": "*_keyword",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}

In this example, any new string field that ends with “_keyword” will be mapped as a “keyword”.

Using Dynamic Templates with Multi-fields

Dynamic templates can also be used with multi-fields. For example:

json
PUT /my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          }
        }
      }
    ]
  }
}

In this example, any new string field will be mapped as a “text” field with a multi-field of type “keyword”.

Order of Dynamic Templates

The order of dynamic templates matters. Elasticsearch will apply the first matching template in the list. If no templates match, Elasticsearch will use its default rules for dynamic mapping.

Disabling Dynamic Mapping

If you want to disable dynamic mapping for specific fields, you can use the “dynamic” parameter in your mapping:

json
PUT /my_index
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "user": {
        "properties": {
          "name": {
            "type": "text"
          },
          "role": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

In this example, if a document is indexed with an extra field, Elasticsearch will throw an exception because the “dynamic” parameter is set to “strict”. If you want to still index your document but without throwing an error, you can set “dynamic” to “false”. In this case, the document will be indexed, but the fields not explicitly declared in your mapping will not be indexed, and hence, not be searchable.

Conclusion

Dynamic templates in Elasticsearch provide a powerful and flexible way to control how Elasticsearch handles dynamically added fields. By understanding and using dynamic templates, you can ensure that your data is indexed and searched in the most efficient way possible. If you want to learn about the exception: dynamic template must be defined with a name, check out this guide.