Skip to content

Commit fc60b91

Browse files
committed
Improve error message in case of invalid dynamic templates.
Backporting elastic#60870 to 7.x branch. Include the attempted 'match_mapping_type' into the message, so that it is clearer that multiple validation attempts have occurred. Dynamic template validation was recently added via elastic#51233 and there was some confusion over the deprecation message itself. (in 7.x only deprecation warning will be omitted and from 8.0 an error will be returned)
1 parent 2da6ccb commit fc60b91

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

docs/reference/mapping/dynamic/templates.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ snippet may cause the update or validation of a dynamic template to fail under c
4646
the mapping snippet is considered valid. However, a validation error is returned at index time if a field matching
4747
the template is indexed as a different type. For example, configuring a dynamic template with no `match_mapping_type`
4848
is considered valid as string type, but if a field matching the dynamic template is indexed as a long, a validation
49-
error is returned at index time.
49+
error is returned at index time. It is recommended to configure the `match_mapping_type` to the expected JSON type or
50+
configure the desired `type` in the mapping snippet.
5051

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

server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import java.io.IOException;
3333
import java.util.ArrayList;
34+
import java.util.Arrays;
3435
import java.util.Collection;
3536
import java.util.Collections;
3637
import java.util.Iterator;
@@ -405,8 +406,10 @@ private static void validateDynamicTemplate(Mapper.TypeParser.ParserContext pars
405406

406407
final boolean shouldEmitDeprecationWarning = parserContext.indexVersionCreated().onOrAfter(Version.V_7_7_0);
407408
if (dynamicTemplateInvalid && shouldEmitDeprecationWarning) {
408-
String message = String.format(Locale.ROOT, "dynamic template [%s] has invalid content [%s]",
409-
dynamicTemplate.getName(), Strings.toString(dynamicTemplate));
409+
String format = "dynamic template [%s] has invalid content [%s], " +
410+
"attempted to validate it with the following match_mapping_type: [%s]";
411+
String message = String.format(Locale.ROOT, format, dynamicTemplate.getName(), Strings.toString(dynamicTemplate),
412+
Arrays.toString(types));
410413

411414
final String deprecationMessage;
412415
if (lastError != null) {

server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ public void testIllegalDynamicTemplateUnknownFieldType() throws Exception {
311311
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE);
312312
assertThat(mapper.mappingSource().toString(), containsString("\"type\":\"string\""));
313313
assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":" +
314-
"\"string\"}}], caused by [No mapper found for type [string]]");
314+
"\"string\"}}], attempted to validate it with the following match_mapping_type: [[string]], " +
315+
"caused by [No mapper found for type [string]]");
315316
}
316317

317318
public void testIllegalDynamicTemplateUnknownAttribute() throws Exception {
@@ -340,6 +341,7 @@ public void testIllegalDynamicTemplateUnknownAttribute() throws Exception {
340341
assertThat(mapper.mappingSource().toString(), containsString("\"foo\":\"bar\""));
341342
assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{" +
342343
"\"foo\":\"bar\",\"type\":\"keyword\"}}], " +
344+
"attempted to validate it with the following match_mapping_type: [[string]], " +
343345
"caused by [unknown parameter [foo] on mapper [__dynamic__my_template] of type [keyword]]");
344346
}
345347

@@ -368,7 +370,8 @@ public void testIllegalDynamicTemplateInvalidAttribute() throws Exception {
368370
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(Strings.toString(mapping)), MergeReason.MAPPING_UPDATE);
369371
assertThat(mapper.mappingSource().toString(), containsString("\"analyzer\":\"foobar\""));
370372
assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{" +
371-
"\"analyzer\":\"foobar\",\"type\":\"text\"}}], caused by [analyzer [foobar] has not been configured in mappings]");
373+
"\"analyzer\":\"foobar\",\"type\":\"text\"}}], attempted to validate it with the following match_mapping_type: [[string]], " +
374+
"caused by [analyzer [foobar] has not been configured in mappings]");
372375
}
373376

374377
public void testIllegalDynamicTemplateNoMappingType() throws Exception {
@@ -437,10 +440,14 @@ public void testIllegalDynamicTemplateNoMappingType() throws Exception {
437440
if (useMatchMappingType) {
438441
assertWarnings("dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"*\",\"mapping\":{" +
439442
"\"foo\":\"bar\",\"type\":\"{dynamic_type}\"}}], " +
443+
"attempted to validate it with the following match_mapping_type: " +
444+
"[[object, string, long, double, boolean, date, binary]], " +
440445
"caused by [unknown parameter [foo] on mapper [__dynamic__my_template] of type [binary]]");
441446
} else {
442447
assertWarnings("dynamic template [my_template] has invalid content [{\"match\":\"string_*\",\"mapping\":{" +
443448
"\"foo\":\"bar\",\"type\":\"{dynamic_type}\"}}], " +
449+
"attempted to validate it with the following match_mapping_type: " +
450+
"[[object, string, long, double, boolean, date, binary]], " +
444451
"caused by [unknown parameter [foo] on mapper [__dynamic__my_template] of type [binary]]");
445452
}
446453
}

0 commit comments

Comments
 (0)