Fix merging of _meta field (#27352)

This commit is contained in:
Alexander Kazakov 2017-11-24 11:44:39 +03:00 committed by Adrien Grand
parent 8916935639
commit 43a91f4184
3 changed files with 47 additions and 3 deletions

View File

@ -56,7 +56,7 @@ public class DocumentMapper implements ToXContentFragment {
private final RootObjectMapper rootObjectMapper; private final RootObjectMapper rootObjectMapper;
private Map<String, Object> meta = emptyMap(); private Map<String, Object> meta;
private final Mapper.BuilderContext builderContext; private final Mapper.BuilderContext builderContext;

View File

@ -98,7 +98,8 @@ public final class Mapping implements ToXContentFragment {
} }
mergedMetaDataMappers.put(merged.getClass(), merged); mergedMetaDataMappers.put(merged.getClass(), merged);
} }
return new Mapping(indexCreated, mergedRoot, mergedMetaDataMappers.values().toArray(new MetadataFieldMapper[0]), mergeWith.meta); Map<String, Object> mergedMeta = mergeWith.meta == null ? meta : mergeWith.meta;
return new Mapping(indexCreated, mergedRoot, mergedMetaDataMappers.values().toArray(new MetadataFieldMapper[0]), mergedMeta);
} }
/** /**
@ -128,7 +129,7 @@ public final class Mapping implements ToXContentFragment {
root.toXContent(builder, params, new ToXContent() { root.toXContent(builder, params, new ToXContent() {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (meta != null && !meta.isEmpty()) { if (meta != null) {
builder.field("_meta", meta); builder.field("_meta", meta);
} }
for (Mapper mapper : metadataMappers) { for (Mapper mapper : metadataMappers) {

View File

@ -289,4 +289,47 @@ public class DocumentMapperMergeTests extends ESSingleNodeTestCase {
Exception e = expectThrows(IllegalArgumentException.class, () -> initMapper.merge(updatedMapper.mapping(), false)); Exception e = expectThrows(IllegalArgumentException.class, () -> initMapper.merge(updatedMapper.mapping(), false));
assertThat(e.getMessage(), containsString("The _parent field's type option can't be changed: [null]->[parent]")); assertThat(e.getMessage(), containsString("The _parent field's type option can't be changed: [null]->[parent]"));
} }
public void testMergeMeta() throws IOException {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
String initMapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("test")
.startObject("_meta")
.field("foo").value("bar")
.endObject()
.endObject()
.endObject()
.string();
DocumentMapper initMapper = parser.parse("test", new CompressedXContent(initMapping));
assertThat(initMapper.meta().get("foo"), equalTo("bar"));
String updateMapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("name").field("type", "text").endObject()
.endObject()
.endObject()
.endObject()
.string();
DocumentMapper updatedMapper = parser.parse("test", new CompressedXContent(updateMapping));
assertThat(initMapper.merge(updatedMapper.mapping(), true).meta().get("foo"), equalTo("bar"));
updateMapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("test")
.startObject("_meta")
.field("foo").value("new_bar")
.endObject()
.endObject()
.endObject()
.string();
updatedMapper = parser.parse("test", new CompressedXContent(updateMapping));
assertThat(initMapper.merge(updatedMapper.mapping(), true).meta().get("foo"), equalTo("new_bar"));
}
} }