<commit_before>[[dynamic-templates]]
=== Dynamic templates

Dynamic templates allow you to define custom mappings that can be applied to
dynamically added fields based on:

* the <<dynamic-mapping,data type>> detected by Elasticsearch, with <<match-mapping-type,`match_mapping_type`>>.
* the name of the field, with <<match-unmatch,`match` and `unmatch`>> or <<match-pattern,`match_pattern`>>.
* the full dotted path to the field, with <<path-match-unmatch,`path_match` and `path_unmatch`>>.

The original field name `{name}` and the detected data type
`{dynamic_type}` <<template-variables,template variables>> can be used in
the mapping specification as placeholders.

IMPORTANT: Dynamic field mappings are only added when a field contains a
concrete value -- not `null` or an empty array. This means that if the
`null_value` option is used in a `dynamic_template`, it will only be applied
after the first document with a concrete value for the field has been
indexed.

Dynamic templates are specified as an array of named objects:

[source,js]
--------------------------------------------------
  "dynamic_templates": [
    {
      "my_template_name": { <1>
        ...  match conditions ... <2>
        "mapping": { ... } <3>
      }
    },
    ...
  ]
--------------------------------------------------
// NOTCONSOLE
<1> The template name can be any string value.
<2> The match conditions can include any of : `match_mapping_type`, `match`, `match_pattern`, `unmatch`, `path_match`, `path_unmatch`.
<3> The mapping that the matched field should use.

If a provided mapping contains an invalid mapping snippet, a validation error
is returned. Validation occurs when applying the dynamic template at index time, 
and, in most cases, when the dynamic template is updated. Providing an invalid mapping 
snippet may cause the update or validation of a dynamic template to fail under certain conditions:

* If no `match_mapping_type` has been specified but the template is valid for at least one predefined mapping type, 
  the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching 
  the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type` 
  is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation 
  error is returned at index time.

* If the `{{name}}` placeholder is used in the mapping snippet, validation is skipped when updating the dynamic 
  template. This is because the field name is unknown at that time. Instead, validation occurs when the template is applied
  at index time.

Templates are processed in order -- the first matching template wins. When<commit_msg>Fix mistake in notes around dynamic template validation. (#60726)<commit_after>[[dynamic-templates]]
=== Dynamic templates

Dynamic templates allow you to define custom mappings that can be applied to
dynamically added fields based on:

* the <<dynamic-mapping,data type>> detected by Elasticsearch, with <<match-mapping-type,`match_mapping_type`>>.
* the name of the field, with <<match-unmatch,`match` and `unmatch`>> or <<match-pattern,`match_pattern`>>.
* the full dotted path to the field, with <<path-match-unmatch,`path_match` and `path_unmatch`>>.

The original field name `{name}` and the detected data type
`{dynamic_type}` <<template-variables,template variables>> can be used in
the mapping specification as placeholders.

IMPORTANT: Dynamic field mappings are only added when a field contains a
concrete value -- not `null` or an empty array. This means that if the
`null_value` option is used in a `dynamic_template`, it will only be applied
after the first document with a concrete value for the field has been
indexed.

Dynamic templates are specified as an array of named objects:

[source,js]
--------------------------------------------------
  "dynamic_templates": [
    {
      "my_template_name": { <1>
        ...  match conditions ... <2>
        "mapping": { ... } <3>
      }
    },
    ...
  ]
--------------------------------------------------
// NOTCONSOLE
<1> The template name can be any string value.
<2> The match conditions can include any of : `match_mapping_type`, `match`, `match_pattern`, `unmatch`, `path_match`, `path_unmatch`.
<3> The mapping that the matched field should use.

If a provided mapping contains an invalid mapping snippet, a validation error
is returned. Validation occurs when applying the dynamic template at index time, 
and, in most cases, when the dynamic template is updated. Providing an invalid mapping 
snippet may cause the update or validation of a dynamic template to fail under certain conditions:

* If no `match_mapping_type` has been specified but the template is valid for at least one predefined mapping type, 
  the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching 
  the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type` 
  is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation 
  error is returned at index time.

* If the `{name}` placeholder is used in the mapping snippet, validation is skipped when updating the dynamic
  template. This is because the field name is unknown at that time. Instead, validation occurs when the template is applied
  at index time.

Templates are processed in order -- the first matching template wins. When
{'fraction_non_alphanumeric': 0.10681532098543428, 'fraction_numerical': 0.003057004135946772, 'mean_word_length': 3.57025472473295, 'pattern_counts': {'":': 6, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011579', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1361, 'n_words': 731}