Skip to content

Fix merging of _meta field #27352

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

Merged
merged 3 commits into from
Nov 24, 2017
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 @@ -56,7 +56,7 @@ public static class Builder {

private final RootObjectMapper rootObjectMapper;

private Map<String, Object> meta = emptyMap();
private Map<String, Object> meta;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this initialization should stay, for the case meta is not defined in the json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rjernst I was thinking of using null for this case. Or is empty map better?
And which value must be used for unset meta?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with null. I had thought changing this from emtpy map to null would require updating Mapping.toXContent. However, it looks like it currently handles both cases. I would update that to remove the empty case (which means explicitly putting an empty _meta would be serialized, not disappear).


private final Mapper.BuilderContext builderContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public Mapping merge(Mapping mergeWith, boolean updateAllTypes) {
}
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);
}

/**
Expand Down Expand Up @@ -128,7 +129,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,47 @@ public void testMergeAddingParent() throws IOException {
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"));
}
}