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 (opensearch-project#15807)

- 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]>
  • Loading branch information
inpink committed Oct 23, 2024
1 parent 20e233e commit 7533e97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ 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))
- Fix missing fields in task index mapping to ensure proper task result storage ([#16201](https://github.com/opensearch-project/OpenSearch/pull/16201))
- Fix typo super->sb in method toString() of RemoteStoreNodeAttribute ([#15362](https://github.com/opensearch-project/OpenSearch/pull/15362))
- 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 7533e97

Please sign in to comment.