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
This commit is contained in:
jimczi 2019-03-28 09:55:29 +01:00
parent 89d97905e5
commit 8775e37d03
1 changed files with 4 additions and 3 deletions

View File

@ -396,7 +396,6 @@ public class SearchResponseMergerTests extends ESTestCase {
assertEquals(totalCount, bucket.getDocCount());
}
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40553")
public void testMergeSearchHits() throws InterruptedException {
final long currentRelativeTime = randomLong();
final SearchTimeProvider timeProvider = new SearchTimeProvider(randomLong(), 0, () -> currentRelativeTime);
@ -442,6 +441,7 @@ public class SearchResponseMergerTests extends ESTestCase {
float expectedMaxScore = Float.NEGATIVE_INFINITY;
int numIndices = requestedSize == 0 ? 0 : randomIntBetween(1, requestedSize);
Iterator<Map.Entry<String, Index[]>> indicesIterator = randomRealisticIndices(numIndices, numResponses).entrySet().iterator();
boolean hasHits = false;
for (int i = 0; i < numResponses; i++) {
Map.Entry<String, Index[]> entry = indicesIterator.next();
String clusterAlias = entry.getKey();
@ -465,6 +465,7 @@ public class SearchResponseMergerTests extends ESTestCase {
float maxScore = scoreSort ? numDocs * scoreFactor : Float.NaN;
SearchHit[] hits = randomSearchHitArray(numDocs, numResponses, clusterAlias, indices, maxScore, scoreFactor,
sortFields, priorityQueue);
hasHits |= hits.length > 0;
expectedMaxScore = Math.max(expectedMaxScore, maxScore);
Object[] collapseValues = null;
@ -514,7 +515,7 @@ public class SearchResponseMergerTests extends ESTestCase {
SearchHits searchHits = searchResponse.getHits();
// the sort fields and the collapse field are not returned when hits are empty
if (searchHits.getHits().length > 0) {
if (hasHits) {
assertArrayEquals(sortFields, searchHits.getSortFields());
assertEquals(collapseField, searchHits.getCollapseField());
} else {
@ -540,7 +541,7 @@ public class SearchResponseMergerTests extends ESTestCase {
SearchHit[] hits = searchHits.getHits();
if (collapseField != null
// the collapse field is not returned when hits are empty
&& hits.length > 0) {
&& hasHits) {
assertEquals(hits.length, searchHits.getCollapseValues().length);
} else {
assertNull(searchHits.getCollapseValues());