Apache Lucene open-source search software
Remove SortedSetDocValuesSetQuery in favor of TermInSetQuery with DocValuesRewriteMethod (#12175)
Greg Miller569533bd76a1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 6650543ea8d..799b4e8e4d2 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -132,6 +132,8 @@ Improvementswas essentially a clone of MultiTermQuery#CONSTANT_SCORE_REWRITE). It uses the new CONSTANT_SCORE_BLENDED_REWRITEby default, but can be overridden through the constructor. (Greg Miller)+* GITHUB#12175: Remove SortedSetDocValuesSetQuery in favor of TermInSetQuery with DocValuesRewriteMethod. (Greg Miller)+Optimizations---------------------diff --git a/lucene/core/src/java/org/apache/lucene/document/KeywordField.java b/lucene/core/src/java/org/apache/lucene/document/KeywordField.javaindex a8abd83ea29..654c2dab9c4 100644--- a/lucene/core/src/java/org/apache/lucene/document/KeywordField.java+++ b/lucene/core/src/java/org/apache/lucene/document/KeywordField.java@@ -22,6 +22,7 @@ import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.Term;import org.apache.lucene.search.ConstantScoreQuery;import org.apache.lucene.search.IndexOrDocValuesQuery;+import org.apache.lucene.search.MultiTermQuery;import org.apache.lucene.search.Query;import org.apache.lucene.search.SortField;import org.apache.lucene.search.SortedSetSelector;@@ -173,8 +174,9 @@ public class KeywordField extends Field {public static Query newSetQuery(String field, BytesRef... values) {Objects.requireNonNull(field, "field must not be null");Objects.requireNonNull(values, "values must not be null");- return new IndexOrDocValuesQuery(- new TermInSetQuery(field, values), new SortedSetDocValuesSetQuery(field, values.clone()));+ Query indexQuery = new TermInSetQuery(field, values);+ Query dvQuery = new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, field, values);+ return new IndexOrDocValuesQuery(indexQuery, dvQuery);}/**diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedDocValuesField.java b/lucene/core/src/java/org/apache/lucene/document/SortedDocValuesField.javaindex 8f883f4bb24..e9e1b26521d 100644--- a/lucene/core/src/java/org/apache/lucene/document/SortedDocValuesField.java+++ b/lucene/core/src/java/org/apache/lucene/document/SortedDocValuesField.java@@ -18,7 +18,9 @@ package org.apache.lucene.document;import org.apache.lucene.index.DocValuesType;import org.apache.lucene.search.IndexOrDocValuesQuery;+import org.apache.lucene.search.MultiTermQuery;import org.apache.lucene.search.Query;+import org.apache.lucene.search.TermInSetQuery;import org.apache.lucene.util.BytesRef;/**@@ -94,10 +96,10 @@ public class SortedDocValuesField extends Field {** <p><b>NOTE</b>: Such queries cannot efficiently advance to the next match, which makes them* slow if they are not ANDed with a selective query. As a consequence, they are best used wrapped- * in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on points, such as- * {@link BinaryPoint#newSetQuery}.+ * in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on postings, such as+ * {@link TermInSetQuery}.*/public static Query newSlowSetQuery(String field, BytesRef... values) {- return new SortedSetDocValuesSetQuery(field, values.clone());+ return new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, field, values);}}diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesField.java b/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesField.javaindex fce5b45bfce..14eaffe842f 100644--- a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesField.java+++ b/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesField.java@@ -18,7 +18,9 @@ package org.apache.lucene.document;import org.apache.lucene.index.DocValuesType;import org.apache.lucene.search.IndexOrDocValuesQuery;+import org.apache.lucene.search.MultiTermQuery;import org.apache.lucene.search.Query;+import org.apache.lucene.search.TermInSetQuery;import org.apache.lucene.util.BytesRef;/**@@ -98,10 +100,10 @@ public class SortedSetDocValuesField extends Field {** <p><b>NOTE</b>: Such queries cannot efficiently advance to the next match, which makes them* slow if they are not ANDed with a selective query. As a consequence, they are best used wrapped- * in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on points, such as- * {@link BinaryPoint#newSetQuery}.+ * in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on postings, such as+ * {@link TermInSetQuery}.*/public static Query newSlowSetQuery(String field, BytesRef... values) {- return new SortedSetDocValuesSetQuery(field, values.clone());+ return new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, field, values);}}diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.java b/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.javadeleted file mode 100644index 3f0c37cc5ac..00000000000--- a/lucene/core/src/java/org/apache/lucene/document/SortedSetDocValuesSetQuery.java+++ /dev/null@@ -1,215 +0,0 @@-/*- * 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.LeafReaderContext;-import org.apache.lucene.index.PrefixCodedTerms;-import org.apache.lucene.index.PrefixCodedTerms.TermIterator;-import org.apache.lucene.index.SortedDocValues;-import org.apache.lucene.index.SortedSetDocValues;-import org.apache.lucene.index.Term;-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.Query;-import org.apache.lucene.search.QueryVisitor;-import org.apache.lucene.search.ScoreMode;-import org.apache.lucene.search.Scorer;-import org.apache.lucene.search.ScorerSupplier;-import org.apache.lucene.search.TwoPhaseIterator;-import org.apache.lucene.search.Weight;-import org.apache.lucene.util.Accountable;-import org.apache.lucene.util.ArrayUtil;-import org.apache.lucene.util.BytesRef;-import org.apache.lucene.util.LongBitSet;-import org.apache.lucene.util.RamUsageEstimator;--/** Similar to SortedSetDocValuesRangeQuery but for a set */-final class SortedSetDocValuesSetQuery extends Query implements Accountable {- private static final long BASE_RAM_BYTES =- RamUsageEstimator.shallowSizeOfInstance(SortedSetDocValuesSetQuery.class);-- private final String field;- private final PrefixCodedTerms termData;- private final int termDataHashCode; // cached hashcode of termData-- /**- * Sole constructor. Note that {@code terms} will be sorted in-place, so callers should clone the- * array prior to calling if they care to preserve original ordering. This decision was made since- * this is an internal implementation (pkg-private class) and not expected to be used directly by- * users.- */- SortedSetDocValuesSetQuery(String field, BytesRef terms[]) {- this.field = Objects.requireNonNull(field);- Objects.requireNonNull(terms);- ArrayUtil.timSort(terms);- PrefixCodedTerms.Builder builder = new PrefixCodedTerms.Builder();- BytesRef previous = null;- for (BytesRef term : terms) {- if (term.equals(previous) == false) {- builder.add(field, term);- }- previous = term;- }- termData = builder.finish();- termDataHashCode = termData.hashCode();- }-- @Override- public boolean equals(Object other) {- return sameClassAs(other) && equalsTo(getClass().cast(other));- }-- private boolean equalsTo(SortedSetDocValuesSetQuery other) {- // termData might be heavy to compare so check the hash code first- return termDataHashCode == other.termDataHashCode && termData.equals(other.termData);- }-- @Override- public int hashCode() {- return Objects.hash(classHash(), termDataHashCode);- }-- @Override- public String toString(String defaultField) {- StringBuilder builder = new StringBuilder();- boolean first = true;- TermIterator iterator = termData.iterator();- for (BytesRef term = iterator.next(); term != null; term = iterator.next()) {- if (!first) {- builder.append(' ');- }- first = false;- builder.append(new Term(iterator.field(), term).toString());- }-- return builder.toString();- }-- @Override- public long ramBytesUsed() {- return BASE_RAM_BYTES- + RamUsageEstimator.sizeOfObject(field)- + RamUsageEstimator.sizeOfObject(termData);- }-- @Override- public void visit(QueryVisitor visitor) {- if (visitor.acceptField(field)) {- visitor.visitLeaf(this);- }- }-- @Override- public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)- throws IOException {- return new ConstantScoreWeight(this, boost) {-- @Override- public Scorer scorer(LeafReaderContext context) throws IOException {- ScorerSupplier scorerSupplier = scorerSupplier(context);- if (scorerSupplier == null) {- return null;- }- return scorerSupplier.get(Long.MAX_VALUE);- }-- @Override- public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {- final Weight weight = this;- if (context.reader().getFieldInfos().fieldInfo(field) == null) {- return null;- }- final SortedSetDocValues values = DocValues.getSortedSet(context.reader(), field);-- // implement ScorerSupplier, since we do some expensive stuff to make a scorer- return new ScorerSupplier() {- @Override- public Scorer get(long leadCost) throws IOException {- final LongBitSet bits = new LongBitSet(values.getValueCount());- long maxOrd = -1;- TermIterator termIterator = termData.iterator();- for (BytesRef term = termIterator.next(); term != null; term = termIterator.next()) {- final long ord = values.lookupTerm(term);- if (ord >= 0) {- maxOrd = ord;- bits.set(ord);- }- }- // no terms matched in this segment- if (maxOrd < 0) {- return new ConstantScoreScorer(weight, score(), scoreMode, DocIdSetIterator.empty());- }- final SortedDocValues singleton = DocValues.unwrapSingleton(values);- final TwoPhaseIterator iterator;- final long max = maxOrd;- if (singleton != null) {- iterator =- new TwoPhaseIterator(singleton) {- @Override- public boolean matches() throws IOException {- return bits.get(singleton.ordValue());- }-- @Override- public float matchCost() {- return 3; // lookup in a bitset- }- };- } else {- iterator =- new TwoPhaseIterator(values) {- @Override- public boolean matches() throws IOException {- for (int i = 0; i < values.docValueCount(); i++) {- long value = values.nextOrd();- if (value > max) {- return false; // values are sorted, terminate- } else if (bits.get(value)) {- return true;- }- }- return false;- }-- @Override- public float matchCost() {- return 3; // lookup in a bitset- }- };- }- return new ConstantScoreScorer(weight, score(), scoreMode, iterator);- }-- @Override- public long cost() {- return values.cost();- }- };- }-- @Override- public boolean isCacheable(LeafReaderContext ctx) {- return DocValues.isCacheable(ctx, field);- }- };- }-}diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.javaindex 72c1ea1ca91..0d3f487d2c4 100644--- a/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/MultiTermQuery.java@@ -107,6 +107,19 @@ public abstract class MultiTermQuery extends Query {}};+ /**+ * A rewrite method that uses {@link org.apache.lucene.index.DocValuesType#SORTED} / {@link+ * org.apache.lucene.index.DocValuesType#SORTED_SET} doc values to find matching docs through a+ * post-filtering type approach. This will be very slow if used in isolation, but will likely be+ * the most performant option when combined with a sparse query clause. All matching docs are+ * assigned a constant score equal to the query's boost.+ *+ * <p>If you don't have doc values indexed, see the other rewrite methods that rely on postings+ * alone (e.g., {@link #CONSTANT_SCORE_BLENDED_REWRITE}, {@link #SCORING_BOOLEAN_REWRITE}, etc.+ * depending on scoring needs).+ */+ public static final RewriteMethod DOC_VALUES_REWRITE = new DocValuesRewriteMethod();+/*** A rewrite method that first translates each term into {@link BooleanClause.Occur#SHOULD} clause* in a BooleanQuery, and keeps the scores as computed by the query. Note that typically suchdiff --git a/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.javaindex 01739af61b6..b989ac07849 100644--- a/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java@@ -65,9 +65,10 @@ import org.apache.lucene.util.automaton.Operations;* more details, see {@link MultiTermQuery#CONSTANT_SCORE_BLENDED_REWRITE}.** <p>Users may also provide a custom {@link MultiTermQuery.RewriteMethod} to define different- * execution behavior, such as relying on doc values (see: {@link DocValuesRewriteMethod}), or if- * scores are required (see: {@link MultiTermQuery#SCORING_BOOLEAN_REWRITE}). See {@link- * MultiTermQuery} documentation for more rewrite options.+ * execution behavior, such as relying on doc values (see: {@link+ * MultiTermQuery#DOC_VALUES_REWRITE}), or if scores are required (see: {@link+ * MultiTermQuery#SCORING_BOOLEAN_REWRITE}). See {@link MultiTermQuery} documentation for more+ * rewrite options.** <p>NOTE: This query produces scores that are equal to its boost*/