Skip to content

Commit 4f57d6a

Browse files
authored
Fix approximate pointrangequery tests (opensearch-project#15590)
Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
1 parent ad1df9e commit 4f57d6a

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ public void grow(int count) {
146146
public void visit(int docID) {
147147
// it is possible that size < 1024 and docCount < size but we will continue to count through all the 1024 docs
148148
// and collect less, but it won't hurt performance
149-
if (docCount[0] < size) {
150-
adder.add(docID);
151-
docCount[0]++;
149+
if (docCount[0] >= size) {
150+
return;
152151
}
152+
adder.add(docID);
153+
docCount[0]++;
153154
}
154155

155156
@Override
@@ -231,7 +232,7 @@ private void intersectRight(PointValues.PointTree pointTree, PointValues.Interse
231232
public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree, long[] docCount)
232233
throws IOException {
233234
PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue());
234-
if (docCount[0] > size) {
235+
if (docCount[0] >= size) {
235236
return;
236237
}
237238
switch (r) {
@@ -279,7 +280,7 @@ public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.Poin
279280
public void intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree, long[] docCount)
280281
throws IOException {
281282
PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue());
282-
if (docCount[0] > size) {
283+
if (docCount[0] >= size) {
283284
return;
284285
}
285286
switch (r) {
@@ -435,6 +436,10 @@ public boolean canApproximate(SearchContext context) {
435436
if (!(context.query() instanceof ApproximateIndexOrDocValuesQuery)) {
436437
return false;
437438
}
439+
// size 0 could be set for caching
440+
if (context.from() + context.size() == 0) {
441+
this.setSize(10_000);
442+
}
438443
this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo()));
439444
if (context.request() != null && context.request().source() != null) {
440445
FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source());

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public void testApproximateRangeWithSizeUnderDefault() throws IOException {
127127
}
128128
doc.add(new LongPoint("point", scratch));
129129
iw.addDocument(doc);
130-
if (i % 15 == 0) iw.flush();
131130
}
132131
iw.flush();
132+
iw.forceMerge(1);
133133
try (IndexReader reader = iw.getReader()) {
134134
try {
135135
long lower = 0;
@@ -168,6 +168,7 @@ public void testApproximateRangeWithSizeOverDefault() throws IOException {
168168
iw.addDocument(doc);
169169
}
170170
iw.flush();
171+
iw.forceMerge(1);
171172
try (IndexReader reader = iw.getReader()) {
172173
try {
173174
long lower = 0;
@@ -185,7 +186,7 @@ protected String toString(int dimension, byte[] value) {
185186
};
186187
IndexSearcher searcher = new IndexSearcher(reader);
187188
TopDocs topDocs = searcher.search(approximateQuery, 11000);
188-
assertEquals(topDocs.totalHits, new TotalHits(11001, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO));
189+
assertEquals(topDocs.totalHits, new TotalHits(11000, TotalHits.Relation.EQUAL_TO));
189190
} catch (IOException e) {
190191
throw new RuntimeException(e);
191192
}
@@ -213,6 +214,7 @@ public void testApproximateRangeShortCircuit() throws IOException {
213214
if (i % 10 == 0) iw.flush();
214215
}
215216
iw.flush();
217+
iw.forceMerge(1);
216218
try (IndexReader reader = iw.getReader()) {
217219
try {
218220
long lower = 0;
@@ -258,6 +260,7 @@ public void testApproximateRangeShortCircuitAscSort() throws IOException {
258260
iw.addDocument(doc);
259261
}
260262
iw.flush();
263+
iw.forceMerge(1);
261264
try (IndexReader reader = iw.getReader()) {
262265
try {
263266
long lower = 0;
@@ -284,12 +287,12 @@ protected String toString(int dimension, byte[] value) {
284287
assertNotEquals(topDocs.totalHits, topDocs1.totalHits);
285288
assertEquals(topDocs.totalHits, new TotalHits(10, TotalHits.Relation.EQUAL_TO));
286289
assertEquals(topDocs1.totalHits, new TotalHits(21, TotalHits.Relation.EQUAL_TO));
287-
assertEquals(topDocs.scoreDocs[0].doc, 0);
288-
assertEquals(topDocs.scoreDocs[1].doc, 1);
289-
assertEquals(topDocs.scoreDocs[2].doc, 2);
290-
assertEquals(topDocs.scoreDocs[3].doc, 3);
291-
assertEquals(topDocs.scoreDocs[4].doc, 4);
292-
assertEquals(topDocs.scoreDocs[5].doc, 5);
290+
assertEquals(topDocs.scoreDocs[0].doc, topDocs1.scoreDocs[0].doc);
291+
assertEquals(topDocs.scoreDocs[1].doc, topDocs1.scoreDocs[1].doc);
292+
assertEquals(topDocs.scoreDocs[2].doc, topDocs1.scoreDocs[2].doc);
293+
assertEquals(topDocs.scoreDocs[3].doc, topDocs1.scoreDocs[3].doc);
294+
assertEquals(topDocs.scoreDocs[4].doc, topDocs1.scoreDocs[4].doc);
295+
assertEquals(topDocs.scoreDocs[5].doc, topDocs1.scoreDocs[5].doc);
293296

294297
} catch (IOException e) {
295298
throw new RuntimeException(e);

0 commit comments

Comments
 (0)