@@ -504,6 +504,72 @@ public void testMergeNoResponsesAdded() {
504
504
assertEquals (0 , response .getShardFailures ().length );
505
505
}
506
506
507
+ public void testMergeEmptySearchHitsWithNonEmpty () {
508
+ long currentRelativeTime = randomLong ();
509
+ final SearchTimeProvider timeProvider = new SearchTimeProvider (randomLong (), 0 , () -> currentRelativeTime );
510
+ SearchResponseMerger merger = new SearchResponseMerger (0 , 10 , Integer .MAX_VALUE , timeProvider , flag -> null );
511
+ SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
512
+ int numFields = randomIntBetween (1 , 3 );
513
+ SortField [] sortFields = new SortField [numFields ];
514
+ for (int i = 0 ; i < numFields ; i ++) {
515
+ sortFields [i ] = new SortField ("field-" + i , SortField .Type .INT , randomBoolean ());
516
+ }
517
+ PriorityQueue <SearchHit > priorityQueue = new PriorityQueue <>(new SearchHitComparator (sortFields ));
518
+ SearchHit [] hits = randomSearchHitArray (10 , 1 , "remote" , new Index []{new Index ("index" , "uuid" )}, Float .NaN , 1 ,
519
+ sortFields , priorityQueue );
520
+ {
521
+ SearchHits searchHits = new SearchHits (hits , new TotalHits (10 , TotalHits .Relation .EQUAL_TO ), Float .NaN , sortFields , null , null );
522
+ InternalSearchResponse response = new InternalSearchResponse (searchHits , null , null , null , false , false , 1 );
523
+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
524
+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
525
+ merger .add (searchResponse );
526
+ }
527
+ {
528
+ SearchHits empty = new SearchHits (new SearchHit [0 ], new TotalHits (0 , TotalHits .Relation .EQUAL_TO ), Float .NaN , null , null , null );
529
+ InternalSearchResponse response = new InternalSearchResponse (empty , null , null , null , false , false , 1 );
530
+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
531
+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
532
+ merger .add (searchResponse );
533
+ }
534
+ assertEquals (2 , merger .numResponses ());
535
+ SearchResponse mergedResponse = merger .getMergedResponse (clusters );
536
+ assertEquals (10 , mergedResponse .getHits ().getTotalHits ().value );
537
+ assertEquals (10 , mergedResponse .getHits ().getHits ().length );
538
+ assertEquals (2 , mergedResponse .getTotalShards ());
539
+ assertEquals (2 , mergedResponse .getSuccessfulShards ());
540
+ assertEquals (0 , mergedResponse .getSkippedShards ());
541
+ assertArrayEquals (sortFields , mergedResponse .getHits ().getSortFields ());
542
+ assertArrayEquals (hits , mergedResponse .getHits ().getHits ());
543
+ assertEquals (clusters , mergedResponse .getClusters ());
544
+ }
545
+
546
+ public void testMergeOnlyEmptyHits () {
547
+ long currentRelativeTime = randomLong ();
548
+ final SearchTimeProvider timeProvider = new SearchTimeProvider (randomLong (), 0 , () -> currentRelativeTime );
549
+ SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
550
+ Tuple <Integer , TotalHits .Relation > randomTrackTotalHits = randomTrackTotalHits ();
551
+ int trackTotalHitsUpTo = randomTrackTotalHits .v1 ();
552
+ TotalHits .Relation totalHitsRelation = randomTrackTotalHits .v2 ();
553
+ SearchResponseMerger merger = new SearchResponseMerger (0 , 10 , trackTotalHitsUpTo , timeProvider , flag -> null );
554
+ int numResponses = randomIntBetween (1 , 5 );
555
+ TotalHits expectedTotalHits = null ;
556
+ for (int i = 0 ; i < numResponses ; i ++) {
557
+ TotalHits totalHits = null ;
558
+ if (trackTotalHitsUpTo != SearchContext .TRACK_TOTAL_HITS_DISABLED ) {
559
+ totalHits = new TotalHits (randomLongBetween (0 , 1000 ), totalHitsRelation );
560
+ long previousValue = expectedTotalHits == null ? 0 : expectedTotalHits .value ;
561
+ expectedTotalHits = new TotalHits (Math .min (previousValue + totalHits .value , trackTotalHitsUpTo ), totalHitsRelation );
562
+ }
563
+ SearchHits empty = new SearchHits (new SearchHit [0 ], totalHits , Float .NaN , null , null , null );
564
+ InternalSearchResponse response = new InternalSearchResponse (empty , null , null , null , false , false , 1 );
565
+ SearchResponse searchResponse = new SearchResponse (response , null , 1 , 1 , 0 , 1L ,
566
+ ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
567
+ merger .add (searchResponse );
568
+ }
569
+ SearchResponse mergedResponse = merger .getMergedResponse (clusters );
570
+ assertEquals (expectedTotalHits , mergedResponse .getHits ().getTotalHits ());
571
+ }
572
+
507
573
private static Tuple <Integer , TotalHits .Relation > randomTrackTotalHits () {
508
574
switch (randomIntBetween (0 , 2 )) {
509
575
case 0 :
0 commit comments