Skip to content

Commit

Permalink
Fix flaky test in testApproximateRangeWithSizeOverDefault by adjust…
Browse files Browse the repository at this point in the history
…ing 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>
  • Loading branch information
github-actions[bot] committed Oct 23, 2024
1 parent 9b72ec4 commit 5a19403
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fix array hashCode calculation in ResyncReplicationRequest ([#16378](https://github.com/opensearch-project/OpenSearch/pull/16378))
- [Workload Management] Enhance rejection mechanism in workload management ([#16417](https://github.com/opensearch-project/OpenSearch/pull/16417))
- [Workload Management] Fixing Create/Update QueryGroup TransportActions to execute from non-cluster manager nodes ([16422](https://github.com/opensearch-project/OpenSearch/pull/16422))
- Fix flaky test in `testApproximateRangeWithSizeOverDefault` by adjusting totalHits assertion logic ([#16434](https://github.com/opensearch-project/OpenSearch/pull/16434#pullrequestreview-2386999409))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.TotalHits.Relation;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.opensearch.search.internal.SearchContext;
Expand Down Expand Up @@ -175,6 +176,7 @@ public void testApproximateRangeWithSizeOverDefault() throws IOException {
try {
long lower = 0;
long upper = 12000;
long maxHits = 12001;
Query approximateQuery = new ApproximatePointRangeQuery(
"point",
pack(lower).bytes,
Expand All @@ -188,7 +190,13 @@ protected String toString(int dimension, byte[] value) {
};
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(approximateQuery, 11000);
assertEquals(topDocs.totalHits, new TotalHits(11000, TotalHits.Relation.EQUAL_TO));

if (topDocs.totalHits.relation == Relation.EQUAL_TO) {
assertEquals(topDocs.totalHits.value, 11000);
} else {
assertTrue(11000 <= topDocs.totalHits.value);
assertTrue(maxHits >= topDocs.totalHits.value);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -226,7 +234,7 @@ protected String toString(int dimension, byte[] value) {
}
};
Query query = LongPoint.newRangeQuery("point", lower, upper);
;

IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(approximateQuery, 10);
TopDocs topDocs1 = searcher.search(query, 10);
Expand All @@ -235,7 +243,6 @@ protected String toString(int dimension, byte[] value) {
assertNotEquals(topDocs.totalHits, topDocs1.totalHits);
assertEquals(topDocs.totalHits, new TotalHits(10, TotalHits.Relation.EQUAL_TO));
assertEquals(topDocs1.totalHits, new TotalHits(101, TotalHits.Relation.EQUAL_TO));

} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -278,7 +285,7 @@ protected String toString(int dimension, byte[] value) {
}
};
Query query = LongPoint.newRangeQuery("point", lower, upper);
;

IndexSearcher searcher = new IndexSearcher(reader);
Sort sort = new Sort(new SortField("point", SortField.Type.LONG));
TopDocs topDocs = searcher.search(approximateQuery, 10, sort);
Expand Down

0 comments on commit 5a19403

Please sign in to comment.