Fix merging of _meta field (#27352)
This commit is contained in:
parent
8916935639
commit
43a91f4184
|
@ -56,7 +56,7 @@ public class DocumentMapper implements ToXContentFragment {
|
|||
|
||||
private final RootObjectMapper rootObjectMapper;
|
||||
|
||||
private Map<String, Object> meta = emptyMap();
|
||||
private Map<String, Object> meta;
|
||||
|
||||
private final Mapper.BuilderContext builderContext;
|
||||
|
||||
|
|
|
@ -98,7 +98,8 @@ public final class Mapping implements ToXContentFragment {
|
|||
}
|
||||
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() {
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (meta != null && !meta.isEmpty()) {
|
||||
if (meta != null) {
|
||||
builder.field("_meta", meta);
|
||||
}
|
||||
for (Mapper mapper : metadataMappers) {
|
||||
|
|
|
@ -289,4 +289,47 @@ public class DocumentMapperMergeTests extends ESSingleNodeTestCase {
|
|||
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]"));
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue