@@ -37,8 +37,22 @@ class HybridQueryScoreDocsMerger<T extends ScoreDoc> {
37
37
* @return merged array of ScoreDocs objects
38
38
*/
39
39
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 ) {
42
56
throw new IllegalArgumentException ("cannot merge top docs because it does not have enough elements" );
43
57
}
44
58
// we overshoot and preallocate more than we need - length of both top docs combined.
0 commit comments