Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions docs/reference/mapping/dynamic/templates.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]]");
}
}