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 67c221cc8355f..234b40d4e40e3 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DateFieldMapper.java @@ -490,22 +490,22 @@ public Query rangeQuery( private Query createPointRangeQuery(long l, long u) { return new ApproximateScoreQuery( - 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)); - } - }, - 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)); - } + 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)); } + }, + 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)); + } + } ); } diff --git a/server/src/main/java/org/opensearch/search/approximate/ApproximateConstantScoreWeight.java b/server/src/main/java/org/opensearch/search/approximate/ApproximateConstantScoreWeight.java index f2c41619a82cc..aec84499a9f7b 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximateConstantScoreWeight.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximateConstantScoreWeight.java @@ -21,7 +21,7 @@ protected ApproximateConstantScoreWeight(Query query, float score) { super(query, score); } - protected abstract long intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException; + protected abstract long intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException; protected abstract long intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException; 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 c2b93d9680a9c..49949e935e7e7 100644 --- a/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java +++ b/server/src/main/java/org/opensearch/search/approximate/ApproximatePointRangeQuery.java @@ -12,7 +12,6 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PointValues; import org.apache.lucene.search.ConstantScoreScorer; -import org.apache.lucene.search.ConstantScoreWeight; import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PointRangeQuery; @@ -30,8 +29,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Objects; -import java.util.function.BiFunction; -import java.util.function.Predicate; /** * An approximate-able version of {@link PointRangeQuery}. It creates an instance of {@link PointRangeQuery} but short-circuits the intersect logic @@ -90,7 +87,6 @@ public void visit(QueryVisitor visitor) { public final ApproximateConstantScoreWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { Weight pointRangeQueryWeight = pointRangeQuery.createWeight(searcher, scoreMode, boost); - return new ApproximateConstantScoreWeight(this, boost) { private final ArrayUtil.ByteArrayComparator comparator = ArrayUtil.getUnsignedComparator(pointRangeQuery.getBytesPerDim()); @@ -204,21 +200,18 @@ private boolean checkValidPointValues(PointValues values) throws IOException { return true; } - private void intersectLeft(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) - throws IOException { + private void intersectLeft(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) throws IOException { intersectLeft(visitor, pointTree); assert pointTree.moveToParent() == false; } - private void intersectRight(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) - throws IOException { + private void intersectRight(PointValues.PointTree pointTree, PointValues.IntersectVisitor visitor) throws IOException { intersectRight(visitor, pointTree); assert pointTree.moveToParent() == false; } // custom intersect visitor to walk the left of the tree - public long intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) - throws IOException { + public long intersectLeft(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException { PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue()); if (docCount[0] >= size) { return 0; @@ -269,8 +262,7 @@ public long intersectLeft(PointValues.IntersectVisitor visitor, PointValues.Poin } // custom intersect visitor to walk the right of tree - public long intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) - throws IOException { + public long intersectRight(PointValues.IntersectVisitor visitor, PointValues.PointTree pointTree) throws IOException { PointValues.Relation r = visitor.compare(pointTree.getMinPackedValue(), pointTree.getMaxPackedValue()); if (docCount[0] >= size) { return 0; @@ -316,7 +308,8 @@ public long intersectRight(PointValues.IntersectVisitor visitor, PointValues.Poi default: throw new IllegalArgumentException("Unreachable code"); } - // docCount can be updated by the local visitor, so we ensure that we return docCount after pointTree.visitDocValues(visitor) + // docCount can be updated by the local visitor, so we ensure that we return docCount after + // pointTree.visitDocValues(visitor) return docCount[0] > 0 ? docCount[0] : 0; } 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 77442cc95c5d8..53a4fd9509102 100644 --- a/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java @@ -862,14 +862,8 @@ public void testToQueryDateWithTimeZone() throws Exception { long upper = 86399999; // 1970-01-01T23:59:59.999 UTC assertEquals(calculateExpectedDateQuery(lower, upper), query); int msPerHour = 3600000; - assertEquals( - calculateExpectedDateQuery(lower - msPerHour, upper - msPerHour), - qsq.timeZone("+01:00").toQuery(context) - ); - assertEquals( - calculateExpectedDateQuery(lower + msPerHour, upper + msPerHour), - qsq.timeZone("-01:00").toQuery(context) - ); + assertEquals(calculateExpectedDateQuery(lower - msPerHour, upper - msPerHour), qsq.timeZone("+01:00").toQuery(context)); + assertEquals(calculateExpectedDateQuery(lower + msPerHour, upper + msPerHour), qsq.timeZone("-01:00").toQuery(context)); } private IndexOrDocValuesQuery calculateExpectedDateQuery(long lower, long upper) { 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 87bf5568c1c01..6e3f87362dc94 100644 --- a/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java +++ b/server/src/test/java/org/opensearch/search/approximate/ApproximatePointRangeQueryTests.java @@ -20,11 +20,8 @@ import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreMode; -import org.apache.lucene.search.Scorer; -import org.apache.lucene.search.ScorerSupplier; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TotalHits; -import org.apache.lucene.search.Weight; import org.apache.lucene.store.Directory; import org.apache.lucene.tests.index.RandomIndexWriter; import org.apache.lucene.util.DocIdSetBuilder; @@ -32,11 +29,8 @@ import org.opensearch.test.OpenSearchTestCase; import java.io.IOException; -import java.lang.reflect.Method; import static org.apache.lucene.document.LongPoint.pack; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.*; public class ApproximatePointRangeQueryTests extends OpenSearchTestCase { @@ -106,12 +100,7 @@ public void testApproximateRangeWithDefaultSize() throws IOException { try { long lower = 0; long upper = 1000; - Query approximateQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims - ) { + 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)); } @@ -149,13 +138,7 @@ public void testApproximateRangeWithSizeUnderDefault() throws IOException { try { long lower = 0; long upper = 45; - Query approximateQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims, - 10 - ) { + 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)); } @@ -193,11 +176,11 @@ public void testApproximateRangeWithSizeOverDefault() throws IOException { long lower = 0; long upper = 12000; Query approximateQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims, - 11_000 + "point", + pack(lower).bytes, + pack(upper).bytes, + dims, + 11_000 ) { protected String toString(int dimension, byte[] value) { return Long.toString(LongPoint.decodeDimension(value, 0)); @@ -285,12 +268,12 @@ public void testApproximateRangeShortCircuitAscSort() throws IOException { long lower = 0; long upper = 20; Query approximateQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims, - 10, - SortOrder.ASC + "point", + pack(lower).bytes, + pack(upper).bytes, + dims, + 10, + SortOrder.ASC ) { protected String toString(int dimension, byte[] value) { return Long.toString(LongPoint.decodeDimension(value, 0)); @@ -346,18 +329,22 @@ public void testIntersectLeft() throws IOException { long lower = 0; long upper = 20; ApproximatePointRangeQuery approximatePointRangeQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims, - 10 + "point", + pack(lower).bytes, + pack(upper).bytes, + dims, + 10 ) { protected String toString(int dimension, byte[] value) { return Long.toString(LongPoint.decodeDimension(value, 0)); } }; IndexSearcher searcher = new IndexSearcher(reader); - ApproximateConstantScoreWeight approximatePointRangeQueryWeight = approximatePointRangeQuery.createWeight(searcher, ScoreMode.TOP_SCORES, 1.0F); + ApproximateConstantScoreWeight approximatePointRangeQueryWeight = approximatePointRangeQuery.createWeight( + searcher, + ScoreMode.TOP_SCORES, + 1.0F + ); LeafReaderContext lrc = reader.leaves().get(0); PointValues values = lrc.reader().getPointValues("point"); final DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, "point"); @@ -396,18 +383,22 @@ public void testIntersectRight() throws IOException { long lower = 0; long upper = 20; ApproximatePointRangeQuery approximatePointRangeQuery = new ApproximatePointRangeQuery( - "point", - pack(lower).bytes, - pack(upper).bytes, - dims, - 10 + "point", + pack(lower).bytes, + pack(upper).bytes, + dims, + 10 ) { protected String toString(int dimension, byte[] value) { return Long.toString(LongPoint.decodeDimension(value, 0)); } }; IndexSearcher searcher = new IndexSearcher(reader); - ApproximateConstantScoreWeight approximatePointRangeQueryWeight = approximatePointRangeQuery.createWeight(searcher, ScoreMode.TOP_SCORES, 1.0F); + ApproximateConstantScoreWeight approximatePointRangeQueryWeight = approximatePointRangeQuery.createWeight( + searcher, + ScoreMode.TOP_SCORES, + 1.0F + ); LeafReaderContext lrc = reader.leaves().get(0); PointValues values = lrc.reader().getPointValues("point"); final DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, "point");