lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [lucene-solr] jtibshirani commented on a change in pull request #715: LUCENE-7714 Add a range query that takes advantage of index sorting.
Date Tue, 18 Jun 2019 15:02:56 GMT
jtibshirani commented on a change in pull request #715: LUCENE-7714 Add a range query that
takes advantage of index sorting.
URL: https://github.com/apache/lucene-solr/pull/715#discussion_r294872999
 
 

 ##########
 File path: lucene/sandbox/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java
 ##########
 @@ -0,0 +1,305 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import com.carrotsearch.randomizedtesting.annotations.Repeat;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SortedNumericDocValues;
+
+/**
+ * A range query that can take advantage of the fact that the index is sorted to speed up
+ * execution. If the index is sorted on the same field as the query, it performs binary
+ * search on the field's {@link SortedNumericDocValues} to find the documents at the lower
+ * and upper ends of the range.
+ * 
+ * This optimized execution strategy is only used if the following conditions hold:
+ * - The index is sorted, and its primary sort is on the same field as the query.
+ * - The segments must have at most one field value per document (otherwise we cannot easily
+ * determine the matching document IDs through a binary search).
+ *
+ * If any of these conditions isn't met, the search is delegated to {@code fallbackQuery}.
+ *
+ * This fallback must be an equivalent range query -- it should produce the same documents
and give
+ * constant scores. As an example, an {@link IndexSortSortedNumericDocValuesRangeQuery} might
be
+ * constructed as follows:
+ * <pre class="prettyprint">
+ *   String field = "field";
+ *   long lowerValue = 0, long upperValue = 10;
+ *   Query fallbackQuery = LongPoint.newRangeQuery(field, lowerValue, upperValue);
+ *   Query rangeQuery = new IndexSortSortedNumericDocValuesRangeQuery(
+ *       field, lowerValue, upperValue, fallbackQuery);
+ * </pre>
+ *
+ * @lucene.experimental
+ */
+public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
+
+  private final String field;
+  private final long lowerValue;
+  private final long upperValue;
+  private final Query fallbackQuery;
+
+  /**
+   * Creates a new {@link IndexSortSortedNumericDocValuesRangeQuery}.
+   *
+   * @param field The field name.
+   * @param lowerValue The lower end of the range (inclusive).
+   * @param upperValue The upper end of the range (exclusive).
+   * @param fallbackQuery A query to fall back to if the optimization cannot be applied.
+      */
+  public IndexSortSortedNumericDocValuesRangeQuery(String field,
+                                                   long lowerValue,
+                                                   long upperValue,
+                                                   Query fallbackQuery) {
+    this.field = Objects.requireNonNull(field);
+    this.lowerValue = lowerValue;
+    this.upperValue = upperValue;
+    this.fallbackQuery = fallbackQuery;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+    IndexSortSortedNumericDocValuesRangeQuery that = (IndexSortSortedNumericDocValuesRangeQuery)
o;
+    return lowerValue == that.lowerValue &&
+        upperValue == that.upperValue &&
+        Objects.equals(field, that.field) &&
+        Objects.equals(fallbackQuery, that.fallbackQuery);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(field, lowerValue, upperValue, fallbackQuery);
+  }
+
+  @Override
+  public void visit(QueryVisitor visitor) {
+    if (visitor.acceptField(field)) {
+      visitor.visitLeaf(this);
+      fallbackQuery.visit(visitor);
+    }
+  }
+
+  @Override
+  public String toString(String field) {
+    StringBuilder b = new StringBuilder();
+    if (this.field.equals(field) == false) {
+      b.append(this.field).append(":");
+    }
+    return b
+        .append("[")
+        .append(lowerValue)
+        .append(" TO ")
+        .append(upperValue)
+        .append("]")
+        .toString();
+  }
+
+  @Override
+  public Query rewrite(IndexReader reader) throws IOException {
+    if (lowerValue == Long.MIN_VALUE && upperValue == Long.MAX_VALUE) {
+      return new DocValuesFieldExistsQuery(field);
+    }
+
+    Query rewrittenFallback = fallbackQuery.rewrite(reader);
+    if (rewrittenFallback == fallbackQuery) {
+      return this;
+    } else {
+      return new IndexSortSortedNumericDocValuesRangeQuery(
+          field, lowerValue, upperValue, fallbackQuery);
+    }
+  }
+
+  @Override
+  public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws
IOException {
+    Weight fallbackWeight = fallbackQuery.createWeight(searcher, scoreMode, boost);
+
+    return new ConstantScoreWeight(this, boost) {
+      @Override
+      public Scorer scorer(LeafReaderContext context) throws IOException {
+        SortedNumericDocValues values = context.reader().getSortedNumericDocValues(field);
 
 Review comment:
   👍 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Mime
View raw message