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 1 commit
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 @@ -490,6 +490,16 @@ private static void checkEnabledFieldChange(ObjectMapper mergeWith, Mapper merge
final String path = mergeWith.fullPath() + "." + mergeWithObjectMapper.simpleName() + ".enabled";
throw new MapperException("Can't update attribute for type [" + path + "] in index mapping");
}

if (mergeIntoObjectMapper.nested().isIncludeInParent() != mergeWithObjectMapper.nested().isIncludeInParent()) {
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
final String path = mergeWith.fullPath() + "." + mergeWithObjectMapper.simpleName() + ".include_in_parent";
throw new MapperException("Can't update attribute for type [" + path + "] in index mapping");
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
}

if (mergeIntoObjectMapper.nested().isIncludeInRoot() != mergeWithObjectMapper.nested().isIncludeInRoot()) {
final String path = mergeWith.fullPath() + "." + mergeWithObjectMapper.simpleName() + ".include_in_root";
throw new MapperException("Can't update attribute for type [" + path + "] in index mapping");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.HashSet;
import java.util.function.Function;

import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -742,4 +743,36 @@ 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() {
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
final Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build();
final Mapper.BuilderContext context = new Mapper.BuilderContext(indexSettings, new ContentPath());

ObjectMapper.Nested nested = ObjectMapper.Nested.newNested(false,false);
final ObjectMapper nestedMapper = new ObjectMapper.Builder("nested1").nested(nested).build(context);
final TextFieldMapper.TextFieldType fieldType = new TextFieldMapper.TextFieldType();
final TextFieldMapper fieldMapper = new TextFieldMapper("field1", fieldType, fieldType, -1, null,
indexSettings, FieldMapper.MultiFields.empty(), FieldMapper.CopyTo.empty());
nestedMapper.putMapper(fieldMapper);
final RootObjectMapper rootObjectMapper = new RootObjectMapper.Builder("type1").build(context);
rootObjectMapper.putMapper(nestedMapper);

// cannot update `include_in_parent` dynamically
ObjectMapper.Nested newNested1 = ObjectMapper.Nested.newNested(true,false);
final ObjectMapper newNestedMapper1 = new ObjectMapper.Builder("nested1").nested(newNested1).build(context);
newNestedMapper1.putMapper(fieldMapper);
final RootObjectMapper mergeWith1 = new RootObjectMapper.Builder("type1").build(context);
mergeWith1.putMapper(newNestedMapper1);
MapperException e1 = expectThrows(MapperException.class, () -> rootObjectMapper.merge(mergeWith1));
assertEquals("Can't update attribute for type [type1.nested1.include_in_parent] in index mapping", e1.getMessage());

// cannot update `include_in_root` dynamically
ObjectMapper.Nested newNested2 = ObjectMapper.Nested.newNested(false,true);
final ObjectMapper newNestedMapper2 = new ObjectMapper.Builder("nested1").nested(newNested2).build(context);
newNestedMapper2.putMapper(fieldMapper);
final RootObjectMapper mergeWith2 = new RootObjectMapper.Builder("type1").build(context);
mergeWith2.putMapper(newNestedMapper2);
MapperException e2 = expectThrows(MapperException.class, () -> rootObjectMapper.merge(mergeWith2));
assertEquals("Can't update attribute for type [type1.nested1.include_in_root] in index mapping", e2.getMessage());
}
}