Skip to content

keep _parent field while updating child type mapping #24407

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
Jun 7, 2017
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 @@ -297,7 +297,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
super.doMerge(mergeWith, updateAllTypes);
ParentFieldMapper fieldMergeWith = (ParentFieldMapper) mergeWith;
if (Objects.equals(parentType, fieldMergeWith.parentType) == false) {
if (fieldMergeWith.parentType != null && Objects.equals(parentType, fieldMergeWith.parentType) == false) {
throw new IllegalArgumentException("The _parent field's type option can't be changed: [" + parentType + "]->[" + fieldMergeWith.parentType + "]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public void run() {
barrier.await();
for (int i = 0; i < 200 && stopped.get() == false; i++) {
final String fieldName = Integer.toString(i);
ParsedDocument doc = documentMapper.parse(SourceToParse.source("test",
"test",
fieldName,
ParsedDocument doc = documentMapper.parse(SourceToParse.source("test",
"test",
fieldName,
new BytesArray("{ \"" + fieldName + "\" : \"test\" }"),
XContentType.JSON));
Mapping update = doc.dynamicMappingsUpdate();
Expand All @@ -191,10 +191,10 @@ public void run() {
while(stopped.get() == false) {
final String fieldName = lastIntroducedFieldName.get();
final BytesReference source = new BytesArray("{ \"" + fieldName + "\" : \"test\" }");
ParsedDocument parsedDoc = documentMapper.parse(SourceToParse.source("test",
"test",
"random",
source,
ParsedDocument parsedDoc = documentMapper.parse(SourceToParse.source("test",
"test",
"random",
source,
XContentType.JSON));
if (parsedDoc.dynamicMappingsUpdate() != null) {
// not in the mapping yet, try again
Expand Down Expand Up @@ -235,4 +235,33 @@ public void testDoNotRepeatOriginalMapping() throws IOException {
assertNotNull(mapper.mappers().getMapper("foo"));
assertFalse(mapper.sourceMapper().enabled());
}

public void testMergeChildType() throws IOException {
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();

String initMapping = XContentFactory.jsonBuilder().startObject().startObject("child")
.startObject("_parent").field("type", "parent").endObject()
.endObject().endObject().string();
DocumentMapper initMapper = parser.parse("child", new CompressedXContent(initMapping));

String updatedMapping = XContentFactory.jsonBuilder().startObject().startObject("child")
.startObject("properties")
.startObject("name").field("type", "text").endObject()
.endObject().endObject().endObject().string();
DocumentMapper updatedMapper = parser.parse("child", new CompressedXContent(updatedMapping));
DocumentMapper mergedMapper = initMapper.merge(updatedMapper.mapping(), false);

Copy link
Member

Choose a reason for hiding this comment

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

Maybe also add a merge with current parent field and a new field?

String updatedMapping = XContentFactory.jsonBuilder().startObject().startObject("child")
   .startObject("_parent").field("type", "parent").endObject()
   .startObject("properties")
   .startObject("field2").field("type", "text").endObject()
   .endObject().endObject().endObject().string();
 DocumentMapper updatedMapper = parser.parse("child", new CompressedXContent(updatedMapping));
 DocumentMapper mergedMapper = initMapper.merge(updatedMapper.mapping(), false);

assertThat(mergedMapper.mappers().getMapper("_parent"), notNullValue());
assertThat(mergedMapper.mappers().getMapper("name"), notNullValue());

String modParentMapping = XContentFactory.jsonBuilder().startObject().startObject("child")
.startObject("_parent").field("type", "new_parent").endObject()
.endObject().endObject().string();
DocumentMapper modParentMapper = parser.parse("child", new CompressedXContent(modParentMapping));
try {
Copy link
Member

Choose a reason for hiding this comment

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

Replace this try-catch block with an expectThrows(...) call?

Exception e = expectThrows(IllegalArgumentException.class, () ->  initMapper.merge(modParentMapper.mapping(), false))
assertThat(e.getMessage(), containsString("The _parent field's type option can't be changed:"));

initMapper.merge(modParentMapper.mapping(), false);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("The _parent field's type option can't be changed:"));
}
}
}