diff --git a/server/src/main/java/org/opensearch/common/util/FeatureFlags.java b/server/src/main/java/org/opensearch/common/util/FeatureFlags.java index e2554d61116ad..b0d0b4dd2b5d2 100644 --- a/server/src/main/java/org/opensearch/common/util/FeatureFlags.java +++ b/server/src/main/java/org/opensearch/common/util/FeatureFlags.java @@ -122,6 +122,16 @@ public class FeatureFlags { Property.NodeScope ); + /** + * Gates the functionality of ApproximatePointRangeQuery where we approximate query results. + */ + public static final String APPROXIMATE_POINT_RANGE_QUERY = "opensearch.experimental.feature.approximate_point_range_query.enabled"; + public static final Setting APPROXIMATE_POINT_RANGE_QUERY_SETTING = Setting.boolSetting( + APPROXIMATE_POINT_RANGE_QUERY, + false, + Property.NodeScope + ); + private static final List> ALL_FEATURE_FLAG_SETTINGS = List.of( REMOTE_STORE_MIGRATION_EXPERIMENTAL_SETTING, EXTENSIONS_SETTING, diff --git a/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java index 90a188c9d6743..e60d5a739a9ae 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java @@ -38,8 +38,8 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.PointValues; import org.apache.lucene.search.BoostQuery; +import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery; -import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.Query; import org.opensearch.OpenSearchParseException; import org.opensearch.Version; @@ -111,6 +111,21 @@ public static DateFormatter getDefaultDateTimeFormatter() { : LEGACY_DEFAULT_DATE_TIME_FORMATTER; } + public static Query getDefaultQuery(Query pointRangeQuery, Query dvQuery, String name, long l, long u) { + return FeatureFlags.isEnabled(FeatureFlags.APPROXIMATE_POINT_RANGE_QUERY_SETTING) + ? new ApproximateIndexOrDocValuesQuery( + pointRangeQuery, + new ApproximatePointRangeQuery(name, pack(new long[] { l }).bytes, pack(new long[] { u }).bytes, new long[] { l }.length) { + @Override + protected String toString(int dimension, byte[] value) { + return Long.toString(LongPoint.decodeDimension(value, 0)); + } + }, + dvQuery + ) + : new IndexOrDocValuesQuery(pointRangeQuery, dvQuery); + } + /** * Resolution of the date time * @@ -466,24 +481,10 @@ public Query rangeQuery( } DateMathParser parser = forcedDateParser == null ? dateMathParser : forcedDateParser; return dateRangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, timeZone, parser, context, resolution, (l, u) -> { - Query pointRangeQuery = isSearchable() ? createPointRangeQuery(l, u) : null; + Query pointRangeQuery = isSearchable() ? LongPoint.newRangeQuery(name(), l, u) : null; Query dvQuery = hasDocValues() ? SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u) : null; if (isSearchable() && hasDocValues()) { - Query query = new ApproximateIndexOrDocValuesQuery( - pointRangeQuery, - new ApproximatePointRangeQuery( - name(), - pack(new long[] { l }).bytes, - pack(new long[] { u }).bytes, - new long[] { l }.length - ) { - @Override - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, - dvQuery - ); + Query query = getDefaultQuery(pointRangeQuery, dvQuery, name(), l, u); if (context.indexSortedOnField(name())) { query = new IndexSortSortedNumericDocValuesRangeQuery(name(), l, u, query); } @@ -499,14 +500,6 @@ protected String toString(int dimension, byte[] value) { }); } - private Query createPointRangeQuery(long l, long u) { - return new PointRangeQuery(name(), 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 static Query dateRangeQuery( Object lowerTerm, Object upperTerm, diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/filterrewrite/Helper.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/filterrewrite/Helper.java index f46929d3896fd..17da7e5712be8 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/filterrewrite/Helper.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/filterrewrite/Helper.java @@ -24,7 +24,6 @@ import org.opensearch.index.mapper.DateFieldMapper; import org.opensearch.index.query.DateRangeIncludingNowQuery; import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery; -import org.opensearch.search.approximate.ApproximatePointRangeQuery; import org.opensearch.search.internal.SearchContext; import java.io.IOException; @@ -56,6 +55,7 @@ private Helper() {} queryWrappers.put(FunctionScoreQuery.class, q -> ((FunctionScoreQuery) q).getSubQuery()); queryWrappers.put(DateRangeIncludingNowQuery.class, q -> ((DateRangeIncludingNowQuery) q).getQuery()); queryWrappers.put(IndexOrDocValuesQuery.class, q -> ((IndexOrDocValuesQuery) q).getIndexQuery()); + queryWrappers.put(ApproximateIndexOrDocValuesQuery.class, q -> ((ApproximateIndexOrDocValuesQuery) q).getOriginalQuery()); } /** @@ -125,19 +125,6 @@ public static long[] getDateHistoAggBounds(final SearchContext context, final St final long[] indexBounds = getShardBounds(leaves, fieldName); if (indexBounds == null) return null; return getBoundsWithRangeQuery(prq, fieldName, indexBounds); - } else if (cq instanceof ApproximateIndexOrDocValuesQuery) { - final ApproximateIndexOrDocValuesQuery aiodvq = (ApproximateIndexOrDocValuesQuery) cq; - final long[] indexBounds = getShardBounds(leaves, fieldName); - if (indexBounds == null) return null; - if ((aiodvq.getApproximationQuery() instanceof ApproximatePointRangeQuery)) { - ApproximatePointRangeQuery aprq = (ApproximatePointRangeQuery) aiodvq.getApproximationQuery(); - if (aprq.canApproximate(context)) { - return getBoundsWithRangeQuery(aprq.pointRangeQuery, fieldName, indexBounds); - } - final IndexOrDocValuesQuery iodvq = (IndexOrDocValuesQuery) aiodvq.getOriginalQuery(); - final PointRangeQuery prq = (PointRangeQuery) iodvq.getIndexQuery(); - return getBoundsWithRangeQuery(prq, fieldName, indexBounds); - } } else if (cq instanceof MatchAllDocsQuery) { return getShardBounds(leaves, fieldName); } else if (cq instanceof FieldExistsQuery) { diff --git a/server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java b/server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java index c423ed348116e..6fd019d16a663 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQuery.java @@ -19,15 +19,15 @@ /** * A wrapper around {@link IndexOrDocValuesQuery} that can be used to run approximate queries. - * It delegates to either {@link ApproximateableQuery} or {@link IndexOrDocValuesQuery} based on whether the query can be approximated or not. - * @see ApproximateableQuery + * It delegates to either {@link ApproximateQuery} or {@link IndexOrDocValuesQuery} based on whether the query can be approximated or not. + * @see ApproximateQuery */ public final class ApproximateIndexOrDocValuesQuery extends ApproximateScoreQuery { - private final ApproximateableQuery approximateIndexQuery; + private final ApproximateQuery approximateIndexQuery; private final IndexOrDocValuesQuery indexOrDocValuesQuery; - public ApproximateIndexOrDocValuesQuery(Query indexQuery, ApproximateableQuery approximateIndexQuery, Query dvQuery) { + public ApproximateIndexOrDocValuesQuery(Query indexQuery, ApproximateQuery approximateIndexQuery, Query dvQuery) { super(new IndexOrDocValuesQuery(indexQuery, dvQuery), approximateIndexQuery); this.approximateIndexQuery = approximateIndexQuery; this.indexOrDocValuesQuery = new IndexOrDocValuesQuery(indexQuery, dvQuery); @@ -67,9 +67,11 @@ public int hashCode() { @Override public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { - if (approximateIndexQuery.canApproximate(this.getContext())) { - return approximateIndexQuery.createWeight(searcher, scoreMode, boost); + // it means we haven't called setContext, some internal test might try to call this without setting context, just return IODVQ's + // weight + if (this.resolvedQuery == null) { + return indexOrDocValuesQuery.createWeight(searcher, scoreMode, boost); } - return indexOrDocValuesQuery.createWeight(searcher, scoreMode, boost); + return this.resolvedQuery.createWeight(searcher, scoreMode, boost); } } diff --git a/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java b/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java index 2b7b4f576b240..ec2c249d77793 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java @@ -37,13 +37,11 @@ * An approximate-able version of {@link PointRangeQuery}. It creates an instance of {@link PointRangeQuery} but short-circuits the intersect logic * after {@code size} is hit */ -public abstract class ApproximatePointRangeQuery extends ApproximateableQuery { +public abstract class ApproximatePointRangeQuery extends ApproximateQuery { private int size; private SortOrder sortOrder; - private long[] docCount = { 0 }; - public final PointRangeQuery pointRangeQuery; protected ApproximatePointRangeQuery(String field, byte[] lowerPoint, byte[] upperPoint, int numDims) { @@ -134,7 +132,7 @@ private PointValues.Relation relate(byte[] minPackedValue, byte[] maxPackedValue } } - public PointValues.IntersectVisitor getIntersectVisitor(DocIdSetBuilder result) { + public PointValues.IntersectVisitor getIntersectVisitor(DocIdSetBuilder result, long[] docCount) { return new PointValues.IntersectVisitor() { DocIdSetBuilder.BulkAdder adder; @@ -217,18 +215,21 @@ private boolean checkValidPointValues(PointValues values) throws IOException { return true; } - private void intersectLeft(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) throws IOException { - intersectLeft(visitor, pointTree); + private void intersectLeft(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor, long[] docCount) + throws IOException { + intersectLeft(visitor, pointTree, docCount); assert pointTree.moveToParent() == false; } - private void intersectRight(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) throws IOException { - intersectRight(visitor, pointTree); + private void intersectRight(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor, long[] docCount) + throws IOException { + intersectRight(visitor, pointTree, docCount); assert pointTree.moveToParent() == false; } // custom intersect visitor to walk the left of the tree - public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException { + public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree, long[] docCount) + throws IOException { PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue()); if (docCount[0] > size) { return; @@ -242,7 +243,7 @@ public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.Poin // we have sufficient doc count. We first move down and then move to the left child if (pointTree.moveToChild() && docCount[0] < size) { do { - intersectLeft(visitor, pointTree); + intersectLeft(visitor, pointTree, docCount); } while (pointTree.moveToSibling() && docCount[0] < size); pointTree.moveToParent(); } else { @@ -257,7 +258,7 @@ public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.Poin // through and do full filtering: if (pointTree.moveToChild() && docCount[0] < size) { do { - intersectLeft(visitor, pointTree); + intersectLeft(visitor, pointTree, docCount); } while (pointTree.moveToSibling() && docCount[0] < size); pointTree.moveToParent(); } else { @@ -275,7 +276,8 @@ public void intersectLeft(PointValues.IntersectVisitor visitor, PointValues.Poin } // custom intersect visitor to walk the right of tree - public void intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException { + public void intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree, long[] docCount) + throws IOException { PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue()); if (docCount[0] > size) { return; @@ -288,13 +290,13 @@ public void intersectRight(PointValues.IntersectVisitor visitor, PointValues.Poi case CELL_INSIDE_QUERY: // If the cell is fully inside, we keep moving right as long as the point tree size is over our size requirement if (pointTree.size() > size && docCount[0] < size && moveRight(pointTree)) { - intersectRight(visitor, pointTree); + intersectRight(visitor, pointTree, docCount); pointTree.moveToParent(); } // if point tree size is no longer over, we have to go back one level where it still was over and the intersect left else if (pointTree.size() <= size && docCount[0] < size) { pointTree.moveToParent(); - intersectLeft(visitor, pointTree); + intersectLeft(visitor, pointTree, docCount); } // if we've reached leaf, it means out size is under the size of the leaf, we can just collect all docIDs else { @@ -307,13 +309,13 @@ else if (pointTree.size() <= size && docCount[0] < size) { case CELL_CROSSES_QUERY: // If the cell is fully inside, we keep moving right as long as the point tree size is over our size requirement if (pointTree.size() > size && docCount[0] < size && moveRight(pointTree)) { - intersectRight(visitor, pointTree); + intersectRight(visitor, pointTree, docCount); pointTree.moveToParent(); } // if point tree size is no longer over, we have to go back one level where it still was over and the intersect left else if (pointTree.size() <= size && docCount[0] < size) { pointTree.moveToParent(); - intersectLeft(visitor, pointTree); + intersectLeft(visitor, pointTree, docCount); } // if we've reached leaf, it means out size is under the size of the leaf, we can just collect all doc values else { @@ -335,6 +337,7 @@ public boolean moveRight(PointValues.PointTree pointTree) throws IOException { @Override public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { LeafReader reader = context.reader(); + long[] docCount = { 0 }; PointValues values = reader.getPointValues(pointRangeQuery.getField()); if (checkValidPointValues(values) == false) { @@ -348,12 +351,12 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti return new ScorerSupplier() { final DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, pointRangeQuery.getField()); - final PointValues.IntersectVisitor visitor = getIntersectVisitor(result); + final PointValues.IntersectVisitor visitor = getIntersectVisitor(result, docCount); long cost = -1; @Override public Scorer get(long leadCost) throws IOException { - intersectLeft(values.getPointTree(), visitor); + intersectLeft(values.getPointTree(), visitor, docCount); DocIdSetIterator iterator = result.build().iterator(); return new ConstantScoreScorer(weight, score(), scoreMode, iterator); } @@ -376,12 +379,12 @@ public long cost() { return new ScorerSupplier() { final DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, pointRangeQuery.getField()); - final PointValues.IntersectVisitor visitor = getIntersectVisitor(result); + final PointValues.IntersectVisitor visitor = getIntersectVisitor(result, docCount); long cost = -1; @Override public Scorer get(long leadCost) throws IOException { - intersectRight(values.getPointTree(), visitor); + intersectRight(values.getPointTree(), visitor, docCount); DocIdSetIterator iterator = result.build().iterator(); return new ConstantScoreScorer(weight, score(), scoreMode, iterator); } @@ -432,7 +435,7 @@ public boolean canApproximate(SearchContext context) { if (!(context.query() instanceof ApproximateIndexOrDocValuesQuery)) { return false; } - this.setSize(Math.max(context.size(), context.trackTotalHitsUpTo())); + this.setSize(Math.max(context.from() + context.size(), context.trackTotalHitsUpTo())); if (context.request() != null && context.request().source() != null) { FieldSortBuilder primarySortField = FieldSortBuilder.getPrimaryFieldSortOrNull(context.request().source()); if (primarySortField != null diff --git a/server/src/main/java/org/opensearch/search/approximate/ApproximateableQuery.java b/server/src/main/java/org/opensearch/search/approximate/ApproximateQuery.java similarity index 91% rename from server/src/main/java/org/opensearch/search/approximate/ApproximateableQuery.java rename to server/src/main/java/org/opensearch/search/approximate/ApproximateQuery.java index d65b844bfe70f..0e6faf396b671 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximateableQuery.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximateQuery.java @@ -14,7 +14,7 @@ /** * Abstract class that can be inherited by queries that can be approximated. Queries should implement {@link #canApproximate(SearchContext)} to specify conditions on when they can be approximated */ -public abstract class ApproximateableQuery extends Query { +public abstract class ApproximateQuery extends Query { protected abstract boolean canApproximate(SearchContext context); diff --git a/server/src/main/java/org/opensearch/search/approximate/ApproximateScoreQuery.java b/server/src/main/java/org/opensearch/search/approximate/ApproximateScoreQuery.java index bfe6fc2c59c40..5fdbb60184e40 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximateScoreQuery.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximateScoreQuery.java @@ -19,18 +19,17 @@ import java.io.IOException; /** - * Base class for a query that can be approximated. - * + * Entry-point for the approximation framework. * This class is heavily inspired by {@link org.apache.lucene.search.IndexOrDocValuesQuery}. It acts as a wrapper that consumer two queries, a regular query and an approximate version of the same. By default, it executes the regular query and returns {@link Weight#scorer} for the original query. At run-time, depending on certain constraints, we can re-write the {@code Weight} to use the approximate weight instead. */ public class ApproximateScoreQuery extends Query { private final Query originalQuery; - private final ApproximateableQuery approximationQuery; + private final ApproximateQuery approximationQuery; - private SearchContext context; + protected Query resolvedQuery; - public ApproximateScoreQuery(Query originalQuery, ApproximateableQuery approximationQuery) { + public ApproximateScoreQuery(Query originalQuery, ApproximateQuery approximationQuery) { this.originalQuery = originalQuery; this.approximationQuery = approximationQuery; } @@ -39,24 +38,23 @@ public Query getOriginalQuery() { return originalQuery; } - public ApproximateableQuery getApproximationQuery() { + public ApproximateQuery getApproximationQuery() { return approximationQuery; } @Override public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { - if (approximationQuery.canApproximate(context)) { - return approximationQuery.createWeight(searcher, scoreMode, boost); + if (resolvedQuery == null) { + throw new IllegalStateException("Cannot create weight without setContext being called"); } - return originalQuery.createWeight(searcher, scoreMode, boost); + return resolvedQuery.createWeight(searcher, scoreMode, boost); } public void setContext(SearchContext context) { - this.context = context; - }; - - public SearchContext getContext() { - return context; + if (resolvedQuery != null) { + throw new IllegalStateException("Query already resolved, duplicate call to setContext"); + } + resolvedQuery = approximationQuery.canApproximate(context) ? approximationQuery : originalQuery; }; @Override diff --git a/server/src/test/java/org/opensearch/index/mapper/DateFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/DateFieldTypeTests.java index 7ffc112dac3ee..fe3866308f504 100644 --- a/server/src/test/java/org/opensearch/index/mapper/DateFieldTypeTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/DateFieldTypeTests.java @@ -43,7 +43,6 @@ import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery; -import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.opensearch.Version; @@ -211,16 +210,7 @@ public void testTermQuery() { String date = "2015-10-12T14:10:55"; long instant = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date)).toInstant().toEpochMilli(); Query expected = new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - "field", - pack(new long[] { instant }).bytes, - pack(new long[] { instant + 999 }).bytes, - new long[] { instant }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery("field", instant, instant + 999), new ApproximatePointRangeQuery( "field", pack(new long[] { instant }).bytes, @@ -281,16 +271,7 @@ public void testRangeQuery() throws IOException { long instant1 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date1)).toInstant().toEpochMilli(); long instant2 = DateFormatters.from(DateFieldMapper.getDefaultDateTimeFormatter().parse(date2)).toInstant().toEpochMilli() + 999; Query expected = new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - "field", - pack(new long[] { instant1 }).bytes, - pack(new long[] { instant2 }).bytes, - new long[] { instant1 }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery("field", instant1, instant2), new ApproximatePointRangeQuery( "field", pack(new long[] { instant1 }).bytes, @@ -313,16 +294,7 @@ protected String toString(int dimension, byte[] value) { instant2 = instant1 + 100; expected = new DateRangeIncludingNowQuery( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - "field", - pack(new long[] { instant1 }).bytes, - pack(new long[] { instant2 }).bytes, - new long[] { instant1 }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery("field", instant1, instant2), new ApproximatePointRangeQuery( "field", pack(new long[] { instant1 }).bytes, @@ -399,16 +371,7 @@ public void testRangeQueryWithIndexSort() { instant1, instant2, new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - "field", - pack(new long[] { instant1 }).bytes, - pack(new long[] { instant2 }).bytes, - new long[] { instant1 }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery("field", instant1, instant2), new ApproximatePointRangeQuery( "field", pack(new long[] { instant1 }).bytes, diff --git a/server/src/test/java/org/opensearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java index 7987217c02e64..46b4acc5d5a20 100644 --- a/server/src/test/java/org/opensearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java @@ -184,16 +184,11 @@ public void testDateRangeQuery() throws Exception { ); assertEquals( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( + LongPoint.newRangeQuery( DATE_FIELD_NAME, - pack(new long[] { parser.parse(lowerBoundExact, () -> 0).toEpochMilli() }).bytes, - pack(new long[] { parser.parse(upperBoundExact, () -> 0).toEpochMilli() }).bytes, - new long[] { parser.parse(lowerBoundExact, () -> 0).toEpochMilli() }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + parser.parse(lowerBoundExact, () -> 0).toEpochMilli(), + parser.parse(upperBoundExact, () -> 0).toEpochMilli() + ), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { parser.parse(lowerBoundExact, () -> 0).toEpochMilli() }).bytes, diff --git a/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java index 3c2c9d6fd4c25..a30e28785b0e1 100644 --- a/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java @@ -52,7 +52,6 @@ import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.NormsFieldExistsQuery; import org.apache.lucene.search.PhraseQuery; -import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.RegexpQuery; @@ -867,16 +866,7 @@ public void testToQueryDateWithTimeZone() throws Exception { private ApproximateIndexOrDocValuesQuery calculateExpectedDateQuery(long lower, long upper) { return new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - DATE_FIELD_NAME, - pack(new long[] { lower }).bytes, - pack(new long[] { upper }).bytes, - new long[] { lower }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery(DATE_FIELD_NAME, lower, upper), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { lower }).bytes, diff --git a/server/src/test/java/org/opensearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/RangeQueryBuilderTests.java index 10863a4405453..cd08af3c5e3b1 100644 --- a/server/src/test/java/org/opensearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/RangeQueryBuilderTests.java @@ -56,7 +56,7 @@ import org.opensearch.index.mapper.MapperService; import org.opensearch.search.approximate.ApproximateIndexOrDocValuesQuery; import org.opensearch.search.approximate.ApproximatePointRangeQuery; -import org.opensearch.search.approximate.ApproximateableQuery; +import org.opensearch.search.approximate.ApproximateQuery; import org.opensearch.test.AbstractQueryTestCase; import org.joda.time.DateTime; import org.joda.time.chrono.ISOChronology; @@ -190,7 +190,7 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, } else if (expectedFieldName.equals(DATE_FIELD_NAME)) { assertThat(query, instanceOf(ApproximateIndexOrDocValuesQuery.class)); Query approximationQuery = ((ApproximateIndexOrDocValuesQuery) query).getApproximationQuery(); - assertThat(approximationQuery, instanceOf(ApproximateableQuery.class)); + assertThat(approximationQuery, instanceOf(ApproximateQuery.class)); Query originalQuery = ((ApproximateIndexOrDocValuesQuery) query).getOriginalQuery(); assertThat(originalQuery, instanceOf(IndexOrDocValuesQuery.class)); MapperService mapperService = context.getMapperService(); @@ -243,16 +243,7 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, } assertEquals( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - DATE_FIELD_NAME, - pack(new long[] { minLong }).bytes, - pack(new long[] { maxLong }).bytes, - new long[] { minLong }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery(DATE_FIELD_NAME, minLong, maxLong), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { minLong }).bytes, @@ -334,23 +325,14 @@ public void testDateRangeQueryFormat() throws IOException { Query parsedQuery = parseQuery(query).toQuery(createShardContext()); assertThat(parsedQuery, instanceOf(ApproximateIndexOrDocValuesQuery.class)); Query approximationQuery = ((ApproximateIndexOrDocValuesQuery) parsedQuery).getApproximationQuery(); - assertThat(approximationQuery, instanceOf(ApproximateableQuery.class)); + assertThat(approximationQuery, instanceOf(ApproximateQuery.class)); Query originalQuery = ((ApproximateIndexOrDocValuesQuery) parsedQuery).getOriginalQuery(); assertThat(originalQuery, instanceOf(IndexOrDocValuesQuery.class)); long lower = DateTime.parse("2012-01-01T00:00:00.000+00").getMillis(); long upper = DateTime.parse("2030-01-01T00:00:00.000+00").getMillis() - 1; assertEquals( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - DATE_FIELD_NAME, - pack(new long[] { lower }).bytes, - pack(new long[] { upper }).bytes, - new long[] { lower }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery(DATE_FIELD_NAME, lower, upper), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { lower }).bytes, @@ -400,16 +382,7 @@ public void testDateRangeBoundaries() throws IOException { long upper = DateTime.parse("2014-12-08T23:59:59.999+00").getMillis(); assertEquals( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - DATE_FIELD_NAME, - pack(new long[] { lower }).bytes, - pack(new long[] { upper }).bytes, - new long[] { lower }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery(DATE_FIELD_NAME, lower, upper), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { lower }).bytes, @@ -444,16 +417,7 @@ protected String toString(int dimension, byte[] value) { upper = DateTime.parse("2014-12-08T00:00:00.000+00").getMillis() - 1; assertEquals( new ApproximateIndexOrDocValuesQuery( - new PointRangeQuery( - DATE_FIELD_NAME, - pack(new long[] { lower }).bytes, - pack(new long[] { upper }).bytes, - new long[] { lower }.length - ) { - protected String toString(int dimension, byte[] value) { - return Long.toString(LongPoint.decodeDimension(value, 0)); - } - }, + LongPoint.newRangeQuery(DATE_FIELD_NAME, lower, upper), new ApproximatePointRangeQuery( DATE_FIELD_NAME, pack(new long[] { lower }).bytes, @@ -491,7 +455,7 @@ public void testDateRangeQueryTimezone() throws IOException { parsedQuery = ((DateRangeIncludingNowQuery) parsedQuery).getQuery(); assertThat(parsedQuery, instanceOf(ApproximateIndexOrDocValuesQuery.class)); parsedQuery = ((ApproximateIndexOrDocValuesQuery) parsedQuery).getApproximationQuery(); - assertThat(parsedQuery, instanceOf(ApproximateableQuery.class)); + assertThat(parsedQuery, instanceOf(ApproximateQuery.class)); // TODO what else can we assert query = "{\n" diff --git a/server/src/test/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQueryTests.java b/server/src/test/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQueryTests.java index 763170e966f82..19afe6981d8f5 100644 --- a/server/src/test/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQueryTests.java +++ b/server/src/test/java/org/opensearch/search/approximate/ApproximateIndexOrDocValuesQueryTests.java @@ -14,7 +14,6 @@ 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; @@ -51,18 +50,9 @@ 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)); - } - }; + Query indexQuery = LongPoint.newRangeQuery("test-index", l, u); - ApproximateableQuery approximateIndexQuery = new ApproximatePointRangeQuery( + ApproximateQuery approximateIndexQuery = new ApproximatePointRangeQuery( "test-index", pack(new long[] { l }).bytes, pack(new long[] { u }).bytes, @@ -89,7 +79,7 @@ protected String toString(int dimension, byte[] value) { // we only get weight since we're expecting to call IODVQ assertFalse(weight instanceof ConstantScoreWeight); - ApproximateableQuery approximateIndexQueryCanApproximate = new ApproximatePointRangeQuery( + ApproximateQuery approximateIndexQueryCanApproximate = new ApproximatePointRangeQuery( "test-index", pack(new long[] { l }).bytes, pack(new long[] { u }).bytes, diff --git a/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java b/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java index 84657986fe1c9..dd683d28f00f7 100644 --- a/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java +++ b/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java @@ -15,7 +15,6 @@ 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; @@ -61,11 +60,7 @@ 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)); - } - }; + Query query = LongPoint.newRangeQuery("point", lower, upper); IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(approximateQuery, 10); TopDocs topDocs1 = searcher.search(query, 10); @@ -224,11 +219,8 @@ 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)); - } - }; + Query query = LongPoint.newRangeQuery("point", lower, upper); + ; IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(approximateQuery, 10); TopDocs topDocs1 = searcher.search(query, 10); @@ -279,11 +271,8 @@ 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)); - } - }; + Query query = LongPoint.newRangeQuery("point", lower, upper); + ; IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(approximateQuery, 10); TopDocs topDocs1 = searcher.search(query, 10); diff --git a/server/src/test/java/org/opensearch/search/approximate/ApproximateScoreQueryTests.java b/server/src/test/java/org/opensearch/search/approximate/ApproximateScoreQueryTests.java index 83fa4a500de76..8bd0fc058d2d4 100644 --- a/server/src/test/java/org/opensearch/search/approximate/ApproximateScoreQueryTests.java +++ b/server/src/test/java/org/opensearch/search/approximate/ApproximateScoreQueryTests.java @@ -42,7 +42,7 @@ protected String toString(int dimension, byte[] value) { } }; - ApproximateableQuery approximateQuery = new ApproximatePointRangeQuery( + ApproximateQuery approximateQuery = new ApproximatePointRangeQuery( "test-index", pack(new long[] { l }).bytes, pack(new long[] { u }).bytes,