inner_hits: Skip adding a parent field to nested documents.
Otherwise an empty string get added as _parent field. Closes #21503
This commit is contained in:
parent
c7bd4f3454
commit
8a3a885058
|
@ -47,6 +47,11 @@ public final class ParentFieldSubFetchPhase implements FetchSubPhase {
|
|||
}
|
||||
|
||||
String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId());
|
||||
if (parentId == null) {
|
||||
// hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document.
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull();
|
||||
if (fields == null) {
|
||||
fields = new HashMap<>();
|
||||
|
@ -59,8 +64,7 @@ public final class ParentFieldSubFetchPhase implements FetchSubPhase {
|
|||
try {
|
||||
SortedDocValues docValues = reader.getSortedDocValues(fieldMapper.name());
|
||||
BytesRef parentId = docValues.get(docId);
|
||||
assert parentId.length > 0;
|
||||
return parentId.utf8ToString();
|
||||
return parentId.length > 0 ? parentId.utf8ToString() : null;
|
||||
} catch (IOException e) {
|
||||
throw ExceptionsHelper.convertToElastic(e);
|
||||
}
|
||||
|
|
|
@ -995,4 +995,21 @@ public class InnerHitsIT extends ESIntegTestCase {
|
|||
equalTo("fox ate rabbit x y z"));
|
||||
}
|
||||
|
||||
public void testNestedInnerHitWrappedInParentChildInnerhit() throws Exception {
|
||||
assertAcked(prepareCreate("test").addMapping("child_type", "_parent", "type=parent_type", "nested_type", "type=nested"));
|
||||
client().prepareIndex("test", "parent_type", "1").setSource("key", "value").get();
|
||||
client().prepareIndex("test", "child_type", "2").setParent("1").setSource("nested_type", Collections.singletonMap("key", "value"))
|
||||
.get();
|
||||
refresh();
|
||||
SearchResponse response = client().prepareSearch("test")
|
||||
.setQuery(boolQuery().must(matchQuery("key", "value"))
|
||||
.should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None)
|
||||
.innerHit(new InnerHitBuilder()), ScoreMode.None).innerHit(new InnerHitBuilder())))
|
||||
.get();
|
||||
assertHitCount(response, 1);
|
||||
SearchHit hit = response.getHits().getAt(0);
|
||||
assertThat(hit.getInnerHits().get("child_type").getAt(0).field("_parent").getValue(), equalTo("1"));
|
||||
assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue