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

- 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.
  • Loading branch information
inpink committed Oct 22, 2024
1 parent 6a5e60f commit ac01491
Showing 1 changed file with 11 additions and 4 deletions.
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 ac01491

Please sign in to comment.