GITHUB-11838 Change API to allow concurrent query rewrite (#11840)

Replace Query#rewrite(IndexReader) with Query#rewrite(IndexSearcher)
This commit is contained in:
Patrick Zhai 2022-10-19 09:49:40 -07:00 committed by GitHub
parent 05971b3315
commit 6cde41c9fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
107 changed files with 344 additions and 350 deletions

View File

@ -34,6 +34,9 @@ API Changes
* GITHUB#11813: Remove Operations.isFinite: the recursive implementation could be problematic
for large automatons (WildcardQuery, PrefixQuery, RegExpQuery, etc). (taroplus, Robert Muir)
* GITHUB#11840: Query rewrite now takes an IndexSearcher instead of IndexReader to enable concurrent
rewriting. (Patrick Zhai)
New Features
---------------------

View File

@ -35,6 +35,7 @@ import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.FuzzyTermsEnum;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
@ -214,7 +215,8 @@ public class NearestFuzzyQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
ScoreTermQueue q = new ScoreTermQueue(MAX_NUM_TERMS);
// load up the list of possible terms
for (FieldVals f : fieldVals) {

View File

@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.Objects;
import org.apache.lucene.index.BinaryDocValues;
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.search.ConstantScoreScorer;
@ -87,8 +86,8 @@ abstract class BinaryRangeFieldRangeQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return super.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return super.rewrite(indexSearcher);
}
private BinaryRangeDocValues getValues(LeafReader reader, String field) throws IOException {

View File

@ -20,7 +20,7 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -79,8 +79,8 @@ class DoubleRangeSlowRangeQuery extends BinaryRangeFieldRangeQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return super.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return super.rewrite(indexSearcher);
}
private static byte[] encodeRanges(double[] min, double[] max) {

View File

@ -31,6 +31,7 @@ import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.DoubleValuesSource;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.similarities.BM25Similarity;
@ -223,7 +224,7 @@ public final class FeatureField extends Field {
abstract Explanation explain(String field, String feature, float w, int freq);
FeatureFunction rewrite(IndexReader reader) throws IOException {
FeatureFunction rewrite(IndexSearcher indexSearcher) throws IOException {
return this;
}
}
@ -340,11 +341,11 @@ public final class FeatureField extends Field {
}
@Override
public FeatureFunction rewrite(IndexReader reader) throws IOException {
public FeatureFunction rewrite(IndexSearcher indexSearcher) throws IOException {
if (pivot != null) {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
float newPivot = computePivotFeatureValue(reader, field, feature);
float newPivot = computePivotFeatureValue(indexSearcher.getIndexReader(), field, feature);
return new SaturationFunction(field, feature, newPivot);
}

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.document.FeatureField.FeatureFunction;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Terms;
@ -50,12 +49,12 @@ final class FeatureQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
FeatureFunction rewritten = function.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
FeatureFunction rewritten = function.rewrite(indexSearcher);
if (function != rewritten) {
return new FeatureQuery(fieldName, featureName, rewritten);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -20,7 +20,7 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -79,8 +79,8 @@ class FloatRangeSlowRangeQuery extends BinaryRangeFieldRangeQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return super.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return super.rewrite(indexSearcher);
}
private static byte[] encodeRanges(float[] min, float[] max) {

View File

@ -19,7 +19,7 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -77,8 +77,8 @@ class IntRangeSlowRangeQuery extends BinaryRangeFieldRangeQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return super.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return super.rewrite(indexSearcher);
}
private static byte[] encodeRanges(int[] min, int[] max) {

View File

@ -20,7 +20,7 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -79,8 +79,8 @@ class LongRangeSlowRangeQuery extends BinaryRangeFieldRangeQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return super.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return super.rewrite(indexSearcher);
}
private static byte[] encodeRanges(long[] min, long[] max) {

View File

@ -19,7 +19,6 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Objects;
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;
@ -89,11 +88,11 @@ abstract class SortedNumericDocValuesRangeQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (lowerValue == Long.MIN_VALUE && upperValue == Long.MAX_VALUE) {
return new FieldExistsQuery(field);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
abstract SortedNumericDocValues getValues(LeafReader reader, String field) throws IOException;

View File

@ -19,7 +19,6 @@ package org.apache.lucene.document;
import java.io.IOException;
import java.util.Objects;
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.SortedDocValues;
@ -103,11 +102,11 @@ abstract class SortedSetDocValuesRangeQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (lowerValue == null && upperValue == null) {
return new FieldExistsQuery(field);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
abstract SortedSetDocValues getValues(LeafReader reader, String field) throws IOException;

View File

@ -19,7 +19,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
@ -268,11 +267,12 @@ public final class BlendedTermQuery extends Query {
}
@Override
public final Query rewrite(IndexReader reader) throws IOException {
public final Query rewrite(IndexSearcher indexSearcher) throws IOException {
final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, this.contexts.length);
for (int i = 0; i < contexts.length; ++i) {
if (contexts[i] == null || contexts[i].wasBuiltFor(reader.getContext()) == false) {
contexts[i] = TermStates.build(reader.getContext(), terms[i], true);
if (contexts[i] == null
|| contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == false) {
contexts[i] = TermStates.build(indexSearcher.getTopReaderContext(), terms[i], true);
}
}
@ -287,7 +287,7 @@ public final class BlendedTermQuery extends Query {
}
for (int i = 0; i < contexts.length; ++i) {
contexts[i] = adjustFrequencies(reader.getContext(), contexts[i], df, ttf);
contexts[i] = adjustFrequencies(indexSearcher.getTopReaderContext(), contexts[i], df, ttf);
}
Query[] termQueries = new Query[terms.length];

View File

@ -30,7 +30,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.BooleanClause.Occur;
/**
@ -193,7 +192,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
// Utility method for rewriting BooleanQuery when scores are not needed.
// This is called from ConstantScoreQuery#rewrite
BooleanQuery rewriteNoScoring(IndexReader reader) throws IOException {
BooleanQuery rewriteNoScoring(IndexSearcher indexSearcher) throws IOException {
boolean actuallyRewritten = false;
BooleanQuery.Builder newQuery =
new BooleanQuery.Builder().setMinimumNumberShouldMatch(getMinimumNumberShouldMatch());
@ -204,7 +203,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
for (BooleanClause clause : clauses) {
Query query = clause.getQuery();
Query rewritten = new ConstantScoreQuery(query).rewrite(reader);
Query rewritten = new ConstantScoreQuery(query).rewrite(indexSearcher);
if (rewritten instanceof ConstantScoreQuery) {
rewritten = ((ConstantScoreQuery) rewritten).getQuery();
}
@ -238,7 +237,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (clauses.size() == 0) {
return new MatchNoDocsQuery("empty BooleanQuery");
}
@ -277,12 +276,12 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
Query rewritten;
if (occur == Occur.FILTER || occur == Occur.MUST_NOT) {
// Clauses that are not involved in scoring can get some extra simplifications
rewritten = new ConstantScoreQuery(query).rewrite(reader);
rewritten = new ConstantScoreQuery(query).rewrite(indexSearcher);
if (rewritten instanceof ConstantScoreQuery) {
rewritten = ((ConstantScoreQuery) rewritten).getQuery();
}
} else {
rewritten = query.rewrite(reader);
rewritten = query.rewrite(indexSearcher);
}
if (rewritten != query || query.getClass() == MatchNoDocsQuery.class) {
// rewrite clause
@ -557,7 +556,7 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
/**
* A {@link Query} wrapper that allows to give a boost to the wrapped query. Boost values that are
@ -73,8 +72,8 @@ public final class BoostQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query rewritten = query.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query rewritten = query.rewrite(indexSearcher);
if (boost == 1f) {
return rewritten;
@ -99,7 +98,7 @@ public final class BoostQuery extends Query {
return new BoostQuery(rewritten, boost);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.util.Bits;
@ -40,8 +39,9 @@ public final class ConstantScoreQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = query.rewrite(indexSearcher);
// Do some extra simplifications that are legal since scores are not needed on the wrapped
// query.
@ -50,7 +50,7 @@ public final class ConstantScoreQuery extends Query {
} else if (rewritten instanceof ConstantScoreQuery) {
rewritten = ((ConstantScoreQuery) rewritten).getQuery();
} else if (rewritten instanceof BooleanQuery) {
rewritten = ((BooleanQuery) rewritten).rewriteNoScoring(reader);
rewritten = ((BooleanQuery) rewritten).rewriteNoScoring(indexSearcher);
}
if (rewritten.getClass() == MatchNoDocsQuery.class) {
@ -70,7 +70,7 @@ public final class ConstantScoreQuery extends Query {
return new ConstantScoreQuery(((BoostQuery) rewritten).getQuery());
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -23,7 +23,6 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
/**
@ -208,11 +207,10 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>
/**
* Optimize our representation and our subqueries representations
*
* @param reader the IndexReader we query
* @return an optimized copy of us (which may not be a copy if there is nothing to optimize)
*/
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (disjuncts.isEmpty()) {
return new MatchNoDocsQuery("empty DisjunctionMaxQuery");
}
@ -232,7 +230,7 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>
boolean actuallyRewritten = false;
List<Query> rewrittenDisjuncts = new ArrayList<>();
for (Query sub : disjuncts) {
Query rewrittenSub = sub.rewrite(reader);
Query rewrittenSub = sub.rewrite(indexSearcher);
actuallyRewritten |= rewrittenSub != sub;
rewrittenDisjuncts.add(rewrittenSub);
}
@ -241,7 +239,7 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>
return new DisjunctionMaxQuery(rewrittenDisjuncts, tieBreakerMultiplier);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.index.SortedSetDocValues;
@ -36,7 +35,7 @@ import org.apache.lucene.util.LongBitSet;
public final class DocValuesRewriteMethod extends MultiTermQuery.RewriteMethod {
@Override
public Query rewrite(IndexReader reader, MultiTermQuery query) {
public Query rewrite(IndexSearcher indexSearcher, MultiTermQuery query) {
return new ConstantScoreQuery(new MultiTermQueryDocValuesWrapper(query));
}

View File

@ -22,7 +22,6 @@ import java.util.Objects;
import java.util.function.DoubleToLongFunction;
import java.util.function.LongToDoubleFunction;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.search.comparators.DoubleComparator;
@ -85,7 +84,7 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
*
* <p>Queries that use DoubleValuesSource objects should call rewrite() during {@link
* Query#createWeight(IndexSearcher, ScoreMode, float)} rather than during {@link
* Query#rewrite(IndexReader)} to avoid IndexReader reference leakage.
* Query#rewrite(IndexSearcher)} to avoid IndexReader reference leakage.
*
* <p>For the same reason, implementations that cache references to the IndexSearcher should
* return a new object from this method.

View File

@ -107,7 +107,8 @@ public class FieldExistsQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
boolean allReadersRewritable = true;
for (LeafReaderContext context : reader.leaves()) {
@ -155,7 +156,7 @@ public class FieldExistsQuery extends Query {
if (allReadersRewritable) {
return new MatchAllDocsQuery();
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -19,7 +19,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
/**
@ -101,9 +100,9 @@ public final class IndexOrDocValuesQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query indexRewrite = indexQuery.rewrite(reader);
Query dvRewrite = dvQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query indexRewrite = indexQuery.rewrite(indexSearcher);
Query dvRewrite = dvQuery.rewrite(indexSearcher);
if (indexRewrite.getClass() == MatchAllDocsQuery.class
|| dvRewrite.getClass() == MatchAllDocsQuery.class) {
return new MatchAllDocsQuery();

View File

@ -764,9 +764,9 @@ public class IndexSearcher {
*/
public Query rewrite(Query original) throws IOException {
Query query = original;
for (Query rewrittenQuery = query.rewrite(reader);
for (Query rewrittenQuery = query.rewrite(this);
rewrittenQuery != query;
rewrittenQuery = query.rewrite(reader)) {
rewrittenQuery = query.rewrite(this)) {
query = rewrittenQuery;
}
query.visit(getNumClausesCheckVisitor());

View File

@ -86,12 +86,12 @@ public class KnnVectorQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
TopDocs[] perLeafResults = new TopDocs[reader.leaves().size()];
Weight filterWeight = null;
if (filter != null) {
IndexSearcher indexSearcher = new IndexSearcher(reader);
BooleanQuery booleanQuery =
new BooleanQuery.Builder()
.add(filter, BooleanClause.Occur.FILTER)

View File

@ -24,7 +24,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
@ -185,7 +184,7 @@ public class MultiPhraseQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (termArrays.length == 0) {
return new MatchNoDocsQuery("empty MultiPhraseQuery");
} else if (termArrays.length == 1) { // optimize one-term case
@ -196,7 +195,7 @@ public class MultiPhraseQuery extends Query {
}
return builder.build();
} else {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
}

View File

@ -18,9 +18,8 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.FilteredTermsEnum; // javadocs
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.SingleTermsEnum; // javadocs
import org.apache.lucene.index.FilteredTermsEnum;
import org.apache.lucene.index.SingleTermsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.index.Terms;
@ -56,7 +55,8 @@ public abstract class MultiTermQuery extends Query {
/** Abstract class that defines how the query is rewritten. */
public abstract static class RewriteMethod {
public abstract Query rewrite(IndexReader reader, MultiTermQuery query) throws IOException;
public abstract Query rewrite(IndexSearcher indexSearcher, MultiTermQuery query)
throws IOException;
/**
* Returns the {@link MultiTermQuery}s {@link TermsEnum}
*
@ -81,7 +81,7 @@ public abstract class MultiTermQuery extends Query {
public static final RewriteMethod CONSTANT_SCORE_REWRITE =
new RewriteMethod() {
@Override
public Query rewrite(IndexReader reader, MultiTermQuery query) {
public Query rewrite(IndexSearcher indexSearcher, MultiTermQuery query) {
return new MultiTermQueryConstantScoreWrapper<>(query);
}
};
@ -279,8 +279,8 @@ public abstract class MultiTermQuery extends Query {
* AttributeSource)}. For example, to rewrite to a single term, return a {@link SingleTermsEnum}
*/
@Override
public final Query rewrite(IndexReader reader) throws IOException {
return rewriteMethod.rewrite(reader, this);
public final Query rewrite(IndexSearcher indexSearcher) throws IOException {
return rewriteMethod.rewrite(indexSearcher, this);
}
/**

View File

@ -18,14 +18,13 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
/**
* This is a {@link PhraseQuery} which is optimized for n-gram phrase query. For example, when you
* query "ABCD" on a 2-gram field, you may want to use NGramPhraseQuery rather than {@link
* PhraseQuery}, because NGramPhraseQuery will {@link #rewrite(IndexReader)} the query to "AB/0
* CD/2", while {@link PhraseQuery} will query "AB/0 BC/1 CD/2" (where term/position).
* PhraseQuery}, because NGramPhraseQuery will {@link Query#rewrite(IndexSearcher)} the query to
* "AB/0 CD/2", while {@link PhraseQuery} will query "AB/0 BC/1 CD/2" (where term/position).
*/
public class NGramPhraseQuery extends Query {
@ -44,7 +43,7 @@ public class NGramPhraseQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Term[] terms = phraseQuery.getTerms();
final int[] positions = phraseQuery.getPositions();
@ -63,7 +62,7 @@ public class NGramPhraseQuery extends Query {
}
if (isOptimizable == false) {
return phraseQuery.rewrite(reader);
return phraseQuery.rewrite(indexSearcher);
}
PhraseQuery.Builder builder = new PhraseQuery.Builder();

View File

@ -25,7 +25,6 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
/**
@ -113,8 +112,8 @@ public class NamedMatches implements Matches {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = in.rewrite(indexSearcher);
if (rewritten != in) {
return new NamedQuery(name, rewritten);
}

View File

@ -24,7 +24,6 @@ import java.util.Objects;
import org.apache.lucene.codecs.lucene90.Lucene90PostingsFormat;
import org.apache.lucene.codecs.lucene90.Lucene90PostingsReader;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
@ -284,7 +283,7 @@ public class PhraseQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (terms.length == 0) {
return new MatchNoDocsQuery("empty PhraseQuery");
} else if (terms.length == 1) {
@ -296,7 +295,7 @@ public class PhraseQuery extends Query {
}
return new PhraseQuery(slop, terms, newPositions);
} else {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
}

View File

@ -17,7 +17,6 @@
package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
/**
* The abstract base class for queries.
@ -77,9 +76,12 @@ public abstract class Query {
* <p>Callers are expected to call <code>rewrite</code> multiple times if necessary, until the
* rewritten query is the same as the original query.
*
* <p>The rewrite process may be able to make use of IndexSearcher's executor and be executed in
* parallel if the executor is provided.
*
* @see IndexSearcher#rewrite(Query)
*/
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return this;
}

View File

@ -86,8 +86,8 @@ public abstract class ScoringRewrite<B> extends TermCollectingRewrite<B> {
public static final RewriteMethod CONSTANT_SCORE_BOOLEAN_REWRITE =
new RewriteMethod() {
@Override
public Query rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
final Query bq = SCORING_BOOLEAN_REWRITE.rewrite(reader, query);
public Query rewrite(IndexSearcher indexSearcher, MultiTermQuery query) throws IOException {
final Query bq = SCORING_BOOLEAN_REWRITE.rewrite(indexSearcher, query);
// strip the scores off
return new ConstantScoreQuery(bq);
}
@ -100,8 +100,9 @@ public abstract class ScoringRewrite<B> extends TermCollectingRewrite<B> {
protected abstract void checkMaxClauseCount(int count) throws IOException;
@Override
public final Query rewrite(final IndexReader reader, final MultiTermQuery query)
public final Query rewrite(IndexSearcher indexSearcher, final MultiTermQuery query)
throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
final B builder = getTopLevelBuilder();
final ParallelArraysTermCollector col = new ParallelArraysTermCollector();
collectTerms(reader, query, col);

View File

@ -29,7 +29,6 @@ import org.apache.lucene.index.Impact;
import org.apache.lucene.index.Impacts;
import org.apache.lucene.index.ImpactsEnum;
import org.apache.lucene.index.ImpactsSource;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.SlowImpactsEnum;
@ -143,7 +142,7 @@ public final class SynonymQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
// optimize zero and non-boosted single term cases
if (terms.length == 0) {
return new BooleanQuery.Builder().build();

View File

@ -24,7 +24,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
@ -113,7 +112,7 @@ public class TermInSetQuery extends Query implements Accountable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final int threshold =
Math.min(BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD, IndexSearcher.getMaxClauseCount());
if (termData.size() <= threshold) {
@ -124,7 +123,7 @@ public class TermInSetQuery extends Query implements Accountable {
}
return new ConstantScoreQuery(bq.build());
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -21,7 +21,6 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.TermStates;
@ -61,12 +60,12 @@ public abstract class TopTermsRewrite<B> extends TermCollectingRewrite<B> {
protected abstract int getMaxSize();
@Override
public final Query rewrite(final IndexReader reader, final MultiTermQuery query)
public final Query rewrite(IndexSearcher indexSearcher, final MultiTermQuery query)
throws IOException {
final int maxSize = Math.min(size, getMaxSize());
final PriorityQueue<ScoreTerm> stQueue = new PriorityQueue<>();
collectTerms(
reader,
indexSearcher.getIndexReader(),
query,
new TermCollector() {
private final MaxNonCompetitiveBoostAttribute maxBoostAtt =

View File

@ -357,11 +357,11 @@
* each Query implementation must provide an implementation of Weight. See the subsection on
* <a href="#weightClass">The Weight Interface</a> below for details on implementing the
* Weight interface.
* <li>{@link org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader)
* rewrite(IndexReader reader)} &mdash; Rewrites queries into primitive queries. Primitive
* queries are: {@link org.apache.lucene.search.TermQuery TermQuery}, {@link
* org.apache.lucene.search.BooleanQuery BooleanQuery}, <span >and other queries that
* implement {@link org.apache.lucene.search.Query#createWeight(IndexSearcher,ScoreMode,float)
* <li>{@link org.apache.lucene.search.Query#rewrite(IndexSearcher) rewrite(IndexReader reader)}
* &mdash; Rewrites queries into primitive queries. Primitive queries are: {@link
* org.apache.lucene.search.TermQuery TermQuery}, {@link org.apache.lucene.search.BooleanQuery
* BooleanQuery}, <span >and other queries that implement {@link
* org.apache.lucene.search.Query#createWeight(IndexSearcher,ScoreMode,float)
* createWeight(IndexSearcher searcher,ScoreMode scoreMode, float boost)}</span>
* </ol>
*

View File

@ -82,7 +82,7 @@ public class TestBooleanRewrites extends LuceneTestCase {
query1.add(new TermQuery(new Term("field", "a")), Occur.FILTER);
// Single clauses rewrite to a term query
final Query rewritten1 = query1.build().rewrite(reader);
final Query rewritten1 = query1.build().rewrite(searcher);
assertTrue(rewritten1 instanceof BoostQuery);
assertEquals(0f, ((BoostQuery) rewritten1).getBoost(), 0f);

View File

@ -60,7 +60,8 @@ public class TestFieldExistsQuery extends LuceneTestCase {
final IndexReader reader = iw.getReader();
iw.close();
assertTrue((new FieldExistsQuery("f")).rewrite(reader) instanceof MatchAllDocsQuery);
assertTrue(
(new FieldExistsQuery("f")).rewrite(newSearcher(reader)) instanceof MatchAllDocsQuery);
reader.close();
dir.close();
}
@ -79,7 +80,8 @@ public class TestFieldExistsQuery extends LuceneTestCase {
final IndexReader reader = iw.getReader();
iw.close();
assertTrue(new FieldExistsQuery("dim").rewrite(reader) instanceof MatchAllDocsQuery);
assertTrue(
new FieldExistsQuery("dim").rewrite(newSearcher(reader)) instanceof MatchAllDocsQuery);
reader.close();
dir.close();
}
@ -103,9 +105,10 @@ public class TestFieldExistsQuery extends LuceneTestCase {
iw.commit();
final IndexReader reader = iw.getReader();
iw.close();
final IndexSearcher searcher = newSearcher(reader);
assertFalse((new FieldExistsQuery("dim")).rewrite(reader) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("f")).rewrite(reader) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dim")).rewrite(searcher) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("f")).rewrite(searcher) instanceof MatchAllDocsQuery);
reader.close();
dir.close();
}
@ -124,10 +127,11 @@ public class TestFieldExistsQuery extends LuceneTestCase {
iw.commit();
final IndexReader reader = iw.getReader();
iw.close();
final IndexSearcher searcher = newSearcher(reader);
assertFalse((new FieldExistsQuery("dv1")).rewrite(reader) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dv2")).rewrite(reader) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dv3")).rewrite(reader) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dv1")).rewrite(searcher) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dv2")).rewrite(searcher) instanceof MatchAllDocsQuery);
assertFalse((new FieldExistsQuery("dv3")).rewrite(searcher) instanceof MatchAllDocsQuery);
reader.close();
dir.close();
}

View File

@ -202,7 +202,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
getIndexStore("field", new float[] {0, 1}, new float[] {1, 2}, new float[] {0, 0});
IndexReader reader = DirectoryReader.open(indexStore)) {
KnnVectorQuery query = new KnnVectorQuery("field", new float[] {2, 3}, 3);
Query dasq = query.rewrite(reader);
Query dasq = query.rewrite(newSearcher(reader));
IndexSearcher leafSearcher = newSearcher(reader.leaves().get(0).reader());
expectThrows(
IllegalStateException.class,
@ -222,7 +222,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
try (IndexReader reader = DirectoryReader.open(d)) {
IndexSearcher searcher = new IndexSearcher(reader);
KnnVectorQuery query = new KnnVectorQuery("field", new float[] {2, 3}, 3);
Query dasq = query.rewrite(reader);
Query dasq = query.rewrite(searcher);
Scorer scorer =
dasq.createWeight(searcher, ScoreMode.COMPLETE, 1).scorer(reader.leaves().get(0));
// before advancing the iterator
@ -249,7 +249,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
IndexReader reader = DirectoryReader.open(d)) {
IndexSearcher searcher = new IndexSearcher(reader);
KnnVectorQuery query = new KnnVectorQuery("field", new float[] {2, 3}, 3);
Query rewritten = query.rewrite(reader);
Query rewritten = query.rewrite(searcher);
Weight weight = searcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = weight.scorer(reader.leaves().get(0));
@ -291,7 +291,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
IndexSearcher searcher = new IndexSearcher(reader);
KnnVectorQuery query =
new KnnVectorQuery("field", VectorUtil.l2normalize(new float[] {2, 3}), 3);
Query rewritten = query.rewrite(reader);
Query rewritten = query.rewrite(searcher);
Weight weight = searcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = weight.scorer(reader.leaves().get(0));
@ -345,7 +345,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
assertEquals(1, reader.leaves().size());
IndexSearcher searcher = new IndexSearcher(reader);
KnnVectorQuery query = new KnnVectorQuery("field", new float[] {2, 3}, 3);
Query rewritten = query.rewrite(reader);
Query rewritten = query.rewrite(searcher);
Weight weight = searcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = weight.scorer(reader.leaves().get(0));
@ -400,7 +400,7 @@ public class TestKnnVectorQuery extends LuceneTestCase {
assertEquals(1, reader.leaves().size());
IndexSearcher searcher = new IndexSearcher(reader);
KnnVectorQuery query = new KnnVectorQuery("field", new float[] {1, 0}, 2);
Query rewritten = query.rewrite(reader);
Query rewritten = query.rewrite(searcher);
Weight weight = searcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = weight.scorer(reader.leaves().get(0));

View File

@ -87,7 +87,7 @@ public class TestMatchNoDocsQuery extends LuceneTestCase {
assertEquals(query.toString(), "key:one MatchNoDocsQuery(\"field not found\")");
assertEquals(searcher.count(query), 1);
hits = searcher.search(query, 1000).scoreDocs;
Query rewrite = query.rewrite(ir);
Query rewrite = query.rewrite(searcher);
assertEquals(1, hits.length);
assertEquals(rewrite.toString(), "key:one");

View File

@ -499,7 +499,7 @@ public class TestMatchesIterator extends MatchesTestBase {
SeekCountingLeafReader reader = new SeekCountingLeafReader(getOnlyLeafReader(this.reader));
this.searcher = new IndexSearcher(reader);
Query query = new PrefixQuery(new Term(FIELD_WITH_OFFSETS, "w"));
Weight w = searcher.createWeight(query.rewrite(reader), ScoreMode.COMPLETE, 1);
Weight w = searcher.createWeight(query.rewrite(searcher), ScoreMode.COMPLETE, 1);
// docs 0-3 match several different terms here, but we only seek to the first term and
// then short-cut return; other terms are ignored until we try and iterate over matches

View File

@ -29,6 +29,7 @@ public class TestNGramPhraseQuery extends LuceneTestCase {
private static IndexReader reader;
private static Directory directory;
private static IndexSearcher searcher;
@BeforeClass
public static void beforeClass() throws Exception {
@ -36,6 +37,7 @@ public class TestNGramPhraseQuery extends LuceneTestCase {
RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
writer.close();
reader = DirectoryReader.open(directory);
searcher = new IndexSearcher(reader);
}
@AfterClass
@ -50,8 +52,8 @@ public class TestNGramPhraseQuery extends LuceneTestCase {
// bi-gram test ABC => AB/BC => AB/BC
NGramPhraseQuery pq1 = new NGramPhraseQuery(2, new PhraseQuery("f", "AB", "BC"));
Query q = pq1.rewrite(reader);
assertSame(q.rewrite(reader), q);
Query q = pq1.rewrite(searcher);
assertSame(q.rewrite(searcher), q);
PhraseQuery rewritten1 = (PhraseQuery) q;
assertArrayEquals(new Term[] {new Term("f", "AB"), new Term("f", "BC")}, rewritten1.getTerms());
assertArrayEquals(new int[] {0, 1}, rewritten1.getPositions());
@ -59,7 +61,7 @@ public class TestNGramPhraseQuery extends LuceneTestCase {
// bi-gram test ABCD => AB/BC/CD => AB//CD
NGramPhraseQuery pq2 = new NGramPhraseQuery(2, new PhraseQuery("f", "AB", "BC", "CD"));
q = pq2.rewrite(reader);
q = pq2.rewrite(searcher);
assertTrue(q instanceof PhraseQuery);
assertNotSame(pq2, q);
PhraseQuery rewritten2 = (PhraseQuery) q;
@ -70,7 +72,7 @@ public class TestNGramPhraseQuery extends LuceneTestCase {
NGramPhraseQuery pq3 =
new NGramPhraseQuery(3, new PhraseQuery("f", "ABC", "BCD", "CDE", "DEF", "EFG", "FGH"));
q = pq3.rewrite(reader);
q = pq3.rewrite(searcher);
assertTrue(q instanceof PhraseQuery);
assertNotSame(pq3, q);
PhraseQuery rewritten3 = (PhraseQuery) q;

View File

@ -139,10 +139,10 @@ public class TestNeedsScores extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query in2 = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query in2 = in.rewrite(indexSearcher);
if (in2 == in) {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
} else {
return new AssertNeedsScores(in2, value);
}

View File

@ -567,7 +567,7 @@ public class TestPhraseQuery extends LuceneTestCase {
/* test that a single term is rewritten to a term query */
public void testRewrite() throws IOException {
PhraseQuery pq = new PhraseQuery("foo", "bar");
Query rewritten = pq.rewrite(searcher.getIndexReader());
Query rewritten = pq.rewrite(searcher);
assertTrue(rewritten instanceof TermQuery);
}

View File

@ -949,12 +949,12 @@ public class TestWANDScorer extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = query.rewrite(indexSearcher);
if (rewritten != query) {
return new MaxScoreWrapperQuery(rewritten, maxRange, maxScore);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -24,11 +24,11 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -100,8 +100,8 @@ public final class DrillDownQuery extends Query {
/**
* Creates a new {@code DrillDownQuery} over the given base query. Can be {@code null}, in which
* case the result {@link Query} from {@link #rewrite(IndexReader)} will be a pure browsing query,
* filtering on the added categories only.
* case the result {@link Query} from {@link Query#rewrite(IndexSearcher)} will be a pure browsing
* query, filtering on the added categories only.
*/
public DrillDownQuery(FacetsConfig config, Query baseQuery) {
this.baseQuery = baseQuery;
@ -156,7 +156,7 @@ public final class DrillDownQuery extends Query {
}
@Override
public Query rewrite(IndexReader r) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
BooleanQuery rewritten = getBooleanQuery();
if (rewritten.clauses().isEmpty()) {
return new MatchAllDocsQuery();

View File

@ -24,7 +24,6 @@ import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.facet.DrillSidewaysScorer.DocsAndCost;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.BulkScorer;
import org.apache.lucene.search.ConstantScoreScorer;
@ -80,8 +79,8 @@ class DrillSidewaysQuery extends Query {
}
/**
* Needed for {@link #rewrite(IndexReader)}. Ensures the same "managed" lists get used since
* {@link DrillSideways} accesses references to these through the original {@code
* Needed for {@link Query#rewrite(IndexSearcher)}. Ensures the same "managed" lists get used
* since {@link DrillSideways} accesses references to these through the original {@code
* DrillSidewaysQuery}.
*/
private DrillSidewaysQuery(
@ -107,17 +106,17 @@ class DrillSidewaysQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query newQuery = baseQuery;
while (true) {
Query rewrittenQuery = newQuery.rewrite(reader);
Query rewrittenQuery = newQuery.rewrite(indexSearcher);
if (rewrittenQuery == newQuery) {
break;
}
newQuery = rewrittenQuery;
}
if (newQuery == baseQuery) {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
} else {
return new DrillSidewaysQuery(
newQuery,

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.facet.MultiDoubleValues;
import org.apache.lucene.facet.MultiDoubleValuesSource;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.ConstantScoreScorer;
import org.apache.lucene.search.ConstantScoreWeight;
@ -154,14 +153,14 @@ public final class DoubleRange extends Range {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (fastMatchQuery != null) {
final Query fastMatchRewritten = fastMatchQuery.rewrite(reader);
final Query fastMatchRewritten = fastMatchQuery.rewrite(indexSearcher);
if (fastMatchRewritten != fastMatchQuery) {
return new ValueSourceQuery(range, fastMatchRewritten, valueSource);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override
@ -252,14 +251,14 @@ public final class DoubleRange extends Range {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (fastMatchQuery != null) {
final Query fastMatchRewritten = fastMatchQuery.rewrite(reader);
final Query fastMatchRewritten = fastMatchQuery.rewrite(indexSearcher);
if (fastMatchRewritten != fastMatchQuery) {
return new MultiValueSourceQuery(range, fastMatchRewritten, valueSource);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.facet.MultiLongValues;
import org.apache.lucene.facet.MultiLongValuesSource;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.ConstantScoreScorer;
import org.apache.lucene.search.ConstantScoreWeight;
@ -141,14 +140,14 @@ public final class LongRange extends Range {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (fastMatchQuery != null) {
final Query fastMatchRewritten = fastMatchQuery.rewrite(reader);
final Query fastMatchRewritten = fastMatchQuery.rewrite(indexSearcher);
if (fastMatchRewritten != fastMatchQuery) {
return new ValueSourceQuery(range, fastMatchRewritten, valueSource);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override
@ -239,14 +238,14 @@ public final class LongRange extends Range {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (fastMatchQuery != null) {
final Query fastMatchRewritten = fastMatchQuery.rewrite(reader);
final Query fastMatchRewritten = fastMatchQuery.rewrite(indexSearcher);
if (fastMatchRewritten != fastMatchQuery) {
return new MultiValueSourceQuery(range, fastMatchRewritten, valuesSource);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -255,7 +255,8 @@ public class TestDrillDownQuery extends FacetTestCase {
public void testNoDrillDown() throws Exception {
Query base = new MatchAllDocsQuery();
DrillDownQuery q = new DrillDownQuery(config, base);
Query rewrite = q.rewrite(reader).rewrite(reader);
IndexSearcher searcher = newSearcher(reader);
Query rewrite = q.rewrite(searcher).rewrite(searcher);
assertEquals(base, rewrite);
}

View File

@ -747,7 +747,7 @@ public class TestDrillSideways extends FacetTestCase {
Query baseQuery =
new TermQuery(new Term("content", "foo")) {
@Override
public Query rewrite(IndexReader reader) {
public Query rewrite(IndexSearcher indexSearcher) {
// return a new instance, forcing the DrillDownQuery to also rewrite itself, exposing
// the bug in LUCENE-9988:
return new TermQuery(getTerm());

View File

@ -1558,12 +1558,12 @@ public class TestRangeFacetCounts extends FacetTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query inRewritten = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query inRewritten = in.rewrite(indexSearcher);
if (in != inRewritten) {
return new UsedQuery(inRewritten, used);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -245,9 +245,11 @@ public class WeightedSpanTermExtractor {
final IndexReader reader = getLeafContext().reader();
Query rewritten;
if (query instanceof MultiTermQuery) {
rewritten = MultiTermQuery.SCORING_BOOLEAN_REWRITE.rewrite(reader, (MultiTermQuery) query);
rewritten =
MultiTermQuery.SCORING_BOOLEAN_REWRITE.rewrite(
new IndexSearcher(reader), (MultiTermQuery) query);
} else {
rewritten = origQuery.rewrite(reader);
rewritten = origQuery.rewrite(new IndexSearcher(reader));
}
if (rewritten != origQuery) {
// only rewrite once and then flatten again - the rewritten query could have a special

View File

@ -1249,11 +1249,11 @@ public class UnifiedHighlighter {
/**
* When highlighting phrases accurately, we need to know which {@link SpanQuery}'s need to have
* {@link Query#rewrite(IndexReader)} called on them. It helps performance to avoid it if it's not
* needed. This method will be invoked on all SpanQuery instances recursively. If you have custom
* SpanQuery queries then override this to check instanceof and provide a definitive answer. If
* the query isn't your custom one, simply return null to have the default rules apply, which
* govern the ones included in Lucene.
* {@link Query#rewrite(IndexSearcher)} called on them. It helps performance to avoid it if it's
* not needed. This method will be invoked on all SpanQuery instances recursively. If you have
* custom SpanQuery queries then override this to check instanceof and provide a definitive
* answer. If the query isn't your custom one, simply return null to have the default rules apply,
* which govern the ones included in Lucene.
*/
protected Boolean requiresRewrite(SpanQuery spanQuery) {
return null;

View File

@ -33,6 +33,7 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
@ -65,8 +66,14 @@ public class FieldQuery {
throws IOException {
this.fieldMatch = fieldMatch;
Set<Query> flatQueries = new LinkedHashSet<>();
flatten(query, reader, flatQueries, 1f);
saveTerms(flatQueries, reader);
IndexSearcher searcher;
if (reader == null) {
searcher = null;
} else {
searcher = new IndexSearcher(reader);
}
flatten(query, searcher, flatQueries, 1f);
saveTerms(flatQueries, searcher);
Collection<Query> expandQueries = expand(flatQueries);
for (Query flatQuery : expandQueries) {
@ -96,7 +103,7 @@ public class FieldQuery {
}
protected void flatten(
Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost)
Query sourceQuery, IndexSearcher searcher, Collection<Query> flatQueries, float boost)
throws IOException {
while (sourceQuery instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) sourceQuery;
@ -107,13 +114,13 @@ public class FieldQuery {
BooleanQuery bq = (BooleanQuery) sourceQuery;
for (BooleanClause clause : bq) {
if (!clause.isProhibited()) {
flatten(clause.getQuery(), reader, flatQueries, boost);
flatten(clause.getQuery(), searcher, flatQueries, boost);
}
}
} else if (sourceQuery instanceof DisjunctionMaxQuery) {
DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) sourceQuery;
for (Query query : dmq) {
flatten(query, reader, flatQueries, boost);
flatten(query, searcher, flatQueries, boost);
}
} else if (sourceQuery instanceof TermQuery) {
if (boost != 1f) {
@ -123,7 +130,7 @@ public class FieldQuery {
} else if (sourceQuery instanceof SynonymQuery) {
SynonymQuery synQuery = (SynonymQuery) sourceQuery;
for (Term term : synQuery.getTerms()) {
flatten(new TermQuery(term), reader, flatQueries, boost);
flatten(new TermQuery(term), searcher, flatQueries, boost);
}
} else if (sourceQuery instanceof PhraseQuery) {
PhraseQuery pq = (PhraseQuery) sourceQuery;
@ -135,28 +142,28 @@ public class FieldQuery {
} else if (sourceQuery instanceof ConstantScoreQuery) {
final Query q = ((ConstantScoreQuery) sourceQuery).getQuery();
if (q != null) {
flatten(q, reader, flatQueries, boost);
flatten(q, searcher, flatQueries, boost);
}
} else if (sourceQuery instanceof FunctionScoreQuery) {
final Query q = ((FunctionScoreQuery) sourceQuery).getWrappedQuery();
if (q != null) {
flatten(q, reader, flatQueries, boost);
flatten(q, searcher, flatQueries, boost);
}
} else if (reader != null) {
} else if (searcher != null) {
Query query = sourceQuery;
Query rewritten;
if (sourceQuery instanceof MultiTermQuery) {
rewritten =
new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(MAX_MTQ_TERMS)
.rewrite(reader, (MultiTermQuery) query);
.rewrite(searcher, (MultiTermQuery) query);
} else {
rewritten = query.rewrite(reader);
rewritten = query.rewrite(searcher);
}
if (rewritten != query) {
// only rewrite once and then flatten again - the rewritten query could have a speacial
// treatment
// if this method is overwritten in a subclass.
flatten(rewritten, reader, flatQueries, boost);
flatten(rewritten, searcher, flatQueries, boost);
}
// if the query is already rewritten we discard it
}
@ -311,7 +318,7 @@ public class FieldQuery {
* - fieldMatch==false
* termSetMap=Map<null,Set<"john","lennon">>
*/
void saveTerms(Collection<Query> flatQueries, IndexReader reader) throws IOException {
void saveTerms(Collection<Query> flatQueries, IndexSearcher searcher) throws IOException {
for (Query query : flatQueries) {
while (query instanceof BoostQuery) {
query = ((BoostQuery) query).getQuery();
@ -320,8 +327,8 @@ public class FieldQuery {
if (query instanceof TermQuery) termSet.add(((TermQuery) query).getTerm().text());
else if (query instanceof PhraseQuery) {
for (Term term : ((PhraseQuery) query).getTerms()) termSet.add(term.text());
} else if (query instanceof MultiTermQuery && reader != null) {
BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(reader);
} else if (query instanceof MultiTermQuery && searcher != null) {
BooleanQuery mtqTerms = (BooleanQuery) query.rewrite(searcher);
for (BooleanClause clause : mtqTerms) {
termSet.add(((TermQuery) clause.getQuery()).getTerm().text());
}

View File

@ -259,7 +259,7 @@ public class TestHighlighter extends BaseTokenStreamTestCase implements Formatte
new Query() {
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
CommonTermsQuery query = new CommonTermsQuery(Occur.MUST, Occur.SHOULD, 3);
query.add(new Term(FIELD_NAME, "this")); // stop-word
query.add(new Term(FIELD_NAME, "long"));
@ -2209,7 +2209,7 @@ public class TestHighlighter extends BaseTokenStreamTestCase implements Formatte
searcher = newSearcher(reader);
// for any multi-term queries to work (prefix, wildcard, range,fuzzy etc)
// you must use a rewritten query!
query = unReWrittenQuery.rewrite(reader);
query = unReWrittenQuery.rewrite(searcher);
if (VERBOSE) System.out.println("Searching for: " + query.toString(FIELD_NAME));
hits = searcher.search(query, 1000);
}

View File

@ -21,9 +21,9 @@ import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
@ -167,7 +167,7 @@ public class TestHighlightCustomQuery extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return new TermQuery(term);
}

View File

@ -1619,7 +1619,7 @@ public class TestUnifiedHighlighter extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) {
public Query rewrite(IndexSearcher indexSearcher) {
return this;
}

View File

@ -1111,8 +1111,8 @@ public class TestUnifiedHighlighterMTQ extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query newOriginalQuery = originalQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query newOriginalQuery = originalQuery.rewrite(indexSearcher);
if (newOriginalQuery != originalQuery) {
return new MyWrapperSpanQuery((SpanQuery) newOriginalQuery);
}

View File

@ -658,8 +658,8 @@ public class TestUnifiedHighlighterStrictPhrases extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query newWrapped = wrapped.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query newWrapped = wrapped.rewrite(indexSearcher);
if (newWrapped != wrapped) {
return new MyQuery(newWrapped);
}

View File

@ -21,7 +21,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
@ -37,6 +39,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
@ -56,6 +59,7 @@ public abstract class AbstractTestCase extends LuceneTestCase {
protected Analyzer analyzerB;
protected Analyzer analyzerK;
protected IndexReader reader;
protected IndexSearcher searcher;
protected static final String[] shortMVValues = {
"", "", "a b c", "", // empty data in multi valued field
@ -343,6 +347,7 @@ public abstract class AbstractTestCase extends LuceneTestCase {
writer.close();
if (reader != null) reader.close();
reader = DirectoryReader.open(dir);
searcher = newSearcher(reader);
}
// make 1 doc with multi valued & not analyzed field
@ -363,6 +368,7 @@ public abstract class AbstractTestCase extends LuceneTestCase {
writer.close();
if (reader != null) reader.close();
reader = DirectoryReader.open(dir);
searcher = newSearcher(reader);
}
protected void makeIndexShortMV() throws Exception {

View File

@ -63,7 +63,7 @@ public class TestFieldQuery extends AbstractTestCase {
FieldQuery fq = new FieldQuery(booleanQuery, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(booleanQuery, reader, flatQueries, 1f);
fq.flatten(booleanQuery, searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq(boost, "A"), tq(boost, "B"), tq(boost, "C"));
}
@ -73,7 +73,7 @@ public class TestFieldQuery extends AbstractTestCase {
query = new BoostQuery(query, boost);
FieldQuery fq = new FieldQuery(query, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(query, reader, flatQueries, 1f);
fq.flatten(query, searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq(boost, "A"), tq(boost, "B"), pqF(boost, "C", "D"));
}
@ -87,7 +87,7 @@ public class TestFieldQuery extends AbstractTestCase {
FieldQuery fq = new FieldQuery(booleanQuery, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(booleanQuery, reader, flatQueries, 1f);
fq.flatten(booleanQuery, searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq(boost, "A"), pqF(boost, "B", "C"));
}
@ -99,7 +99,7 @@ public class TestFieldQuery extends AbstractTestCase {
FieldQuery fq = new FieldQuery(query.build(), true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(query.build(), reader, flatQueries, 1f);
fq.flatten(query.build(), searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq("AA"), pqF("BC", "CD"), pqF("EF", "FG", "GH"));
}
@ -107,7 +107,7 @@ public class TestFieldQuery extends AbstractTestCase {
Query query = pqF("A");
FieldQuery fq = new FieldQuery(query, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(query, reader, flatQueries, 1f);
fq.flatten(query, searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq("A"));
}
@ -950,7 +950,7 @@ public class TestFieldQuery extends AbstractTestCase {
query = new BoostQuery(query, boost);
FieldQuery fq = new FieldQuery(query, true, true);
Set<Query> flatQueries = new HashSet<>();
fq.flatten(query, reader, flatQueries, 1f);
fq.flatten(query, searcher, flatQueries, 1f);
assertCollectionQueries(flatQueries, tq(boost, "A"));
}
}

View File

@ -18,7 +18,6 @@
package org.apache.lucene.search.join;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.search.DocIdSetIterator;
@ -88,12 +87,12 @@ public class ParentChildrenBlockJoinQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query childRewrite = childQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query childRewrite = childQuery.rewrite(indexSearcher);
if (childRewrite != childQuery) {
return new ParentChildrenBlockJoinQuery(parentFilter, childRewrite, parentDocId);
} else {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
}

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Explanation;
@ -305,12 +304,12 @@ public class ToChildBlockJoinQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query parentRewrite = parentQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query parentRewrite = parentQuery.rewrite(indexSearcher);
if (parentRewrite != parentQuery) {
return new ToChildBlockJoinQuery(parentRewrite, parentsFilter);
} else {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
}

View File

@ -22,7 +22,6 @@ import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.ConstantScoreQuery;
@ -425,12 +424,12 @@ public class ToParentBlockJoinQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query childRewrite = childQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query childRewrite = childQuery.rewrite(indexSearcher);
if (childRewrite != childQuery) {
return new ToParentBlockJoinQuery(childRewrite, parentsFilter, scoreMode);
} else {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
}

View File

@ -152,7 +152,7 @@ public final class SearchImpl extends LukeModel implements Search {
if (rewrite) {
try {
query = query.rewrite(reader);
query = query.rewrite(searcher);
} catch (IOException e) {
throw new LukeException(
String.format(Locale.ENGLISH, "Failed to rewrite query: %s", query.toString()), e);

View File

@ -499,12 +499,12 @@ public class TestDiversifiedTopDocsCollector extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = query.rewrite(indexSearcher);
if (rewritten != query) {
return new DocValueScoreQuery(rewritten, scoreField);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -19,7 +19,6 @@ package org.apache.lucene.monitor;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.*;
import org.apache.lucene.search.Matches;
@ -34,10 +33,10 @@ class ForceNoBulkScoringQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = inner.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = inner.rewrite(indexSearcher);
if (rewritten != inner) return new ForceNoBulkScoringQuery(rewritten);
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -22,9 +22,9 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.tests.util.LuceneTestCase;
@ -65,7 +65,7 @@ public abstract class MonitorTestBase extends LuceneTestCase {
public static class ThrowOnRewriteQuery extends Query {
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
throw new IOException("Error rewriting");
}

View File

@ -67,7 +67,7 @@ public class TestForceNoBulkScoringQuery extends LuceneTestCase {
assertEquals(q.getWrappedQuery(), pq);
Query rewritten = q.rewrite(reader);
Query rewritten = q.rewrite(newSearcher(reader));
assertTrue(rewritten instanceof ForceNoBulkScoringQuery);
Query inner = ((ForceNoBulkScoringQuery) rewritten).getWrappedQuery();

View File

@ -30,6 +30,7 @@ import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -101,7 +102,8 @@ public class CommonTermsQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
if (this.terms.isEmpty()) {
return new MatchNoDocsQuery("CommonTermsQuery with no terms");
} else if (this.terms.size() == 1) {

View File

@ -19,7 +19,6 @@ package org.apache.lucene.queries.function;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.DoubleValues;
@ -122,8 +121,8 @@ public final class FunctionScoreQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query rewritten = in.rewrite(indexSearcher);
if (rewritten == in) {
return this;
}

View File

@ -22,9 +22,9 @@ import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -57,8 +57,8 @@ public class MoreLikeThisQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
MoreLikeThis mlt = new MoreLikeThis(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
MoreLikeThis mlt = new MoreLikeThis(indexSearcher.getIndexReader());
mlt.setFieldNames(moreLikeFields);
mlt.setAnalyzer(analyzer);

View File

@ -19,7 +19,6 @@ package org.apache.lucene.queries.payloads;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
@ -83,12 +82,12 @@ public class PayloadScoreQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query matchRewritten = wrappedQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query matchRewritten = wrappedQuery.rewrite(indexSearcher);
if (wrappedQuery != matchRewritten && matchRewritten instanceof SpanQuery) {
return new PayloadScoreQuery((SpanQuery) matchRewritten, function, decoder, includeSpanScore);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
@ -116,13 +115,13 @@ public class SpanPayloadCheckQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query matchRewritten = match.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query matchRewritten = match.rewrite(indexSearcher);
if (match != matchRewritten && matchRewritten instanceof SpanQuery) {
return new SpanPayloadCheckQuery(
(SpanQuery) matchRewritten, payloadToMatch, payloadType, operation);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -18,7 +18,6 @@ package org.apache.lucene.queries.spans;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
@ -93,13 +92,13 @@ public final class FieldMaskingSpanQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
SpanQuery rewritten = (SpanQuery) maskedQuery.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
SpanQuery rewritten = (SpanQuery) maskedQuery.rewrite(indexSearcher);
if (rewritten != maskedQuery) {
return new FieldMaskingSpanQuery(rewritten, field);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
@ -109,9 +108,9 @@ abstract class SpanContainQuery extends SpanQuery implements Cloneable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
SpanQuery rewrittenBig = (SpanQuery) big.rewrite(reader);
SpanQuery rewrittenLittle = (SpanQuery) little.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
SpanQuery rewrittenBig = (SpanQuery) big.rewrite(indexSearcher);
SpanQuery rewrittenLittle = (SpanQuery) little.rewrite(indexSearcher);
if (big != rewrittenBig || little != rewrittenLittle) {
try {
SpanContainQuery clone = (SpanContainQuery) super.clone();
@ -122,7 +121,7 @@ abstract class SpanContainQuery extends SpanQuery implements Cloneable {
throw new AssertionError(e);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.search.BooleanClause.Occur;
@ -117,8 +116,8 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return rewriteMethod.rewrite(reader, query);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return rewriteMethod.rewrite(indexSearcher, query);
}
@Override
@ -141,7 +140,8 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
/** Abstract class that defines how the query is rewritten. */
public abstract static class SpanRewriteMethod extends MultiTermQuery.RewriteMethod {
@Override
public abstract SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException;
public abstract SpanQuery rewrite(IndexSearcher indexSearcher, MultiTermQuery query)
throws IOException;
}
/**
@ -182,8 +182,9 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
};
@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
return (SpanQuery) delegate.rewrite(reader, query);
public SpanQuery rewrite(IndexSearcher indexSearcher, MultiTermQuery query)
throws IOException {
return (SpanQuery) delegate.rewrite(indexSearcher, query);
}
};
@ -233,8 +234,8 @@ public class SpanMultiTermQueryWrapper<Q extends MultiTermQuery> extends SpanQue
}
@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
return (SpanQuery) delegate.rewrite(reader, query);
public SpanQuery rewrite(IndexSearcher indexSearcher, MultiTermQuery query) throws IOException {
return (SpanQuery) delegate.rewrite(indexSearcher, query);
}
@Override

View File

@ -23,7 +23,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
@ -242,12 +241,12 @@ public class SpanNearQuery extends SpanQuery implements Cloneable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
boolean actuallyRewritten = false;
List<SpanQuery> rewrittenClauses = new ArrayList<>();
for (int i = 0; i < clauses.size(); i++) {
SpanQuery c = clauses.get(i);
SpanQuery query = (SpanQuery) c.rewrite(reader);
SpanQuery query = (SpanQuery) c.rewrite(indexSearcher);
actuallyRewritten |= query != c;
rewrittenClauses.add(query);
}
@ -260,7 +259,7 @@ public class SpanNearQuery extends SpanQuery implements Cloneable {
throw new AssertionError(e);
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -19,7 +19,6 @@ package org.apache.lucene.queries.spans;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
@ -224,13 +223,13 @@ public final class SpanNotQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader);
SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(indexSearcher);
SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(indexSearcher);
if (rewrittenInclude != include || rewrittenExclude != exclude) {
return new SpanNotQuery(rewrittenInclude, rewrittenExclude, pre, post);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
@ -67,19 +66,19 @@ public final class SpanOrQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
SpanOrQuery rewritten = new SpanOrQuery();
boolean actuallyRewritten = false;
for (int i = 0; i < clauses.size(); i++) {
SpanQuery c = clauses.get(i);
SpanQuery query = (SpanQuery) c.rewrite(reader);
SpanQuery query = (SpanQuery) c.rewrite(indexSearcher);
actuallyRewritten |= query != c;
rewritten.addClause(query);
}
if (actuallyRewritten) {
return rewritten;
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -19,7 +19,6 @@ package org.apache.lucene.queries.spans;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
@ -113,8 +112,8 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Clonea
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
SpanQuery rewritten = (SpanQuery) match.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
SpanQuery rewritten = (SpanQuery) match.rewrite(indexSearcher);
if (rewritten != match) {
try {
SpanPositionCheckQuery clone = (SpanPositionCheckQuery) this.clone();
@ -125,7 +124,7 @@ public abstract class SpanPositionCheckQuery extends SpanQuery implements Clonea
}
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -781,8 +781,8 @@ public class TestValueSources extends LuceneTestCase {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
var rewrite = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
var rewrite = in.rewrite(indexSearcher);
return rewrite == in ? this : new AssertScoreComputedOnceQuery(rewrite);
}

View File

@ -18,7 +18,6 @@ package org.apache.lucene.queries.spans;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -50,10 +49,10 @@ public class AssertingSpanQuery extends SpanQuery {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query q = in.rewrite(reader);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
Query q = in.rewrite(indexSearcher);
if (q == in) {
return super.rewrite(reader);
return super.rewrite(indexSearcher);
} else if (q instanceof SpanQuery) {
return new AssertingSpanQuery((SpanQuery) q);
} else {

View File

@ -164,7 +164,7 @@ public class TestFieldMaskingSpanQuery extends LuceneTestCase {
new FieldMaskingSpanQuery(
new SpanTermQuery(new Term("last", "sally")) {
@Override
public Query rewrite(IndexReader reader) {
public Query rewrite(IndexSearcher indexSearcher) {
return new SpanOrQuery(
new SpanTermQuery(new Term("first", "sally")),
new SpanTermQuery(new Term("first", "james")));

View File

@ -232,7 +232,8 @@ public class TestSpanMultiTermQueryWrapper extends LuceneTestCase {
wrapper.setRewriteMethod(
new SpanMultiTermQueryWrapper.SpanRewriteMethod() {
@Override
public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
public SpanQuery rewrite(IndexSearcher indexSearcher, MultiTermQuery query)
throws IOException {
return null;
}
});

View File

@ -22,7 +22,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.spans.SpanNearQuery;
import org.apache.lucene.queries.spans.SpanNotQuery;
@ -256,7 +255,7 @@ public class ComplexPhraseQueryParser extends QueryParser {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final Query contents = this.contents[0];
// ArrayList spanClauses = new ArrayList();
if (contents instanceof TermQuery
@ -284,7 +283,7 @@ public class ComplexPhraseQueryParser extends QueryParser {
// HashSet bclauseterms=new HashSet();
Query qc = clause.getQuery();
// Rewrite this clause e.g one* becomes (one OR onerous)
qc = new IndexSearcher(reader).rewrite(qc);
qc = indexSearcher.rewrite(qc);
if (clause.getOccur().equals(BooleanClause.Occur.MUST_NOT)) {
numNegatives++;
}

View File

@ -17,7 +17,7 @@
package org.apache.lucene.queryparser.surround.query;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -28,8 +28,8 @@ class DistanceRewriteQuery extends RewriteQuery<DistanceQuery> {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
return srndQuery.getSpanNearQuery(reader, fieldName, qf);
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
return srndQuery.getSpanNearQuery(indexSearcher.getIndexReader(), fieldName, qf);
}
@Override

View File

@ -18,7 +18,7 @@ package org.apache.lucene.queryparser.surround.query;
import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
abstract class RewriteQuery<SQ extends SrndQuery> extends Query {
@ -33,7 +33,7 @@ abstract class RewriteQuery<SQ extends SrndQuery> extends Query {
}
@Override
public abstract Query rewrite(IndexReader reader) throws IOException;
public abstract Query rewrite(IndexSearcher indexSearcher) throws IOException;
@Override
public String toString(String field) {

View File

@ -19,9 +19,9 @@ package org.apache.lucene.queryparser.surround.query;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
@ -33,10 +33,10 @@ class SimpleTermRewriteQuery extends RewriteQuery<SimpleTerm> {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
final List<Query> luceneSubQueries = new ArrayList<>();
srndQuery.visitMatchingTerms(
reader,
indexSearcher.getIndexReader(),
fieldName,
new SimpleTerm.MatchingTermVisitor() {
@Override

View File

@ -315,7 +315,7 @@ public class TestCoreParser extends LuceneTestCase {
}
protected Query rewrite(Query q) throws IOException {
return q.rewrite(reader());
return q.rewrite(searcher());
}
protected void dumpResults(String qType, Query q, int numDocs) throws IOException {

View File

@ -38,6 +38,7 @@ import org.apache.lucene.search.BoostAttribute;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.FuzzyTermsEnum;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.TermQuery;
@ -282,7 +283,8 @@ public class FuzzyLikeThisQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
IndexReader reader = indexSearcher.getIndexReader();
ScoreTermQueue q = new ScoreTermQueue(maxNumTerms);
// load up the list of possible terms
for (FieldVals f : fieldVals) {

View File

@ -253,7 +253,7 @@ public final class CombinedFieldQuery extends Query implements Accountable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (terms.length == 0 || fieldAndWeights.isEmpty()) {
return new BooleanQuery.Builder().build();
}

View File

@ -22,7 +22,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
@ -125,7 +124,7 @@ public final class CoveringQuery extends Query implements Accountable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (minimumNumberMatch instanceof LongValuesSource.ConstantLongValuesSource) {
final long constantMin =
((LongValuesSource.ConstantLongValuesSource) minimumNumberMatch).getValue();
@ -136,7 +135,7 @@ public final class CoveringQuery extends Query implements Accountable {
BooleanQuery.Builder builder =
new BooleanQuery.Builder().setMinimumNumberShouldMatch((int) Math.max(constantMin, 1));
for (Query query : queries) {
Query r = query.rewrite(reader);
Query r = query.rewrite(indexSearcher);
builder.add(r, BooleanClause.Occur.SHOULD);
}
return builder.build();
@ -144,14 +143,14 @@ public final class CoveringQuery extends Query implements Accountable {
Multiset<Query> rewritten = new Multiset<>();
boolean actuallyRewritten = false;
for (Query query : queries) {
Query r = query.rewrite(reader);
Query r = query.rewrite(indexSearcher);
rewritten.add(r);
actuallyRewritten |= query != r;
}
if (actuallyRewritten) {
return new CoveringQuery(rewritten, minimumNumberMatch);
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -22,7 +22,6 @@ import java.util.function.Predicate;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
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;
@ -148,12 +147,12 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (lowerValue == Long.MIN_VALUE && upperValue == Long.MAX_VALUE) {
return new FieldExistsQuery(field);
}
Query rewrittenFallback = fallbackQuery.rewrite(reader);
Query rewrittenFallback = fallbackQuery.rewrite(indexSearcher);
if (rewrittenFallback.getClass() == MatchAllDocsQuery.class) {
return new MatchAllDocsQuery();
}

View File

@ -23,7 +23,6 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PointValues;
@ -169,7 +168,7 @@ public abstract class MultiRangeQuery extends Query implements Cloneable {
* #mergeOverlappingRanges}
*/
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (numDims != 1) {
return this;
}

View File

@ -113,14 +113,14 @@ public class PhraseWildcardQuery extends Query {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (phraseTerms.isEmpty()) {
return NO_MATCH_QUERY;
}
if (phraseTerms.size() == 1) {
return phraseTerms.get(0).getQuery();
}
return super.rewrite(reader);
return super.rewrite(indexSearcher);
}
@Override

View File

@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.PostingsEnum;
@ -449,7 +448,7 @@ public class TermAutomatonQuery extends Query implements Accountable {
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (Operations.isEmpty(det)) {
return new MatchNoDocsQuery();
}

View File

@ -81,7 +81,7 @@ public class TestFuzzyLikeThisQuery extends LuceneTestCase {
public void testClosestEditDistanceMatchComesFirst() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("smith", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
Query q = flt.rewrite(searcher);
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant smythe", queryTerms.contains(new Term("name", "smythe")));
@ -98,7 +98,7 @@ public class TestFuzzyLikeThisQuery extends LuceneTestCase {
public void testMultiWord() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("jonathin smoth", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
Query q = flt.rewrite(searcher);
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant jonathan", queryTerms.contains(new Term("name", "jonathan")));
@ -116,7 +116,7 @@ public class TestFuzzyLikeThisQuery extends LuceneTestCase {
flt.addTerms("jonathin smoth", "name", 2, 1);
flt.addTerms("jonathin smoth", "this field does not exist", 2, 1);
// don't fail here just because the field doesn't exits
Query q = flt.rewrite(searcher.getIndexReader());
Query q = flt.rewrite(searcher);
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant jonathan", queryTerms.contains(new Term("name", "jonathan")));
@ -132,7 +132,7 @@ public class TestFuzzyLikeThisQuery extends LuceneTestCase {
public void testNoMatchFirstWordBug() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("fernando smith", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
Query q = flt.rewrite(searcher);
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant smith", queryTerms.contains(new Term("name", "smith")));

View File

@ -80,7 +80,8 @@ public class TestCoveringQuery extends LuceneTestCase {
LongValuesSource vs = LongValuesSource.fromIntField("field");
assertEquals(
new CoveringQuery(Collections.singleton(tq), vs),
new CoveringQuery(Collections.singleton(pq), vs).rewrite(new MultiReader()));
new CoveringQuery(Collections.singleton(pq), vs)
.rewrite(new IndexSearcher(new MultiReader())));
}
public void testToString() {

View File

@ -372,7 +372,7 @@ public class TestIndexSortSortedNumericDocValuesRangeQuery extends LuceneTestCas
IndexReader reader = writer.getReader();
Query query = createQuery("field", Long.MIN_VALUE, Long.MAX_VALUE);
Query rewrittenQuery = query.rewrite(reader);
Query rewrittenQuery = query.rewrite(newSearcher(reader));
assertEquals(new FieldExistsQuery("field"), rewrittenQuery);
writer.close();
@ -390,7 +390,7 @@ public class TestIndexSortSortedNumericDocValuesRangeQuery extends LuceneTestCas
Query fallbackQuery = new BooleanQuery.Builder().build();
Query query = new IndexSortSortedNumericDocValuesRangeQuery("field", 1, 42, fallbackQuery);
Query rewrittenQuery = query.rewrite(reader);
Query rewrittenQuery = query.rewrite(newSearcher(reader));
assertNotEquals(query, rewrittenQuery);
assertThat(rewrittenQuery, instanceOf(IndexSortSortedNumericDocValuesRangeQuery.class));

View File

@ -806,7 +806,7 @@ public class TestMultiRangeQueries extends LuceneTestCase {
builder2.add(LongPoint.newRangeQuery("point", lower, upper), BooleanClause.Occur.SHOULD);
}
MultiRangeQuery multiRangeQuery = (MultiRangeQuery) builder1.build().rewrite(reader);
MultiRangeQuery multiRangeQuery = (MultiRangeQuery) builder1.build().rewrite(searcher);
BooleanQuery booleanQuery = builder2.build();
int count = searcher.search(multiRangeQuery, DummyTotalHitCountCollector.createManager());
int booleanCount = searcher.search(booleanQuery, DummyTotalHitCountCollector.createManager());
@ -839,7 +839,7 @@ public class TestMultiRangeQueries extends LuceneTestCase {
builder2.add(LongPoint.newRangeQuery("point", lower, upper), BooleanClause.Occur.SHOULD);
}
MultiRangeQuery multiRangeQuery = (MultiRangeQuery) builder1.build().rewrite(reader);
MultiRangeQuery multiRangeQuery = (MultiRangeQuery) builder1.build().rewrite(searcher);
BooleanQuery booleanQuery = builder2.build();
int count =
multiRangeQuery

View File

@ -785,7 +785,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
assertTrue(q.rewrite(r) instanceof MatchNoDocsQuery);
assertTrue(q.rewrite(newSearcher(r)) instanceof MatchNoDocsQuery);
IOUtils.close(w, r, dir);
}
@ -804,7 +804,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
Query rewrite = q.rewrite(r);
Query rewrite = q.rewrite(newSearcher(r));
assertTrue(rewrite instanceof TermQuery);
assertEquals(new Term("field", "foo"), ((TermQuery) rewrite).getTerm());
IOUtils.close(w, r, dir);
@ -827,7 +827,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
Query rewrite = q.rewrite(r);
Query rewrite = q.rewrite(newSearcher(r));
assertTrue(rewrite instanceof PhraseQuery);
Term[] terms = ((PhraseQuery) rewrite).getTerms();
assertEquals(new Term("field", "foo"), terms[0]);
@ -859,7 +859,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
Query rewrite = q.rewrite(r);
Query rewrite = q.rewrite(newSearcher(r));
assertTrue(rewrite instanceof PhraseQuery);
Term[] terms = ((PhraseQuery) rewrite).getTerms();
assertEquals(new Term("field", "foo"), terms[0]);
@ -888,7 +888,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
Query rewrite = q.rewrite(r);
Query rewrite = q.rewrite(newSearcher(r));
assertTrue(rewrite instanceof MultiPhraseQuery);
Term[][] terms = ((MultiPhraseQuery) rewrite).getTermArrays();
assertEquals(1, terms.length);
@ -923,7 +923,7 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
w.addDocument(doc);
IndexReader r = w.getReader();
Query rewrite = q.rewrite(r);
Query rewrite = q.rewrite(newSearcher(r));
assertTrue(rewrite instanceof MultiPhraseQuery);
Term[][] terms = ((MultiPhraseQuery) rewrite).getTermArrays();
assertEquals(2, terms.length);

Some files were not shown because too many files have changed in this diff Show More