Skip to content

Commit 0c95d1a

Browse files
authored
Filter out empty top docs results before merging (#126385)
`Lucene.EMPTY_TOP_DOCS` to identify empty to docs results. These were previously null results, but did not need to be send over transport as incremental reduction was performed only on the data node. Now it can happen that the coord node received a merge result with empty top docs, which has nothing interesting for merging, but that can lead to an exception because the type of the empty array does not match the type of other shards results, for instance if the query was sorted by field. To resolve this, we filter out empty top docs results before merging. Closes #126118
1 parent 62636f9 commit 0c95d1a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

docs/changelog/126385.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 126385
2+
summary: Filter out empty top docs results before merging
3+
area: Search
4+
type: bug
5+
issues:
6+
- 126118

server/src/main/java/org/elasticsearch/action/search/SearchPhaseController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ static TopDocs mergeTopDocs(List<TopDocs> results, int topN, int from) {
150150
return topDocs;
151151
} else if (topDocs instanceof TopFieldGroups firstTopDocs) {
152152
final Sort sort = new Sort(firstTopDocs.fields);
153-
final TopFieldGroups[] shardTopDocs = results.toArray(new TopFieldGroups[0]);
153+
final TopFieldGroups[] shardTopDocs = results.stream().filter(td -> td != Lucene.EMPTY_TOP_DOCS).toArray(TopFieldGroups[]::new);
154154
mergedTopDocs = TopFieldGroups.merge(sort, from, topN, shardTopDocs, false);
155155
} else if (topDocs instanceof TopFieldDocs firstTopDocs) {
156156
final Sort sort = checkSameSortTypes(results, firstTopDocs.fields);
157-
final TopFieldDocs[] shardTopDocs = results.toArray(new TopFieldDocs[0]);
157+
final TopFieldDocs[] shardTopDocs = results.stream().filter((td -> td != Lucene.EMPTY_TOP_DOCS)).toArray(TopFieldDocs[]::new);
158158
mergedTopDocs = TopDocs.merge(sort, from, topN, shardTopDocs);
159159
} else {
160160
final TopDocs[] shardTopDocs = results.toArray(new TopDocs[numShards]);

0 commit comments

Comments
 (0)