3232import org .elasticsearch .search .SearchHits ;
3333import org .elasticsearch .search .SearchShardTarget ;
3434import org .elasticsearch .search .internal .InternalSearchResponse ;
35+ import org .elasticsearch .search .internal .SearchContext ;
3536import org .elasticsearch .search .profile .ProfileShardResult ;
3637import org .elasticsearch .search .profile .SearchProfileShardResults ;
3738import org .elasticsearch .search .profile .SearchProfileShardResultsTests ;
@@ -84,7 +85,7 @@ public void testMergeTookInMillis() throws InterruptedException {
8485 SearchTimeProvider timeProvider = new SearchTimeProvider (randomLong (), 0 , () -> currentRelativeTime );
8586 SearchResponse .Clusters clusters = SearchResponseTests .randomClusters ();
8687 SearchResponseMerger merger = new SearchResponseMerger (randomIntBetween (0 , 1000 ), randomIntBetween (0 , 10000 ),
87- timeProvider , clusters , flag -> null );
88+ SearchContext . TRACK_TOTAL_HITS_ACCURATE , timeProvider , clusters , flag -> null );
8889 for (int i = 0 ; i < numResponses ; i ++) {
8990 SearchResponse searchResponse = new SearchResponse (InternalSearchResponse .empty (), null , 1 , 1 , 0 , randomLong (),
9091 ShardSearchFailure .EMPTY_ARRAY , SearchResponseTests .randomClusters ());
@@ -97,7 +98,8 @@ public void testMergeTookInMillis() throws InterruptedException {
9798
9899 public void testMergeShardFailures () throws InterruptedException {
99100 SearchTimeProvider searchTimeProvider = new SearchTimeProvider (0 , 0 , () -> 0 );
100- SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
101+ SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , SearchContext .TRACK_TOTAL_HITS_ACCURATE ,
102+ searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
101103 PriorityQueue <Tuple <ShardId , ShardSearchFailure >> priorityQueue = new PriorityQueue <>(Comparator .comparing (Tuple ::v1 ));
102104 int numIndices = numResponses * randomIntBetween (1 , 3 );
103105 Iterator <Map .Entry <String , Index []>> indicesPerCluster = randomRealisticIndices (numIndices , numResponses ).entrySet ().iterator ();
@@ -136,7 +138,8 @@ public void testMergeShardFailures() throws InterruptedException {
136138
137139 public void testMergeShardFailuresNullShardId () throws InterruptedException {
138140 SearchTimeProvider searchTimeProvider = new SearchTimeProvider (0 , 0 , () -> 0 );
139- SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
141+ SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , SearchContext .TRACK_TOTAL_HITS_ACCURATE ,
142+ searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
140143 List <ShardSearchFailure > expectedFailures = new ArrayList <>();
141144 for (int i = 0 ; i < numResponses ; i ++) {
142145 int numFailures = randomIntBetween (1 , 50 );
@@ -157,7 +160,8 @@ public void testMergeShardFailuresNullShardId() throws InterruptedException {
157160
158161 public void testMergeProfileResults () throws InterruptedException {
159162 SearchTimeProvider searchTimeProvider = new SearchTimeProvider (0 , 0 , () -> 0 );
160- SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
163+ SearchResponseMerger merger = new SearchResponseMerger (0 , 0 , SearchContext .TRACK_TOTAL_HITS_ACCURATE ,
164+ searchTimeProvider , SearchResponse .Clusters .EMPTY , flag -> null );
161165 Map <String , ProfileShardResult > expectedProfile = new HashMap <>();
162166 for (int i = 0 ; i < numResponses ; i ++) {
163167 SearchProfileShardResults profile = SearchProfileShardResultsTests .createTestItem ();
@@ -206,10 +210,14 @@ public void testMergeSearchHits() throws InterruptedException {
206210 sortFields = null ;
207211 scoreSort = true ;
208212 }
209- TotalHits .Relation totalHitsRelation = frequently () ? randomFrom (TotalHits .Relation .values ()) : null ;
213+ Tuple <Integer , TotalHits .Relation > randomTrackTotalHits = randomTrackTotalHits ();
214+ int trackTotalHitsUpTo = randomTrackTotalHits .v1 ();
215+ TotalHits .Relation totalHitsRelation = randomTrackTotalHits .v2 ();
210216
211217 PriorityQueue <SearchHit > priorityQueue = new PriorityQueue <>(new SearchHitComparator (sortFields ));
212- SearchResponseMerger searchResponseMerger = new SearchResponseMerger (from , size , timeProvider , clusters , flag -> null );
218+ SearchResponseMerger searchResponseMerger = new SearchResponseMerger (from , size , trackTotalHitsUpTo ,
219+ timeProvider , clusters , flag -> null );
220+
213221 TotalHits expectedTotalHits = null ;
214222 int expectedTotal = 0 ;
215223 int expectedSuccessful = 0 ;
@@ -232,11 +240,10 @@ public void testMergeSearchHits() throws InterruptedException {
232240 expectedSkipped += skipped ;
233241
234242 TotalHits totalHits = null ;
235- if (totalHitsRelation != null ) {
236- //TODO totalHits may overflow if each cluster reports a very high number?
243+ if (trackTotalHitsUpTo != SearchContext .TRACK_TOTAL_HITS_DISABLED ) {
237244 totalHits = new TotalHits (randomLongBetween (0 , 1000 ), totalHitsRelation );
238245 long previousValue = expectedTotalHits == null ? 0 : expectedTotalHits .value ;
239- expectedTotalHits = new TotalHits (previousValue + totalHits .value , totalHitsRelation );
246+ expectedTotalHits = new TotalHits (Math . min ( previousValue + totalHits .value , trackTotalHitsUpTo ) , totalHitsRelation );
240247 }
241248
242249 final int numDocs = totalHits == null || totalHits .value >= requestedSize ? requestedSize : (int ) totalHits .value ;
@@ -321,6 +328,19 @@ public void testMergeSearchHits() throws InterruptedException {
321328 }
322329 }
323330
331+ private static Tuple <Integer , TotalHits .Relation > randomTrackTotalHits () {
332+ switch (randomIntBetween (0 , 2 )) {
333+ case 0 :
334+ return Tuple .tuple (SearchContext .TRACK_TOTAL_HITS_DISABLED , null );
335+ case 1 :
336+ return Tuple .tuple (randomIntBetween (10 , 1000 ), TotalHits .Relation .GREATER_THAN_OR_EQUAL_TO );
337+ case 2 :
338+ return Tuple .tuple (SearchContext .TRACK_TOTAL_HITS_ACCURATE , TotalHits .Relation .EQUAL_TO );
339+ default :
340+ throw new UnsupportedOperationException ();
341+ }
342+ }
343+
324344 private static SearchHit [] randomSearchHitArray (int numDocs , int numResponses , String clusterAlias , Index [] indices , float maxScore ,
325345 int scoreFactor , SortField [] sortFields , PriorityQueue <SearchHit > priorityQueue ) {
326346 SearchHit [] hits = new SearchHit [numDocs ];
0 commit comments