Skip to content

Commit 6f3aabb

Browse files
authored
Fix for merging scoreDocs when totalHits are greater than 1 and fieldDocs are 0 (opensearch-project#1295)
1 parent e805cf8 commit 6f3aabb

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/main/java/org/opensearch/neuralsearch/search/query/HybridQueryScoreDocsMerger.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@ class HybridQueryScoreDocsMerger<T extends ScoreDoc> {
3737
* @return merged array of ScoreDocs objects
3838
*/
3939
public T[] merge(final T[] sourceScoreDocs, final T[] newScoreDocs, final Comparator<T> comparator, final boolean isSortEnabled) {
40-
if (Objects.requireNonNull(sourceScoreDocs, "score docs cannot be null").length < MIN_NUMBER_OF_ELEMENTS_IN_SCORE_DOC
41-
|| Objects.requireNonNull(newScoreDocs, "score docs cannot be null").length < MIN_NUMBER_OF_ELEMENTS_IN_SCORE_DOC) {
40+
// The length of sourceScoreDocs or newScoreDocs can be 0 in the following conditions
41+
// 1. When concurrent segment search is enabled then there can be multiple collector instances that can have search results.
42+
// 2. The total hits count of every collector instance represent the actual count of search results present in the shard
43+
// irrespective of pagination.
44+
// 3. If the PagingFieldCollector removes the search results as per `search_after` criteria and totalHits added in the collector is
45+
// greater than 0,
46+
// then the newTopFieldDocs method in the HybridCollectorManager will set the fieldDocs as TopFieldDocs(totalHits, new FieldDoc[0],
47+
// sortFields).
48+
// In this case the size of fieldDocs is 0 with no delimiters.
49+
if (Objects.requireNonNull(sourceScoreDocs, "score docs cannot be null").length == 0) {
50+
return newScoreDocs;
51+
}
52+
if (Objects.requireNonNull(newScoreDocs, "score docs cannot be null").length == 0) {
53+
return sourceScoreDocs;
54+
}
55+
if (sourceScoreDocs.length < MIN_NUMBER_OF_ELEMENTS_IN_SCORE_DOC || newScoreDocs.length < MIN_NUMBER_OF_ELEMENTS_IN_SCORE_DOC) {
4256
throw new IllegalArgumentException("cannot merge top docs because it does not have enough elements");
4357
}
4458
// we overshoot and preallocate more than we need - length of both top docs combined.

0 commit comments

Comments
 (0)