Skip to content

Commit e279e60

Browse files
committed
Filter out empty top docs results before merging
`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 cc54010 commit e279e60

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

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)