Skip to content

Commit 5a19403

Browse files
Fix flaky test in testApproximateRangeWithSizeOverDefault by adjusting totalHits assertion logic (#15807) (#16434)
- Updated the test to account for Lucene's behavior where `IndexSearcher.search()` may return `GREATER_THAN_OR_EQUAL_TO` for totalHits when the number of matches exceeds 1000. - Added logic to check if `totalHits.relation` is `EQUAL_TO`. If so, assert that the count is exactly 11000. Otherwise, ensure the count is at least 11000 and within the allowed upper limit (`maxHits`). - This change prevents intermittent test failures caused by Lucene’s performance optimizations. Signed-off-by: inpink <[email protected]> (cherry picked from commit 66f0110) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 9b72ec4 commit 5a19403

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
9696
- Fix array hashCode calculation in ResyncReplicationRequest ([#16378](https://github.com/opensearch-project/OpenSearch/pull/16378))
9797
- [Workload Management] Enhance rejection mechanism in workload management ([#16417](https://github.com/opensearch-project/OpenSearch/pull/16417))
9898
- [Workload Management] Fixing Create/Update QueryGroup TransportActions to execute from non-cluster manager nodes ([16422](https://github.com/opensearch-project/OpenSearch/pull/16422))
99+
- Fix flaky test in `testApproximateRangeWithSizeOverDefault` by adjusting totalHits assertion logic ([#16434](https://github.com/opensearch-project/OpenSearch/pull/16434#pullrequestreview-2386999409))
99100

100101
### Security
101102

server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.lucene.search.SortField;
2222
import org.apache.lucene.search.TopDocs;
2323
import org.apache.lucene.search.TotalHits;
24+
import org.apache.lucene.search.TotalHits.Relation;
2425
import org.apache.lucene.store.Directory;
2526
import org.apache.lucene.tests.index.RandomIndexWriter;
2627
import org.opensearch.search.internal.SearchContext;
@@ -175,6 +176,7 @@ public void testApproximateRangeWithSizeOverDefault() throws IOException {
175176
try {
176177
long lower = 0;
177178
long upper = 12000;
179+
long maxHits = 12001;
178180
Query approximateQuery = new ApproximatePointRangeQuery(
179181
"point",
180182
pack(lower).bytes,
@@ -188,7 +190,13 @@ protected String toString(int dimension, byte[] value) {
188190
};
189191
IndexSearcher searcher = new IndexSearcher(reader);
190192
TopDocs topDocs = searcher.search(approximateQuery, 11000);
191-
assertEquals(topDocs.totalHits, new TotalHits(11000, TotalHits.Relation.EQUAL_TO));
193+
194+
if (topDocs.totalHits.relation == Relation.EQUAL_TO) {
195+
assertEquals(topDocs.totalHits.value, 11000);
196+
} else {
197+
assertTrue(11000 <= topDocs.totalHits.value);
198+
assertTrue(maxHits >= topDocs.totalHits.value);
199+
}
192200
} catch (IOException e) {
193201
throw new RuntimeException(e);
194202
}
@@ -226,7 +234,7 @@ protected String toString(int dimension, byte[] value) {
226234
}
227235
};
228236
Query query = LongPoint.newRangeQuery("point", lower, upper);
229-
;
237+
230238
IndexSearcher searcher = new IndexSearcher(reader);
231239
TopDocs topDocs = searcher.search(approximateQuery, 10);
232240
TopDocs topDocs1 = searcher.search(query, 10);
@@ -235,7 +243,6 @@ protected String toString(int dimension, byte[] value) {
235243
assertNotEquals(topDocs.totalHits, topDocs1.totalHits);
236244
assertEquals(topDocs.totalHits, new TotalHits(10, TotalHits.Relation.EQUAL_TO));
237245
assertEquals(topDocs1.totalHits, new TotalHits(101, TotalHits.Relation.EQUAL_TO));
238-
239246
} catch (IOException e) {
240247
throw new RuntimeException(e);
241248
}
@@ -278,7 +285,7 @@ protected String toString(int dimension, byte[] value) {
278285
}
279286
};
280287
Query query = LongPoint.newRangeQuery("point", lower, upper);
281-
;
288+
282289
IndexSearcher searcher = new IndexSearcher(reader);
283290
Sort sort = new Sort(new SortField("point", SortField.Type.LONG));
284291
TopDocs topDocs = searcher.search(approximateQuery, 10, sort);

0 commit comments

Comments
 (0)