Skip to content

Commit 0131ec4

Browse files
Fix NPE bug inner_hits (#50709)
When there several subqueries on different relations of the join field, and only one of subqueries is using inner_hits, NPE occurs. This PR prevents NPE error. Closes #50539
1 parent b0ffc60 commit 0131ec4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentChildInnerHitContextBuilder.java

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public TopDocsAndMaxScore[] topDocs(SearchHit[] hits) throws IOException {
127127
.build();
128128
} else {
129129
String parentId = getSortedDocValue(parentIdFieldMapper.name(), context, hit.docId());
130+
if (parentId == null) {
131+
result[i] = new TopDocsAndMaxScore(Lucene.EMPTY_TOP_DOCS, Float.NaN);
132+
continue;
133+
}
130134
q = context.mapperService().fullName(IdFieldMapper.NAME).termQuery(parentId, qsc);
131135
}
132136

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
"Test two sub-queries with only one having inner_hits":
3+
- skip:
4+
version: " - 7.99.99"
5+
reason: "The bug was corrected from 8.0"
6+
7+
- do:
8+
indices.create:
9+
index: test
10+
body:
11+
mappings:
12+
properties:
13+
entity_type: { "type": "keyword" }
14+
join_field: { "type": "join", "relations": { "question": "answer", "person" : "address" } }
15+
settings:
16+
number_of_shards: 1
17+
18+
- do:
19+
index:
20+
index: test
21+
id: 1
22+
body: { "join_field": { "name": "question" }, "entity_type": "question" }
23+
24+
- do:
25+
index:
26+
index: test
27+
id: 2
28+
routing: 1
29+
body: { "join_field": { "name": "answer", "parent": 1} , "entity_type": "answer" }
30+
31+
- do:
32+
index:
33+
index: test
34+
id: 3
35+
body: { "join_field": { "name": "person" }, "entity_type": "person" }
36+
37+
- do:
38+
index:
39+
index: test
40+
routing: 3
41+
id: 4
42+
body: { "join_field": { "name": "address", "parent": 3 }, "entity_type": "address" }
43+
44+
- do:
45+
indices.refresh: {}
46+
47+
- do:
48+
search:
49+
index: test
50+
body:
51+
query:
52+
bool:
53+
should:
54+
- term:
55+
entity_type: person
56+
- has_parent:
57+
parent_type: question
58+
query:
59+
match_all: {}
60+
inner_hits: {}
61+
62+
63+
- match: { hits.total.value: 2 }
64+
- match: { hits.hits.0._id: "3" }
65+
- match: { hits.hits.0.inner_hits.question.hits.total.value: 0}
66+
- match: { hits.hits.1._id: "2" }
67+
- match: { hits.hits.1.inner_hits.question.hits.total.value: 1}
68+
- match: { hits.hits.1.inner_hits.question.hits.hits.0._id: "1"}

0 commit comments

Comments
 (0)