diff --git a/docs/reference/mapping/dynamic/templates.asciidoc b/docs/reference/mapping/dynamic/templates.asciidoc index 9107b5d76db13..7f13007bc61f1 100644 --- a/docs/reference/mapping/dynamic/templates.asciidoc +++ b/docs/reference/mapping/dynamic/templates.asciidoc @@ -38,15 +38,16 @@ Dynamic templates are specified as an array of named objects: <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 +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 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. It is recommended to configure the `match_mapping_type` to the expected JSON type or + configure the desired `type` in the mapping snippet. * 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 diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java index e2d4b197bb741..188c63c32dee9 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; @@ -408,14 +409,16 @@ private static void validateDynamicTemplate(Mapper.TypeParser.ParserContext pars final boolean failInvalidDynamicTemplates = parserContext.indexVersionCreated().onOrAfter(Version.V_8_0_0); if (dynamicTemplateInvalid) { - String message = String.format(Locale.ROOT, "dynamic template [%s] has invalid content [%s]", - dynamicTemplate.getName(), Strings.toString(dynamicTemplate)); + String format = "dynamic template [%s] has invalid content [%s], " + + "attempted to validate it with the following match_mapping_type: [%s]"; + String message = String.format(Locale.ROOT, format, dynamicTemplate.getName(), Strings.toString(dynamicTemplate), + Arrays.toString(types)); if (failInvalidDynamicTemplates) { throw new IllegalArgumentException(message, lastError); } else { final String deprecationMessage; if (lastError != null) { - deprecationMessage = String.format(Locale.ROOT, "%s, caused by [%s]", message, lastError.getMessage()); + deprecationMessage = String.format(Locale.ROOT, "%s, last error: [%s]", message, lastError.getMessage()); } else { deprecationMessage = message; } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java index 3e52680c74679..01e078e70364d 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java @@ -472,6 +472,7 @@ public void testIllegalDynamicTemplate7DotXIndex() throws Exception { DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE); assertThat(mapper.mappingSource().toString(), containsString("\"type\":\"string\"")); assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":" + - "\"string\"}}], caused by [No mapper found for type [string]]"); + "\"string\"}}], attempted to validate it with the following match_mapping_type: [[string]], " + + "last error: [No mapper found for type [string]]"); } }