@@ -570,6 +570,72 @@ public void testMergeNoResponsesAdded() {
570570 assertEquals (0 , response .getShardFailures ().length );
571571 }
572572
573+ public void testMergeEmptySearchHitsWithNonEmpty () {
574+ long currentRelativeTime = randomLong ();
575+ final SearchTimeProvider timeProvider = new SearchTimeProvider (randomLong (), 0 , () -> currentRelativeTime );
576+ SearchResponseMerger merger = new SearchResponseMerger (0 , 10 , Integer .MAX_VALUE , timeProvider , flag -> null );
577+ SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
578+ int numFields = randomIntBetween (1 , 3 );
579+ SortField [] sortFields = new SortField [numFields ];
580+ for (int i = 0 ; i < numFields ; i ++) {
581+ sortFields [i ] = new SortField ("field-" + i , SortField .Type .INT , randomBoolean ());
582+ }
583+ PriorityQueue <SearchHit > priorityQueue = new PriorityQueue <>(new SearchHitComparator (sortFields ));
584+ SearchHit [] hits = randomSearchHitArray (10 , 1 , "remote" , new Index []{new Index ("index" , "uuid" )}, Float .NaN , 1 ,
585+ sortFields , priorityQueue );
586+ {
587+ SearchHits searchHits = new SearchHits (hits , new TotalHits (10 , TotalHits .Relation .EQUAL_TO ), Float .NaN , sortFields , null , null );
588+ InternalSearchResponse response = new InternalSearchResponse (searchHits , null , null , null , false , false , 1 );
589+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
590+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
591+ merger .add (searchResponse );
592+ }
593+ {
594+ SearchHits empty = new SearchHits (new SearchHit [0 ], new TotalHits (0 , TotalHits .Relation .EQUAL_TO ), Float .NaN , null , null , null );
595+ InternalSearchResponse response = new InternalSearchResponse (empty , null , null , null , false , false , 1 );
596+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
597+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
598+ merger .add (searchResponse );
599+ }
600+ assertEquals (2 , merger .numResponses ());
601+ SearchResponse mergedResponse = merger .getMergedResponse (clusters );
602+ assertEquals (10 , mergedResponse .getHits ().getTotalHits ().value );
603+ assertEquals (10 , mergedResponse .getHits ().getHits ().length );
604+ assertEquals (2 , mergedResponse .getTotalShards ());
605+ assertEquals (2 , mergedResponse .getSuccessfulShards ());
606+ assertEquals (0 , mergedResponse .getSkippedShards ());
607+ assertArrayEquals (sortFields , mergedResponse .getHits ().getSortFields ());
608+ assertArrayEquals (hits , mergedResponse .getHits ().getHits ());
609+ assertEquals (clusters , mergedResponse .getClusters ());
610+ }
611+
612+ public void testMergeOnlyEmptyHits () {
613+ long currentRelativeTime = randomLong ();
614+ final SearchTimeProvider timeProvider = new SearchTimeProvider (randomLong (), 0 , () -> currentRelativeTime );
615+ SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
616+ Tuple <Integer , TotalHits .Relation > randomTrackTotalHits = randomTrackTotalHits ();
617+ int trackTotalHitsUpTo = randomTrackTotalHits .v1 ();
618+ TotalHits .Relation totalHitsRelation = randomTrackTotalHits .v2 ();
619+ SearchResponseMerger merger = new SearchResponseMerger (0 , 10 , trackTotalHitsUpTo , timeProvider , flag -> null );
620+ int numResponses = randomIntBetween (1 , 5 );
621+ TotalHits expectedTotalHits = null ;
622+ for (int i = 0 ; i < numResponses ; i ++) {
623+ TotalHits totalHits = null ;
624+ if (trackTotalHitsUpTo != SearchContext .TRACK_TOTAL_HITS_DISABLED ) {
625+ totalHits = new TotalHits (randomLongBetween (0 , 1000 ), totalHitsRelation );
626+ long previousValue = expectedTotalHits == null ? 0 : expectedTotalHits .value ;
627+ expectedTotalHits = new TotalHits (Math .min (previousValue + totalHits .value , trackTotalHitsUpTo ), totalHitsRelation );
628+ }
629+ SearchHits empty = new SearchHits (new SearchHit [0 ], totalHits , Float .NaN , null , null , null );
630+ InternalSearchResponse response = new InternalSearchResponse (empty , null , null , null , false , false , 1 );
631+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
632+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
633+ merger .add (searchResponse );
634+ }
635+ SearchResponse mergedResponse = merger .getMergedResponse (clusters );
636+ assertEquals (expectedTotalHits , mergedResponse .getHits ().getTotalHits ());
637+ }
638+
573639 private static Tuple <Integer , TotalHits .Relation > randomTrackTotalHits () {
574640 switch (randomIntBetween (0 , 2 )) {
575641 case 0 :
0 commit comments