Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating include_in_parent/include_in_root of nested field. #54386

Merged
merged 7 commits into from
Apr 16, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,7 @@ protected void doMerge(final ObjectMapper mergeWith) {
this.dynamic = mergeWith.dynamic;
}

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

for (Mapper mergeWithMapper : mergeWith) {
Mapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName());
Expand All @@ -484,6 +482,22 @@ protected void doMerge(final ObjectMapper mergeWith) {
}
}

private void checkObjectMapperParameters(final ObjectMapper mergeWith) {
if (isEnabled() != mergeWith.isEnabled()) {
throw new MapperException("The [enabled] parameter can't be updated for the object mapping [" + name() + "].");
}

if (nested().isIncludeInParent() != mergeWith.nested().isIncludeInParent()) {
throw new MapperException("The [include_in_parent] parameter can't be updated for the nested object mapping [" +
name() + "].");
}

if (nested().isIncludeInRoot() != mergeWith.nested().isIncludeInRoot()) {
throw new MapperException("The [include_in_root] parameter can't be updated for the nested object mapping [" +
name() + "].");
}
}

@Override
public ObjectMapper updateFieldType(Map<String, MappedFieldType> fullNameToFieldType) {
List<Mapper> updatedMappers = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,31 @@ public void testReorderParent() throws IOException {
assertThat(doc.docs().get(1).get("nested1.field2"), equalTo("4"));
assertThat(doc.docs().get(2).get("field"), equalTo("value"));
}

public void testMergeNestedMappings() throws IOException {
MapperService mapperService = createIndex("index1", Settings.EMPTY, jsonBuilder().startObject()
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
.startObject("properties")
.startObject("nested1")
.field("type", "nested")
.endObject()
.endObject().endObject()).mapperService();

String mapping1 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").field("include_in_parent", true)
.endObject().endObject().endObject().endObject());

// cannot update `include_in_parent` dynamically
MapperException e1 = expectThrows(MapperException.class, () -> mapperService.merge("type",
new CompressedXContent(mapping1), MergeReason.MAPPING_UPDATE));
assertEquals("The [include_in_parent] parameter can't be updated for the nested object mapping [nested1].", e1.getMessage());

String mapping2 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").field("include_in_root", true)
.endObject().endObject().endObject().endObject());

// cannot update `include_in_root` dynamically
MapperException e2 = expectThrows(MapperException.class, () -> mapperService.merge("type",
new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE));
assertEquals("The [include_in_root] parameter can't be updated for the nested object mapping [nested1].", e2.getMessage());
}
}