Skip to content

Commit 19d2f16

Browse files
committed
Fix SearchResponseMerger#testMergeSearchHits
This commit fixes an edge case in tests where search hits are empty after the merge but some shards returned hits. This can happen if the total number of merged hits is less than the provided `from`. Closes #40553
1 parent f2ec4a2 commit 19d2f16

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ public void testMergeAggs() throws InterruptedException {
330330
assertEquals(totalCount, bucket.getDocCount());
331331
}
332332

333-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40553")
334333
public void testMergeSearchHits() throws InterruptedException {
335334
final long currentRelativeTime = randomLong();
336335
final SearchTimeProvider timeProvider = new SearchTimeProvider(randomLong(), 0, () -> currentRelativeTime);
@@ -376,6 +375,7 @@ public void testMergeSearchHits() throws InterruptedException {
376375
float expectedMaxScore = Float.NEGATIVE_INFINITY;
377376
int numIndices = requestedSize == 0 ? 0 : randomIntBetween(1, requestedSize);
378377
Iterator<Map.Entry<String, Index[]>> indicesIterator = randomRealisticIndices(numIndices, numResponses).entrySet().iterator();
378+
boolean hasHits = false;
379379
for (int i = 0; i < numResponses; i++) {
380380
Map.Entry<String, Index[]> entry = indicesIterator.next();
381381
String clusterAlias = entry.getKey();
@@ -399,6 +399,7 @@ public void testMergeSearchHits() throws InterruptedException {
399399
float maxScore = scoreSort ? numDocs * scoreFactor : Float.NaN;
400400
SearchHit[] hits = randomSearchHitArray(numDocs, numResponses, clusterAlias, indices, maxScore, scoreFactor,
401401
sortFields, priorityQueue);
402+
hasHits |= hits.length > 0;
402403
expectedMaxScore = Math.max(expectedMaxScore, maxScore);
403404

404405
Object[] collapseValues = null;
@@ -448,7 +449,7 @@ public void testMergeSearchHits() throws InterruptedException {
448449

449450
SearchHits searchHits = searchResponse.getHits();
450451
// the sort fields and the collapse field are not returned when hits are empty
451-
if (searchHits.getHits().length > 0) {
452+
if (hasHits) {
452453
assertArrayEquals(sortFields, searchHits.getSortFields());
453454
assertEquals(collapseField, searchHits.getCollapseField());
454455
} else {
@@ -474,7 +475,7 @@ public void testMergeSearchHits() throws InterruptedException {
474475
SearchHit[] hits = searchHits.getHits();
475476
if (collapseField != null
476477
// the collapse field is not returned when hits are empty
477-
&& hits.length > 0) {
478+
&& hasHits) {
478479
assertEquals(hits.length, searchHits.getCollapseValues().length);
479480
} else {
480481
assertNull(searchHits.getCollapseValues());

0 commit comments

Comments
 (0)