Skip to content

Commit 4482f73

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 338a538 commit 4482f73

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public TopDocs[] topDocs(SearchHit[] hits) throws IOException {
156156
.build();
157157
} else {
158158
String parentId = getSortedDocValue(parentIdFieldMapper.name(), context, hit.docId());
159+
if (parentId == null) {
160+
result[i] = Lucene.EMPTY_TOP_DOCS;
161+
continue;
162+
}
159163
q = context.mapperService().fullName(IdFieldMapper.NAME).termQuery(parentId, qsc);
160164
}
161165

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

0 commit comments

Comments
 (0)