Skip to content

Commit e7cf1e9

Browse files
committed
Remove empty results before merging
We addressed the empty top docs issue with elastic#126385 specifically for scenarios where empty top docs don't go through the wire. Yet they may be serialized from data node back to the coord node, in which case they will no longer be equal to Lucene#EMPTY_TOP_DOCS. This commit expands the existing filtering of empty top docs to include also those that did go through serialization. Closes elastic#126742
1 parent 411a946 commit e7cf1e9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
5555

5656
import java.io.IOException;
57+
import java.lang.reflect.Array;
5758
import java.util.ArrayList;
5859
import java.util.Collection;
5960
import java.util.Collections;
@@ -150,11 +151,11 @@ static TopDocs mergeTopDocs(List<TopDocs> results, int topN, int from) {
150151
return topDocs;
151152
} else if (topDocs instanceof TopFieldGroups firstTopDocs) {
152153
final Sort sort = new Sort(firstTopDocs.fields);
153-
final TopFieldGroups[] shardTopDocs = results.stream().filter(td -> td != Lucene.EMPTY_TOP_DOCS).toArray(TopFieldGroups[]::new);
154+
final TopFieldGroups[] shardTopDocs = removeEmptyResults(results).toArray(TopFieldGroups[]::new);
154155
mergedTopDocs = TopFieldGroups.merge(sort, from, topN, shardTopDocs, false);
155156
} else if (topDocs instanceof TopFieldDocs firstTopDocs) {
156157
final Sort sort = checkSameSortTypes(results, firstTopDocs.fields);
157-
final TopFieldDocs[] shardTopDocs = results.stream().filter((td -> td != Lucene.EMPTY_TOP_DOCS)).toArray(TopFieldDocs[]::new);
158+
final TopFieldDocs[] shardTopDocs = removeEmptyResults(results).toArray(TopFieldDocs[]::new);
158159
mergedTopDocs = TopDocs.merge(sort, from, topN, shardTopDocs);
159160
} else {
160161
final TopDocs[] shardTopDocs = results.toArray(new TopDocs[numShards]);
@@ -163,6 +164,18 @@ static TopDocs mergeTopDocs(List<TopDocs> results, int topN, int from) {
163164
return mergedTopDocs;
164165
}
165166

167+
private static <T extends TopDocs> List<T> removeEmptyResults(List<T> results) {
168+
List<T> nonEmptyResults = new ArrayList<>();
169+
for (T result : results) {
170+
if (result.totalHits.value() > 0 || result.totalHits.relation() != Relation.EQUAL_TO) {
171+
nonEmptyResults.add(result);
172+
} else {
173+
assert result.scoreDocs.length == 0;
174+
}
175+
}
176+
return nonEmptyResults;
177+
}
178+
166179
private static Sort checkSameSortTypes(Collection<TopDocs> results, SortField[] firstSortFields) {
167180
Sort sort = new Sort(firstSortFields);
168181
if (results.size() < 2) return sort;

0 commit comments

Comments
 (0)