atris 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_r292760051
 
 

 ##########
 File path: 
lucene/core/src/java/org/apache/lucene/document/IndexSortDocValuesRangeQuery.java
 ##########
 @@ -0,0 +1,279 @@
+/*
+ * 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.document;
+
+import java.io.IOException;
+import java.util.Objects;
+
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.ConstantScoreScorer;
+import org.apache.lucene.search.ConstantScoreWeight;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.LeafFieldComparator;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.QueryVisitor;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.Weight;
+
+/**
+ * A range query that takes advantage of the fact that the index is sorted to 
speed up execution.
+ * It assumes that the index is sorted on the same field as the query, and 
performs binary search
+ * on the field's doc values to find the documents at the lower and upper ends 
of the range.
+ *
+ * This optimized execution strategy can only be used if all of these 
conditions hold:
+ *   - The index is sorted, and its primary sort is on the same field as the 
query.
+ *   - The provided field has {@link SortedNumericDocValues}.
+ *   - The segments must have at most one field value per document (otherwise 
we cannot easily
+ *     determine the matching document IDs through a binary search).
+ */
+public final class IndexSortDocValuesRangeQuery extends Query {
+
+  private final String field;
+  private final long lowerValue;
+  private final long upperValue;
+
+  /**
+   * Creates a new {@link IndexSortDocValuesRangeQuery}.
+   *
+   * @param field The field name.
+   * @param lowerValue The lower end of the range (inclusive).
+   * @param upperValue The upper end of the range (inclusive).
+   */
+  public IndexSortDocValuesRangeQuery(String field,
+                                      long lowerValue,
+                                      long upperValue) {
+    this.field = field;
+    this.lowerValue = lowerValue;
+    this.upperValue = upperValue;
+  }
+
+  @Override
+  public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, 
float boost) {
+    return new ConstantScoreWeight(this, boost) {
+      @Override
+      public Scorer scorer(LeafReaderContext context) throws IOException {
+        LeafReader reader = context.reader();
+
+        Sort indexSort = reader.getMetaData().getSort();
+        if (indexSort == null || indexSort.getSort().length == 0) {
+          throw new IllegalArgumentException("To use 
IndexSortDocValuesRangeQuery, the index must be sorted.");
+        }
+
+        SortField sortField = indexSort.getSort()[0];
+        if (sortField.getField().equals(field) == false) {
+          throw new IllegalArgumentException("To use 
IndexSortDocValuesRangeQuery, the index must be sorted " +
+              "on the same field as the query field.");
+        }
+
+        SortedNumericDocValues sortedDocValues = 
reader.getSortedNumericDocValues(field);
+        NumericDocValues docValues = 
DocValues.unwrapSingleton(sortedDocValues);
 
 Review comment:
   I am curious to understand why we cannot support multiple values here. Is it 
because of duplicate elimination in binary searches?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to