Skip to content

Commit b9d9f59

Browse files
authored
Fix QueryPhaseTest that use trackTotalHits (#127470)
This commit fixes a couple of test scenarios in QueryPhaseTest that incorrectly assert values around trackTotalHits. Test updated as per apache/lucene#14561 This test fix is needed when running with Lucene >= 10.2.1
1 parent 6515d32 commit b9d9f59

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.apache.lucene.search.TermQuery;
5656
import org.apache.lucene.search.TopDocs;
5757
import org.apache.lucene.search.TotalHits;
58+
import org.apache.lucene.search.TotalHits.Relation;
5859
import org.apache.lucene.search.Weight;
5960
import org.apache.lucene.search.join.BitSetProducer;
6061
import org.apache.lucene.search.join.ScoreMode;
@@ -567,12 +568,13 @@ public void testTerminateAfterWithHitsNoHitCountShortcut() throws Exception {
567568
// size is lower than terminate_after
568569
context.setSize(5);
569570
// track_total_hits is lower than terminate_after
570-
context.trackTotalHitsUpTo(randomIntBetween(1, 6));
571+
int trackTotalHits = randomIntBetween(1, 6);
572+
context.trackTotalHitsUpTo(trackTotalHits);
571573
QueryPhase.executeQuery(context);
572574
// depending on docs distribution we may or may not be able to honor terminate_after: low scoring hits are skipped via
573575
// setMinCompetitiveScore, which bypasses terminate_after until the next leaf collector is pulled, when that happens.
574576
assertThat(context.queryResult().terminatedEarly(), either(is(true)).or(is(false)));
575-
assertThat(context.queryResult().topDocs().topDocs.totalHits.value(), equalTo(7L));
577+
assertThat(context.queryResult().topDocs().topDocs.totalHits.value(), greaterThanOrEqualTo((long) trackTotalHits));
576578
assertThat(context.queryResult().topDocs().topDocs.totalHits.relation(), equalTo(TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO));
577579
assertThat(context.queryResult().topDocs().topDocs.scoreDocs.length, equalTo(5));
578580
}
@@ -990,7 +992,10 @@ public void testMinScore() throws Exception {
990992
context.trackTotalHitsUpTo(5);
991993

992994
QueryPhase.addCollectorsAndSearch(context);
993-
assertEquals(10, context.queryResult().topDocs().topDocs.totalHits.value());
995+
TotalHits totalHits = context.queryResult().topDocs().topDocs.totalHits;
996+
assertThat(totalHits.value(), greaterThanOrEqualTo(5L));
997+
var expectedRelation = totalHits.value() == 10 ? Relation.EQUAL_TO : Relation.GREATER_THAN_OR_EQUAL_TO;
998+
assertThat(totalHits.relation(), is(expectedRelation));
994999
}
9951000
}
9961001

0 commit comments

Comments
 (0)