Skip to content

Commit b9f711c

Browse files
committed
Disallow multifields on mappers with index-time scripts (elastic#71558)
Multifields are built at the same time as their parent fields, using a positioned xcontent parser to read information. Fields with index time scripts are built entirely differently, and it does not make sense to combine the two. This commit adds a base test to MapperScriptTestCase that ensures a field mapper defined with both multifields and a script parameter throws a parse error.
1 parent 97ce7f9 commit b9f711c

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public Builder(String name, ScriptCompiler scriptCompiler) {
9595
if (s != null && indexed.get() == false && docValues.get() == false) {
9696
throw new MapperParsingException("Cannot define script on field with index:false and doc_values:false");
9797
}
98+
if (s != null && multiFieldsBuilder.hasMultiFields()) {
99+
throw new MapperParsingException("Cannot define multifields on a field with a script");
100+
}
98101
});
99102
}
100103

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ public Builder update(FieldMapper toMerge, ContentPath contentPath) {
427427
return this;
428428
}
429429

430+
public boolean hasMultiFields() {
431+
return mapperBuilders.isEmpty() == false;
432+
}
433+
430434
public MultiFields build(Mapper.Builder mainFieldBuilder, ContentPath contentPath) {
431435
if (mapperBuilders.isEmpty()) {
432436
return empty();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public Builder(String name, NumberType type, ScriptCompiler compiler, boolean ig
115115
if (s != null && indexed.get() == false && hasDocValues.get() == false) {
116116
throw new MapperParsingException("Cannot define script on field with index:false and doc_values:false");
117117
}
118+
if (s != null && multiFieldsBuilder.hasMultiFields()) {
119+
throw new MapperParsingException("Cannot define multifields on a field with a script");
120+
}
118121
});
119122
}
120123

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperScriptTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ public final void testIndexAndDocValuesFalseNotPermitted() {
9595
assertThat(e.getMessage(), containsString("Cannot define script on field with index:false and doc_values:false"));
9696
}
9797

98+
public final void testMultiFieldsNotPermitted() {
99+
Exception e = expectThrows(MapperParsingException.class, () -> createDocumentMapper(fieldMapping(b -> {
100+
b.field("type", type());
101+
b.field("script", "serializer_test");
102+
b.startObject("fields");
103+
b.startObject("subfield").field("type", "keyword").endObject();
104+
b.endObject();
105+
})));
106+
assertThat(e.getMessage(), containsString("Cannot define multifields on a field with a script"));
107+
}
108+
98109
public final void testOnScriptErrorParameterRequiresScript() {
99110
Exception e = expectThrows(MapperParsingException.class, () -> createDocumentMapper(fieldMapping(b -> {
100111
b.field("type", type());

0 commit comments

Comments
 (0)