Skip to content

Commit

Permalink
Fix SearchResponseMerger#testMergeSearchHits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimczi committed Mar 28, 2019
1 parent c122fc6 commit c03f2e6
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ public void testMergeAggs() throws InterruptedException {
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);
Expand Down Expand Up @@ -442,6 +441,7 @@ public void testMergeSearchHits() throws InterruptedException {
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();
Expand All @@ -465,6 +465,7 @@ public void testMergeSearchHits() throws InterruptedException {
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;
Expand Down Expand Up @@ -514,7 +515,7 @@ public void testMergeSearchHits() throws InterruptedException {

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 {
Expand All @@ -540,7 +541,7 @@ public void testMergeSearchHits() throws InterruptedException {
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());
Expand Down

0 comments on commit c03f2e6

Please sign in to comment.