forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
- Loading branch information
1 parent
e92bd90
commit 5728c31
Showing
3 changed files
with
186 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.approximate; | ||
|
||
import com.carrotsearch.randomizedtesting.generators.RandomNumbers; | ||
|
||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer; | ||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.LongPoint; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.PointRangeQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TopDocs; | ||
import org.apache.lucene.search.TotalHits; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.tests.index.RandomIndexWriter; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.apache.lucene.document.LongPoint.pack; | ||
|
||
public class ApproximatePointRangeQueryTests extends OpenSearchTestCase { | ||
|
||
public void testApproximateRangeEqualsActualRange() throws IOException { | ||
try (Directory directory = newDirectory()) { | ||
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory, new WhitespaceAnalyzer())) { | ||
int dims = 1; | ||
|
||
long[] scratch = new long[dims]; | ||
for (int i = 0; i < 100; i++) { | ||
int numPoints = RandomNumbers.randomIntBetween(random(), 1, 10); | ||
Document doc = new Document(); | ||
for (int j = 0; j < numPoints; j++) { | ||
for (int v = 0; v < dims; v++) { | ||
scratch[v] = RandomNumbers.randomLongBetween(random(), 0, 100); | ||
} | ||
doc.add(new LongPoint("point", scratch)); | ||
} | ||
iw.addDocument(doc); | ||
} | ||
iw.flush(); | ||
try (IndexReader reader = iw.getReader()) { | ||
try { | ||
long lower = RandomNumbers.randomLongBetween(random(), -100, 200); | ||
long upper = lower + RandomNumbers.randomLongBetween(random(), 0, 100); | ||
Query approximateQuery = new ApproximatePointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
Query query = new PointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
IndexSearcher searcher = new IndexSearcher(reader); | ||
TopDocs topDocs = searcher.search(approximateQuery, 10); | ||
TopDocs topDocs1 = searcher.search(query, 10); | ||
assertEquals(topDocs.totalHits, topDocs1.totalHits); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
public void testApproximateRangeWithSize() throws IOException { | ||
try (Directory directory = newDirectory()) { | ||
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory, new WhitespaceAnalyzer())) { | ||
int dims = 1; | ||
|
||
long[] scratch = new long[dims]; | ||
for (int i = 0; i < 100; i++) { | ||
int numPoints = RandomNumbers.randomIntBetween(random(), 1, 10); | ||
Document doc = new Document(); | ||
for (int j = 0; j < numPoints; j++) { | ||
for (int v = 0; v < dims; v++) { | ||
scratch[v] = RandomNumbers.randomLongBetween(random(), 0, 100); | ||
} | ||
doc.add(new LongPoint("point", scratch)); | ||
} | ||
iw.addDocument(doc); | ||
} | ||
iw.flush(); | ||
try (IndexReader reader = iw.getReader()) { | ||
try { | ||
long lower = RandomNumbers.randomLongBetween(random(), -100, 200); | ||
long upper = lower + RandomNumbers.randomLongBetween(random(), 0, 100); | ||
Query approximateQuery = new ApproximatePointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims, 100) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
Query query = new PointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
IndexSearcher searcher = new IndexSearcher(reader); | ||
TopDocs topDocs = searcher.search(approximateQuery, 10); | ||
TopDocs topDocs1 = searcher.search(query, 10); | ||
assertEquals(topDocs.totalHits, topDocs1.totalHits); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
public void testApproximateRangeShortCircuit() throws IOException { | ||
try (Directory directory = newDirectory()) { | ||
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory, new WhitespaceAnalyzer())) { | ||
int dims = 1; | ||
|
||
long[] scratch = new long[dims]; | ||
int numPoints = 1000; | ||
for (int i = 0; i < numPoints; i++) { | ||
Document doc = new Document(); | ||
for (int v = 0; v < dims; v++) { | ||
scratch[v] = i; | ||
} | ||
doc.add(new LongPoint("point", scratch)); | ||
iw.addDocument(doc); | ||
if (i % 10 == 0) iw.flush(); | ||
} | ||
iw.flush(); | ||
try (IndexReader reader = iw.getReader()) { | ||
try { | ||
long lower = 0; | ||
long upper = 100; | ||
Query approximateQuery = new ApproximatePointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims, 10) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
Query query = new PointRangeQuery("point", pack(lower).bytes, pack(upper).bytes, dims) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
IndexSearcher searcher = new IndexSearcher(reader); | ||
TopDocs topDocs = searcher.search(approximateQuery, 10); | ||
TopDocs topDocs1 = searcher.search(query, 10); | ||
|
||
// since we short-circuit from the approx range at the end of size these will not be equal | ||
assertNotEquals(topDocs.totalHits, topDocs1.totalHits); | ||
assertEquals(topDocs.totalHits, new TotalHits(11, TotalHits.Relation.EQUAL_TO)); | ||
assertEquals(topDocs1.totalHits, new TotalHits(101, TotalHits.Relation.EQUAL_TO)); | ||
|
||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,5 @@ protected String toString(int dimension, byte[] value) { | |
} | ||
} | ||
} | ||
|
||
} | ||
} |