Skip to content

Commit 5c66caf

Browse files
authored
Fix updating include_in_parent/include_in_root of nested field throws… (#54386)
The main changes are: 1. Throw an error when updating `include_in_parent` or `include_in_root` attribute of nested field dynamically by the PUT mapping API. 2. Add a test for the change. Closes #53792
1 parent d1394f8 commit 5c66caf

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

+17-3
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,7 @@ protected void doMerge(final ObjectMapper mergeWith) {
466466
this.dynamic = mergeWith.dynamic;
467467
}
468468

469-
if (isEnabled() != mergeWith.isEnabled()) {
470-
throw new MapperException("The [enabled] parameter can't be updated for the object mapping [" + name() + "].");
471-
}
469+
checkObjectMapperParameters(mergeWith);
472470

473471
for (Mapper mergeWithMapper : mergeWith) {
474472
Mapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName());
@@ -485,6 +483,22 @@ protected void doMerge(final ObjectMapper mergeWith) {
485483
}
486484
}
487485

486+
private void checkObjectMapperParameters(final ObjectMapper mergeWith) {
487+
if (isEnabled() != mergeWith.isEnabled()) {
488+
throw new MapperException("The [enabled] parameter can't be updated for the object mapping [" + name() + "].");
489+
}
490+
491+
if (nested().isIncludeInParent() != mergeWith.nested().isIncludeInParent()) {
492+
throw new MapperException("The [include_in_parent] parameter can't be updated for the nested object mapping [" +
493+
name() + "].");
494+
}
495+
496+
if (nested().isIncludeInRoot() != mergeWith.nested().isIncludeInRoot()) {
497+
throw new MapperException("The [include_in_root] parameter can't be updated for the nested object mapping [" +
498+
name() + "].");
499+
}
500+
}
501+
488502
@Override
489503
public ObjectMapper updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) {
490504
List<Mapper> updatedMappers = null;

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

+27
Original file line numberDiff line numberDiff line change
@@ -742,4 +742,31 @@ public void testReorderParent() throws IOException {
742742
assertThat(doc.docs().get(1).get("nested1.field2"), equalTo("4"));
743743
assertThat(doc.docs().get(2).get("field"), equalTo("value"));
744744
}
745+
746+
public void testMergeNestedMappings() throws IOException {
747+
MapperService mapperService = createIndex("index1", Settings.EMPTY, jsonBuilder().startObject()
748+
.startObject("properties")
749+
.startObject("nested1")
750+
.field("type", "nested")
751+
.endObject()
752+
.endObject().endObject()).mapperService();
753+
754+
String mapping1 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
755+
.startObject("nested1").field("type", "nested").field("include_in_parent", true)
756+
.endObject().endObject().endObject().endObject());
757+
758+
// cannot update `include_in_parent` dynamically
759+
MapperException e1 = expectThrows(MapperException.class, () -> mapperService.merge("type",
760+
new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE));
761+
assertEquals("The [include_in_parent] parameter can't be updated for the nested object mapping [nested1].", e1.getMessage());
762+
763+
String mapping2 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
764+
.startObject("nested1").field("type", "nested").field("include_in_root", true)
765+
.endObject().endObject().endObject().endObject());
766+
767+
// cannot update `include_in_root` dynamically
768+
MapperException e2 = expectThrows(MapperException.class, () -> mapperService.merge("type",
769+
new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE));
770+
assertEquals("The [include_in_root] parameter can't be updated for the nested object mapping [nested1].", e2.getMessage());
771+
}
745772
}

0 commit comments

Comments
 (0)