From dev-return-359542-archive-asf-public=cust-asf.ponee.io@lucene.apache.org Tue Jun 18 15:03:03 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 32AF018066B for ; Tue, 18 Jun 2019 17:03:03 +0200 (CEST) Received: (qmail 87062 invoked by uid 500); 18 Jun 2019 15:03:01 -0000 Mailing-List: contact dev-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@lucene.apache.org Delivered-To: mailing list dev@lucene.apache.org Received: (qmail 87052 invoked by uid 99); 18 Jun 2019 15:03:01 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 Jun 2019 15:03:01 +0000 From: GitBox To: dev@lucene.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. Message-ID: <156087017642.27267.11824005662764090408.gitbox@gitbox.apache.org> Date: Tue, 18 Jun 2019 15:02:56 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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: + *
+ *   String field = "field";
+ *   long lowerValue = 0, long upperValue = 10;
+ *   Query fallbackQuery = LongPoint.newRangeQuery(field, lowerValue, upperValue);
+ *   Query rangeQuery = new IndexSortSortedNumericDocValuesRangeQuery(
+ *       field, lowerValue, upperValue, fallbackQuery);
+ * 
+ * + * @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