Skip to content

Commit 1ee8822

Browse files
Fix flaky test (#18235)
Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent 2c1b2aa commit 1ee8822

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

server/src/internalClusterTest/java/org/opensearch/search/simple/SimpleSearchIT.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,14 @@ public void testSimpleTerminateAfterTrackTotalHitsUpToRandomSize0() throws Excep
439439
doTestSimpleTerminateAfterTrackTotalHitsUpTo(0);
440440
}
441441

442-
public void testSimpleTerminateAfterTrackTotalHitsUpToSize() throws Exception {
443-
doTestSimpleTerminateAfterTrackTotalHitsUpTo(randomIntBetween(1, 29));
442+
// Tests scenario where size <= track_total_hits_up_to (5)
443+
public void testSimpleTerminateAfterTrackTotalHitsUpToSmallSize() throws Exception {
444+
doTestSimpleTerminateAfterTrackTotalHitsUpTo(randomIntBetween(1, 5));
445+
}
446+
447+
// Tests scenario where size > track_total_hits_up_to (5)
448+
public void testSimpleTerminateAfterTrackTotalHitsUpToLargeSize() throws Exception {
449+
doTestSimpleTerminateAfterTrackTotalHitsUpTo(randomIntBetween(6, 29));
444450
}
445451

446452
public void testSimpleIndexSortEarlyTerminate() throws Exception {

server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ public boolean canApproximate(SearchContext context) {
449449
if (context.from() + context.size() == 0) {
450450
this.setSize(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO);
451451
} else {
452-
this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo()));
452+
// We add +1 to ensure we collect at least one more document than required. This guarantees correct relation value:
453+
// - If we find exactly trackTotalHitsUpTo docs: relation = EQUAL_TO
454+
// - If we find > trackTotalHitsUpTo docs: relation = GREATER_THAN_OR_EQUAL_TO
455+
// With +1, we will consistently get GREATER_THAN_OR_EQUAL_TO relation.
456+
this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo()) + 1);
453457
}
454458
if (context.request() != null && context.request().source() != null) {
455459
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source());

0 commit comments

Comments
 (0)