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
b42fa37
commit 900a685
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
...rc/test/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQueryTests.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,123 @@ | ||
/* | ||
* 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 org.apache.lucene.document.LongPoint; | ||
import org.apache.lucene.document.SortedNumericDocValuesField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.IndexWriter; | ||
import org.apache.lucene.search.ConstantScoreWeight; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.PointRangeQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.ScoreMode; | ||
import org.apache.lucene.search.Weight; | ||
import org.apache.lucene.store.Directory; | ||
import org.opensearch.search.internal.SearchContext; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.apache.lucene.document.LongPoint.pack; | ||
|
||
public class ApproximateIndexOrDocValuesQueryTests extends OpenSearchTestCase { | ||
private Directory dir; | ||
private IndexWriter w; | ||
private DirectoryReader reader; | ||
private IndexSearcher searcher; | ||
|
||
@Before | ||
public void initSearcher() throws IOException { | ||
dir = newDirectory(); | ||
w = new IndexWriter(dir, newIndexWriterConfig()); | ||
} | ||
|
||
@After | ||
public void closeAllTheReaders() throws IOException { | ||
reader.close(); | ||
w.close(); | ||
dir.close(); | ||
} | ||
|
||
public void testApproximateIndexOrDocValuesQueryWeight() throws IOException { | ||
|
||
long l = Long.MIN_VALUE; | ||
long u = Long.MAX_VALUE; | ||
Query indexQuery = new PointRangeQuery( | ||
"test-index", | ||
pack(new long[] { l }).bytes, | ||
pack(new long[] { u }).bytes, | ||
new long[] { l }.length | ||
) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
|
||
ApproximateableQuery approximateIndexQuery = new ApproximatePointRangeQuery( | ||
"test-index", | ||
pack(new long[] { l }).bytes, | ||
pack(new long[] { u }).bytes, | ||
new long[] { l }.length | ||
) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
}; | ||
|
||
Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery("test-index", l, u); | ||
|
||
ApproximateIndexOrDocValuesQuery approximateIndexOrDocValuesQuery = new ApproximateIndexOrDocValuesQuery( | ||
indexQuery, | ||
approximateIndexQuery, | ||
dvQuery | ||
); | ||
|
||
reader = DirectoryReader.open(w); | ||
searcher = newSearcher(reader); | ||
|
||
Weight weight = approximateIndexOrDocValuesQuery.createWeight(searcher, ScoreMode.COMPLETE, 1f); | ||
|
||
// we only get weight since we're expecting to call IODVQ | ||
assertFalse(weight instanceof ConstantScoreWeight); | ||
|
||
ApproximateableQuery approximateIndexQueryCanApproximate = new ApproximatePointRangeQuery( | ||
"test-index", | ||
pack(new long[] { l }).bytes, | ||
pack(new long[] { u }).bytes, | ||
new long[] { l }.length | ||
) { | ||
protected String toString(int dimension, byte[] value) { | ||
return Long.toString(LongPoint.decodeDimension(value, 0)); | ||
} | ||
|
||
public boolean canApproximate(SearchContext context) { | ||
return true; | ||
} | ||
}; | ||
|
||
ApproximateIndexOrDocValuesQuery approximateIndexOrDocValuesQueryCanApproximate = new ApproximateIndexOrDocValuesQuery( | ||
indexQuery, | ||
approximateIndexQueryCanApproximate, | ||
dvQuery | ||
); | ||
|
||
Weight approximateIndexOrDocValuesQueryCanApproximateWeight = approximateIndexOrDocValuesQueryCanApproximate.createWeight( | ||
searcher, | ||
ScoreMode.COMPLETE, | ||
1f | ||
); | ||
|
||
// we get ConstantScoreWeight since we're expecting to call ApproximatePointRangeQuery | ||
assertTrue(approximateIndexOrDocValuesQueryCanApproximateWeight instanceof ConstantScoreWeight); | ||
|
||
} | ||
} |
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