diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 06e1902f7b1..bcc29ef07cb 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -88,6 +88,10 @@ API Changes * LUCENE-6223: Move BooleanQuery.BooleanWeight to BooleanWeight. (Robert Muir) +* LUCENE-6226: Query.createWeight() and IndexSearcher.createNormalizedWeight() + now take an integer bitmask indicating which postings values should be + retrieved from the index. (Alan Woodward, Adrien Grand) + Other * LUCENE-6193: Collapse identical catch branches in try-catch statements. diff --git a/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java b/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java index 4d91651bada..8ef50cdf981 100644 --- a/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java +++ b/lucene/core/src/java/org/apache/lucene/index/PostingsEnum.java @@ -92,17 +92,16 @@ public abstract class PostingsEnum extends DocIdSetIterator { } /** - * Returns the next position. If there are no more - * positions, or the iterator does not support positions, - * this will return DocsEnum.NO_MORE_POSITIONS */ + * Returns the next position, or -1 if positions are not available. + * Should only be called up to freq() times */ public abstract int nextPosition() throws IOException; /** Returns start offset for the current position, or -1 - * if offsets were not indexed. */ + * if offsets are not available. */ public abstract int startOffset() throws IOException; /** Returns end offset for the current position, or -1 if - * offsets were not indexed. */ + * offsets are not available. */ public abstract int endOffset() throws IOException; /** Returns the payload at this position, or null if no diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java index 9b159bf4f0e..9551e828ae7 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java @@ -163,8 +163,8 @@ public class BooleanQuery extends Query implements Iterable { public final Iterator iterator() { return clauses().iterator(); } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return new BooleanWeight(this, searcher, needsScores, disableCoord); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return new BooleanWeight(this, searcher, postingsFlags, disableCoord); } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java index 524f5d6b7ea..7cb935c54b1 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.util.Bits; @@ -43,17 +44,17 @@ public class BooleanWeight extends Weight { private final boolean disableCoord; private final boolean needsScores; - public BooleanWeight(BooleanQuery query, IndexSearcher searcher, boolean needsScores, boolean disableCoord) throws IOException { + public BooleanWeight(BooleanQuery query, IndexSearcher searcher, int postingsFlags, boolean disableCoord) throws IOException { super(query); this.query = query; - this.needsScores = needsScores; + this.needsScores = (postingsFlags & PostingsEnum.FLAG_FREQS) != 0; this.similarity = searcher.getSimilarity(); this.disableCoord = disableCoord; weights = new ArrayList<>(query.clauses().size()); for (int i = 0 ; i < query.clauses().size(); i++) { BooleanClause c = query.clauses().get(i); - final boolean queryNeedsScores = needsScores && c.getOccur() != Occur.MUST_NOT; - Weight w = c.getQuery().createWeight(searcher, queryNeedsScores); + final int subQueryFlags = c.getOccur() == Occur.MUST_NOT ? PostingsEnum.FLAG_NONE : postingsFlags; + Weight w = c.getQuery().createWeight(searcher, subQueryFlags); weights.add(w); if (!c.isProhibited()) { maxCoord++; diff --git a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java index 898b395f841..c668dd2326e 100644 --- a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; @@ -111,7 +112,7 @@ public class ConstantScoreQuery extends Query { public ConstantWeight(IndexSearcher searcher) throws IOException { super(ConstantScoreQuery.this); - this.innerWeight = (query == null) ? null : query.createWeight(searcher, false); + this.innerWeight = (query == null) ? null : query.createWeight(searcher, PostingsEnum.FLAG_NONE); } @Override @@ -327,7 +328,7 @@ public class ConstantScoreQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new ConstantScoreQuery.ConstantWeight(searcher); } diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java index cfa19add59f..f995a4de99a 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java @@ -118,10 +118,10 @@ public class DisjunctionMaxQuery extends Query implements Iterable { protected ArrayList weights = new ArrayList<>(); // The Weight's for our subqueries, in 1-1 correspondence with disjuncts /** Construct the Weight for this Query searched by searcher. Recursively construct subquery weights. */ - public DisjunctionMaxWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public DisjunctionMaxWeight(IndexSearcher searcher, int postingsFlags) throws IOException { super(DisjunctionMaxQuery.this); for (Query disjunctQuery : disjuncts) { - weights.add(disjunctQuery.createWeight(searcher, needsScores)); + weights.add(disjunctQuery.createWeight(searcher, postingsFlags)); } } @@ -194,8 +194,8 @@ public class DisjunctionMaxQuery extends Query implements Iterable { /** Create the Weight used to score us */ @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return new DisjunctionMaxWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return new DisjunctionMaxWeight(searcher, postingsFlags); } /** Optimize our representation and our subqueries representations diff --git a/lucene/core/src/java/org/apache/lucene/search/FilteredQuery.java b/lucene/core/src/java/org/apache/lucene/search/FilteredQuery.java index 5af0b72af7d..3948242deb0 100644 --- a/lucene/core/src/java/org/apache/lucene/search/FilteredQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/FilteredQuery.java @@ -78,8 +78,8 @@ public class FilteredQuery extends Query { * This is accomplished by overriding the Scorer returned by the Weight. */ @Override - public Weight createWeight(final IndexSearcher searcher, boolean needsScores) throws IOException { - final Weight weight = query.createWeight (searcher, needsScores); + public Weight createWeight(final IndexSearcher searcher, int postingsFlags) throws IOException { + final Weight weight = query.createWeight (searcher, postingsFlags); return new Weight(FilteredQuery.this) { @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java index 596ede4784f..37bbce29f35 100644 --- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java @@ -33,6 +33,7 @@ import org.apache.lucene.index.IndexReaderContext; import org.apache.lucene.index.IndexWriter; // javadocs import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.MultiFields; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.ReaderUtil; import org.apache.lucene.index.StoredDocument; import org.apache.lucene.index.StoredFieldVisitor; @@ -240,13 +241,14 @@ public class IndexSearcher { return collector.topDocs(); } else { final TopScoreDocCollector[] collectors = new TopScoreDocCollector[leafSlices.length]; - boolean needsScores = false; + int postingsFlags = PostingsEnum.FLAG_NONE; for (int i = 0; i < leafSlices.length; ++i) { collectors[i] = TopScoreDocCollector.create(numHits, after); - needsScores |= collectors[i].needsScores(); + if (collectors[i].needsScores()) + postingsFlags |= PostingsEnum.FLAG_FREQS; } - final Weight weight = createNormalizedWeight(query, needsScores); + final Weight weight = createNormalizedWeight(query, postingsFlags); final List> topDocsFutures = new ArrayList<>(leafSlices.length); for (int i = 0; i < leafSlices.length; ++i) { final LeafReaderContext[] leaves = leafSlices[i].leaves; @@ -338,7 +340,8 @@ public class IndexSearcher { */ public void search(Query query, Collector results) throws IOException { - search(leafContexts, createNormalizedWeight(query, results.needsScores()), results); + int postingsFlags = results.needsScores() ? PostingsEnum.FLAG_FREQS : PostingsEnum.FLAG_NONE; + search(leafContexts, createNormalizedWeight(query, postingsFlags), results); } /** Search implementation with arbitrary sorting. Finds @@ -462,13 +465,14 @@ public class IndexSearcher { return collector.topDocs(); } else { final TopFieldCollector[] collectors = new TopFieldCollector[leafSlices.length]; - boolean needsScores = false; + int postingsFlags = PostingsEnum.FLAG_NONE; for (int i = 0; i < leafSlices.length; ++i) { collectors[i] = TopFieldCollector.create(sort, numHits, after, fillFields, doDocScores, doMaxScore); - needsScores |= collectors[i].needsScores(); + if (collectors[i].needsScores()) + postingsFlags |= PostingsEnum.FLAG_FREQS; } - final Weight weight = createNormalizedWeight(query, needsScores); + final Weight weight = createNormalizedWeight(query, postingsFlags); final List> topDocsFutures = new ArrayList<>(leafSlices.length); for (int i = 0; i < leafSlices.length; ++i) { final LeafReaderContext[] leaves = leafSlices[i].leaves; @@ -565,7 +569,7 @@ public class IndexSearcher { * entire index. */ public Explanation explain(Query query, int doc) throws IOException { - return explain(createNormalizedWeight(query, true), doc); + return explain(createNormalizedWeight(query, PostingsEnum.FLAG_FREQS), doc); } /** Expert: low-level implementation method @@ -595,9 +599,9 @@ public class IndexSearcher { * can then directly be used to get a {@link Scorer}. * @lucene.internal */ - public Weight createNormalizedWeight(Query query, boolean needsScores) throws IOException { + public Weight createNormalizedWeight(Query query, int postingsFlags) throws IOException { query = rewrite(query); - Weight weight = query.createWeight(this, needsScores); + Weight weight = query.createWeight(this, postingsFlags); float v = weight.getValueForNormalization(); float norm = getSimilarity().queryNorm(v); if (Float.isInfinite(norm) || Float.isNaN(norm)) { diff --git a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java index 8817abc0eed..3800a3824a6 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java @@ -150,7 +150,7 @@ public class MatchAllDocsQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) { return new MatchAllDocsWeight(searcher); } diff --git a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java index e515e39e5c6..3c8c23af90c 100644 --- a/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/MultiPhraseQuery.java @@ -142,11 +142,13 @@ public class MultiPhraseQuery extends Query { private final Similarity.SimWeight stats; private final Map termContexts = new HashMap<>(); private final boolean needsScores; + private final int postingsFlags; - public MultiPhraseWeight(IndexSearcher searcher, boolean needsScores) + public MultiPhraseWeight(IndexSearcher searcher, int postingsFlags) throws IOException { super(MultiPhraseQuery.this); - this.needsScores = needsScores; + this.needsScores = (postingsFlags & PostingsEnum.FLAG_FREQS) != 0; + this.postingsFlags = postingsFlags | PostingsEnum.FLAG_POSITIONS; this.similarity = searcher.getSimilarity(); final IndexReaderContext context = searcher.getTopReaderContext(); @@ -228,7 +230,7 @@ public class MultiPhraseQuery extends Query { return null; } termsEnum.seekExact(term.bytes(), termState); - postingsEnum = termsEnum.postings(liveDocs, null, PostingsEnum.FLAG_POSITIONS); + postingsEnum = termsEnum.postings(liveDocs, null, postingsFlags); if (postingsEnum == null) { // term does exist, but has no positions @@ -296,8 +298,8 @@ public class MultiPhraseQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return new MultiPhraseWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return new MultiPhraseWeight(searcher, postingsFlags); } /** Prints a user-readable version of this query. */ diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java index 55c47207c6a..e79522dadb1 100644 --- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java @@ -211,13 +211,15 @@ public class PhraseQuery extends Query { private class PhraseWeight extends Weight { private final Similarity similarity; private final Similarity.SimWeight stats; + private final int postingsFlags; private final boolean needsScores; private transient TermContext states[]; - public PhraseWeight(IndexSearcher searcher, boolean needsScores) + public PhraseWeight(IndexSearcher searcher, int postingsFlags) throws IOException { super(PhraseQuery.this); - this.needsScores = needsScores; + this.postingsFlags = postingsFlags | PostingsEnum.FLAG_POSITIONS; + this.needsScores = (postingsFlags & PostingsEnum.FLAG_FREQS) != 0; this.similarity = searcher.getSimilarity(); final IndexReaderContext context = searcher.getTopReaderContext(); states = new TermContext[terms.size()]; @@ -266,7 +268,7 @@ public class PhraseQuery extends Query { return null; } te.seekExact(t.bytes(), state); - PostingsEnum postingsEnum = te.postings(liveDocs, null, PostingsEnum.FLAG_POSITIONS); + PostingsEnum postingsEnum = te.postings(liveDocs, null, postingsFlags); // PhraseQuery on a field that did not index // positions. @@ -318,8 +320,8 @@ public class PhraseQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return new PhraseWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return new PhraseWeight(searcher, postingsFlags); } /** diff --git a/lucene/core/src/java/org/apache/lucene/search/Query.java b/lucene/core/src/java/org/apache/lucene/search/Query.java index cd03ccb9f48..ece27c6348d 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Query.java +++ b/lucene/core/src/java/org/apache/lucene/search/Query.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Set; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; /** The abstract base class for queries. @@ -74,10 +75,10 @@ public abstract class Query implements Cloneable { *

* Only implemented by primitive queries, which re-write to themselves. * - * @param needsScores True if document scores ({@link Scorer#score}) or match - * frequencies ({@link Scorer#freq}) are needed. + * @param postingsFlags Bitmask indicating which postings features should be returned + * by this query (see {@link PostingsEnum}) */ - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { throw new UnsupportedOperationException("Query " + this + " does not implement createWeight"); } diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java b/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java index b01668d9a86..d89dd201904 100644 --- a/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java @@ -61,7 +61,7 @@ public abstract class QueryRescorer extends Rescorer { List leaves = searcher.getIndexReader().leaves(); - Weight weight = searcher.createNormalizedWeight(query, true); + Weight weight = searcher.createNormalizedWeight(query, PostingsEnum.FLAG_FREQS); // Now merge sort docIDs from hits, with reader's leaves: int hitUpto = 0; diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryWrapperFilter.java b/lucene/core/src/java/org/apache/lucene/search/QueryWrapperFilter.java index 4756d2cd55f..8d84bed6d72 100644 --- a/lucene/core/src/java/org/apache/lucene/search/QueryWrapperFilter.java +++ b/lucene/core/src/java/org/apache/lucene/search/QueryWrapperFilter.java @@ -53,7 +53,7 @@ public class QueryWrapperFilter extends Filter { public DocIdSet getDocIdSet(final LeafReaderContext context, final Bits acceptDocs) throws IOException { // get a private context that is used to rewrite, createWeight and score eventually final LeafReaderContext privateContext = context.reader().getContext(); - final Weight weight = new IndexSearcher(privateContext).createNormalizedWeight(query, false); + final Weight weight = new IndexSearcher(privateContext).createNormalizedWeight(query, PostingsEnum.FLAG_NONE); return new DocIdSet() { @Override public DocIdSetIterator iterator() throws IOException { diff --git a/lucene/core/src/java/org/apache/lucene/search/Scorer.java b/lucene/core/src/java/org/apache/lucene/search/Scorer.java index bf6502ef74c..968b24d56df 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Scorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/Scorer.java @@ -73,7 +73,7 @@ public abstract class Scorer extends PostingsEnum { public Collection getChildren() { return Collections.emptyList(); } - + /** A child Scorer and its relationship to its parent. * the meaning of the relationship depends upon the parent query. * @lucene.experimental */ diff --git a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java index 3a353e5fdea..3eeb0d45ad3 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java @@ -47,12 +47,12 @@ public class TermQuery extends Query { private final Similarity similarity; private final Similarity.SimWeight stats; private final TermContext termStates; - private final boolean needsScores; + private final int postingsFlags; - public TermWeight(IndexSearcher searcher, boolean needsScores, TermContext termStates) + public TermWeight(IndexSearcher searcher, int postingsFlags, TermContext termStates) throws IOException { super(TermQuery.this); - this.needsScores = needsScores; + this.postingsFlags = postingsFlags; assert termStates != null : "TermContext must not be null"; this.termStates = termStates; this.similarity = searcher.getSimilarity(); @@ -83,7 +83,7 @@ public class TermQuery extends Query { if (termsEnum == null) { return null; } - PostingsEnum docs = termsEnum.postings(acceptDocs, null, needsScores ? PostingsEnum.FLAG_FREQS : PostingsEnum.FLAG_NONE); + PostingsEnum docs = termsEnum.postings(acceptDocs, null, postingsFlags); assert docs != null; return new TermScorer(this, docs, similarity.simScorer(stats, context)); } @@ -168,7 +168,7 @@ public class TermQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { final IndexReaderContext context = searcher.getTopReaderContext(); final TermContext termState; if (perReaderTermState == null @@ -184,7 +184,7 @@ public class TermQuery extends Query { // we must not ignore the given docFreq - if set use the given value (lie) if (docFreq != -1) termState.setDocFreq(docFreq); - return new TermWeight(searcher, needsScores, termState); + return new TermWeight(searcher, postingsFlags, termState); } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/TermScorer.java b/lucene/core/src/java/org/apache/lucene/search/TermScorer.java index 70808359bf4..084bd281346 100644 --- a/lucene/core/src/java/org/apache/lucene/search/TermScorer.java +++ b/lucene/core/src/java/org/apache/lucene/search/TermScorer.java @@ -26,6 +26,7 @@ import org.apache.lucene.util.BytesRef; /** Expert: A Scorer for documents matching a Term. */ final class TermScorer extends Scorer { + private final PostingsEnum postingsEnum; private final Similarity.SimScorer docScorer; diff --git a/lucene/core/src/java/org/apache/lucene/search/Weight.java b/lucene/core/src/java/org/apache/lucene/search/Weight.java index 8ba2403078a..16fbfc5ee16 100644 --- a/lucene/core/src/java/org/apache/lucene/search/Weight.java +++ b/lucene/core/src/java/org/apache/lucene/search/Weight.java @@ -41,7 +41,7 @@ import org.apache.lucene.util.Bits; * A Weight is used in the following way: *

    *
  1. A Weight is constructed by a top-level query, given a - * IndexSearcher ({@link Query#createWeight(IndexSearcher, boolean)}). + * IndexSearcher ({@link Query#createWeight(IndexSearcher, int)}). *
  2. The {@link #getValueForNormalization()} method is called on the * Weight to compute the query normalization factor * {@link Similarity#queryNorm(float)} of the query clauses contained in the diff --git a/lucene/core/src/java/org/apache/lucene/search/package-info.java b/lucene/core/src/java/org/apache/lucene/search/package-info.java index eba4bdd2fb7..64ded263f74 100644 --- a/lucene/core/src/java/org/apache/lucene/search/package-info.java +++ b/lucene/core/src/java/org/apache/lucene/search/package-info.java @@ -358,7 +358,7 @@ * {@link org.apache.lucene.search.Query Query} class has several methods that are important for * derived classes: *
      - *
    1. {@link org.apache.lucene.search.Query#createWeight(IndexSearcher,boolean) createWeight(IndexSearcher searcher,boolean)} — A + *
    2. {@link org.apache.lucene.search.Query#createWeight(IndexSearcher,int) createWeight(IndexSearcher searcher,boolean)} — A * {@link org.apache.lucene.search.Weight Weight} is the internal representation of the * Query, so each Query implementation must * provide an implementation of Weight. See the subsection on {@link org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) rewrite(IndexReader reader)} — Rewrites queries into primitive queries. Primitive queries are: * {@link org.apache.lucene.search.TermQuery TermQuery}, * {@link org.apache.lucene.search.BooleanQuery BooleanQuery}, and other queries that implement {@link org.apache.lucene.search.Query#createWeight(IndexSearcher,boolean) createWeight(IndexSearcher searcher,boolean)}
    3. + * >and other queries that implement {@link org.apache.lucene.search.Query#createWeight(IndexSearcher,int) createWeight(IndexSearcher searcher,boolean)} *
    * *

    The Weight Interface

    @@ -509,7 +509,7 @@ *

    Assuming we are not sorting (since sorting doesn't affect the raw Lucene score), * we call one of the search methods of the IndexSearcher, passing in the * {@link org.apache.lucene.search.Weight Weight} object created by - * {@link org.apache.lucene.search.IndexSearcher#createNormalizedWeight(org.apache.lucene.search.Query,boolean) + * {@link org.apache.lucene.search.IndexSearcher#createNormalizedWeight(org.apache.lucene.search.Query,int) * IndexSearcher.createNormalizedWeight(Query,boolean)}, * {@link org.apache.lucene.search.Filter Filter} and the number of results we want. * This method returns a {@link org.apache.lucene.search.TopDocs TopDocs} object, diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java index e46bb45e85e..a9e6b93184e 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java @@ -71,7 +71,7 @@ public class PayloadNearQuery extends SpanNearQuery { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new PayloadNearSpanWeight(this, searcher); } diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java index 463a6a0f806..b208cc5c6a7 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java @@ -67,7 +67,7 @@ public class PayloadTermQuery extends SpanTermQuery { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new PayloadTermWeight(this, searcher); } diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java index 9b740f6526b..af407d787a5 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java @@ -106,8 +106,8 @@ public class FieldMaskingSpanQuery extends SpanQuery { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return maskedQuery.createWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return maskedQuery.createWeight(searcher, postingsFlags); } @Override diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java index 00bed758488..022a498cdf1 100644 --- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java @@ -42,7 +42,7 @@ public abstract class SpanQuery extends Query { public abstract String getField(); @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new SpanWeight(this, searcher); } diff --git a/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java b/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java index 843c092fa7d..2d77407a4f8 100644 --- a/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java +++ b/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java @@ -264,6 +264,11 @@ final class JustCompileSearch { throw new UnsupportedOperationException( UNSUPPORTED_MSG ); } + @Override + public boolean needsScores() { + throw new UnsupportedOperationException(UNSUPPORTED_MSG); + } + @Override public TopDocs topDocs() { throw new UnsupportedOperationException( UNSUPPORTED_MSG ); @@ -278,11 +283,6 @@ final class JustCompileSearch { public TopDocs topDocs( int start, int end ) { throw new UnsupportedOperationException( UNSUPPORTED_MSG ); } - - @Override - public boolean needsScores() { - throw new UnsupportedOperationException( UNSUPPORTED_MSG ); - } } static final class JustCompileWeight extends Weight { diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanCoord.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanCoord.java index 593a9702973..f6346a40d23 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanCoord.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanCoord.java @@ -28,6 +28,7 @@ import org.apache.lucene.index.FieldInvertState; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.store.Directory; @@ -706,7 +707,7 @@ public class TestBooleanCoord extends LuceneTestCase { /** asserts score for our single matching good doc */ private void assertScore(final float expected, Query query) throws Exception { // test in-order - Weight weight = searcher.createNormalizedWeight(query, true); + Weight weight = searcher.createNormalizedWeight(query, PostingsEnum.FLAG_FREQS); Scorer scorer = weight.scorer(reader.leaves().get(0), null); assertTrue(scorer.docID() == -1 || scorer.docID() == DocIdSetIterator.NO_MORE_DOCS); assertEquals(0, scorer.nextDoc()); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java index 2f7de6fa0cd..09194c288ee 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java @@ -185,7 +185,7 @@ public class TestBooleanOr extends LuceneTestCase { bq.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD); - Weight w = s.createNormalizedWeight(bq, true); + Weight w = s.createNormalizedWeight(bq, PostingsEnum.FLAG_FREQS); assertEquals(1, s.getIndexReader().leaves().size()); BulkScorer scorer = w.bulkScorer(s.getIndexReader().leaves().get(0), null); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java index be38c142ff0..fa4575aabdb 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java @@ -34,6 +34,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.MultiReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.similarities.DefaultSimilarity; @@ -231,7 +232,7 @@ public class TestBooleanQuery extends LuceneTestCase { q.add(new BooleanClause(new TermQuery(new Term("field", term)), BooleanClause.Occur.SHOULD)); } - Weight weight = s.createNormalizedWeight(q, true); + Weight weight = s.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); Scorer scorer = weight.scorer(s.leafContexts.get(0), null); @@ -249,7 +250,7 @@ public class TestBooleanQuery extends LuceneTestCase { // verify exact match: for(int iter2=0;iter2<10;iter2++) { - weight = s.createNormalizedWeight(q, true); + weight = s.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); scorer = weight.scorer(s.leafContexts.get(0), null); if (VERBOSE) { diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQueryVisitSubscorers.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQueryVisitSubscorers.java index 6b555db7af1..3e8c9c07766 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQueryVisitSubscorers.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQueryVisitSubscorers.java @@ -282,8 +282,8 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase { static class BooleanQuery2 extends BooleanQuery { @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return new BooleanWeight(this, searcher, needsScores, false) { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return new BooleanWeight(this, searcher, postingsFlags, false) { @Override public BulkScorer bulkScorer(LeafReaderContext context, Bits acceptDocs) throws IOException { Scorer scorer = scorer(context, acceptDocs); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java index 0aa716af7a8..569517c2797 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorer.java @@ -70,7 +70,7 @@ public class TestBooleanScorer extends LuceneTestCase { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new Weight(CrazyMustUseBulkScorerQuery.this) { @Override public Explanation explain(LeafReaderContext context, int doc) { diff --git a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java index 70ca0947791..07c7aa8b55c 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java @@ -22,6 +22,7 @@ import java.io.IOException; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.similarities.DefaultSimilarity; @@ -67,7 +68,7 @@ public class TestConstantScoreQuery extends LuceneTestCase { assertEquals("Score differs from expected", expectedScore, this.scorer.score(), 0); count[0]++; } - + @Override public boolean needsScores() { return true; diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java index 384f99efcf0..d3ea5893b2b 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java @@ -34,6 +34,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.SlowCompositeReaderWrapper; import org.apache.lucene.index.StoredDocument; @@ -178,7 +179,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase { QueryUtils.check(random(), dq, s); assertTrue(s.getTopReaderContext() instanceof LeafReaderContext); - final Weight dw = s.createNormalizedWeight(dq, true); + final Weight dw = s.createNormalizedWeight(dq, PostingsEnum.FLAG_FREQS); LeafReaderContext context = (LeafReaderContext)s.getTopReaderContext(); final Scorer ds = dw.scorer(context, context.reader().getLiveDocs()); final boolean skipOk = ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS; @@ -194,7 +195,7 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase { dq.add(tq("dek", "DOES_NOT_EXIST")); assertTrue(s.getTopReaderContext() instanceof LeafReaderContext); QueryUtils.check(random(), dq, s); - final Weight dw = s.createNormalizedWeight(dq, true); + final Weight dw = s.createNormalizedWeight(dq, PostingsEnum.FLAG_FREQS); LeafReaderContext context = (LeafReaderContext)s.getTopReaderContext(); final Scorer ds = dw.scorer(context, context.reader().getLiveDocs()); assertTrue("firsttime skipTo found no match", diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMinShouldMatch2.java b/lucene/core/src/test/org/apache/lucene/search/TestMinShouldMatch2.java index 829ba688bb3..c3f6f1abce7 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestMinShouldMatch2.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestMinShouldMatch2.java @@ -31,6 +31,7 @@ import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.StringField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.LeafReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.Term; @@ -124,7 +125,7 @@ public class TestMinShouldMatch2 extends LuceneTestCase { } bq.setMinimumNumberShouldMatch(minShouldMatch); - BooleanWeight weight = (BooleanWeight) searcher.createNormalizedWeight(bq, true); + BooleanWeight weight = (BooleanWeight) searcher.createNormalizedWeight(bq, PostingsEnum.FLAG_FREQS); switch (mode) { case DOC_VALUES: diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java index 4e36ad98e55..a3a752cf700 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java @@ -30,6 +30,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.MultiFields; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermsEnum; @@ -344,7 +345,7 @@ public class TestMultiPhraseQuery extends LuceneTestCase { MultiPhraseQuery query = new MultiPhraseQuery(); query.add(new Term[] { new Term("body", "this"), new Term("body", "that") }); query.add(new Term("body", "is")); - Weight weight = query.createWeight(searcher, true); + Weight weight = query.createWeight(searcher, PostingsEnum.FLAG_FREQS); assertEquals(10f * 10f, weight.getValueForNormalization(), 0.001f); writer.close(); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java b/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java index 67a64d94694..4b415d671cf 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java @@ -25,6 +25,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; @@ -63,15 +64,15 @@ public class TestNeedsScores extends LuceneTestCase { Query required = new TermQuery(new Term("field", "this")); Query prohibited = new TermQuery(new Term("field", "3")); BooleanQuery bq = new BooleanQuery(); - bq.add(new AssertNeedsScores(required, true), BooleanClause.Occur.MUST); - bq.add(new AssertNeedsScores(prohibited, false), BooleanClause.Occur.MUST_NOT); + bq.add(new AssertNeedsScores(required, PostingsEnum.FLAG_FREQS), BooleanClause.Occur.MUST); + bq.add(new AssertNeedsScores(prohibited, PostingsEnum.FLAG_NONE), BooleanClause.Occur.MUST_NOT); assertEquals(4, searcher.search(bq, 5).totalHits); // we exclude 3 } /** nested inside constant score query */ public void testConstantScoreQuery() throws Exception { Query term = new TermQuery(new Term("field", "this")); - Query constantScore = new ConstantScoreQuery(new AssertNeedsScores(term, false)); + Query constantScore = new ConstantScoreQuery(new AssertNeedsScores(term, PostingsEnum.FLAG_NONE)); assertEquals(5, searcher.search(constantScore, 5).totalHits); } @@ -79,38 +80,38 @@ public class TestNeedsScores extends LuceneTestCase { public void testQueryWrapperFilter() throws Exception { Query query = new MatchAllDocsQuery(); Query term = new TermQuery(new Term("field", "this")); - Filter filter = new QueryWrapperFilter(new AssertNeedsScores(term, false)); + Filter filter = new QueryWrapperFilter(new AssertNeedsScores(term, PostingsEnum.FLAG_NONE)); assertEquals(5, searcher.search(query, filter, 5).totalHits); } /** when not sorting by score */ public void testSortByField() throws Exception { - Query query = new AssertNeedsScores(new MatchAllDocsQuery(), false); + Query query = new AssertNeedsScores(new MatchAllDocsQuery(), PostingsEnum.FLAG_NONE); assertEquals(5, searcher.search(query, 5, Sort.INDEXORDER).totalHits); } /** when sorting by score */ public void testSortByScore() throws Exception { - Query query = new AssertNeedsScores(new MatchAllDocsQuery(), true); + Query query = new AssertNeedsScores(new MatchAllDocsQuery(), PostingsEnum.FLAG_FREQS); assertEquals(5, searcher.search(query, 5, Sort.RELEVANCE).totalHits); } /** - * Wraps a query, checking that the needsScores param + * Wraps a query, checking that the postingsFlags param * passed to Weight.scorer is the expected value. */ static class AssertNeedsScores extends Query { final Query in; - final boolean value; + final int value; - AssertNeedsScores(Query in, boolean value) { + AssertNeedsScores(Query in, int value) { this.in = in; this.value = value; } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - final Weight w = in.createWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + final Weight w = in.createWeight(searcher, postingsFlags); return new Weight(AssertNeedsScores.this) { @Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { @@ -129,7 +130,7 @@ public class TestNeedsScores extends LuceneTestCase { @Override public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException { - assertEquals("query=" + in, value, needsScores); + assertEquals("query=" + in, value, postingsFlags); return w.scorer(context, acceptDocs); } }; @@ -155,7 +156,7 @@ public class TestNeedsScores extends LuceneTestCase { final int prime = 31; int result = super.hashCode(); result = prime * result + ((in == null) ? 0 : in.hashCode()); - result = prime * result + (value ? 1231 : 1237); + result = prime * result + (value * 37); return result; } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java b/lucene/core/src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java index 3279ab47a2a..cf0cbf2b139 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java @@ -21,6 +21,7 @@ import java.io.IOException; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; @@ -107,7 +108,7 @@ public class TestPositiveScoresOnlyCollector extends LuceneTestCase { IndexReader ir = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(ir); - Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher, true); + Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher, PostingsEnum.FLAG_FREQS); Scorer s = new SimpleScorer(fake); TopDocsCollector tdc = TopScoreDocCollector.create(scores.length); Collector c = new PositiveScoresOnlyCollector(tdc); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java b/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java index 0aac3165da9..cc41c1da336 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java @@ -425,7 +425,7 @@ public class TestQueryRescorer extends LuceneTestCase { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new Weight(FixedScoreQuery.this) { diff --git a/lucene/core/src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java index 449acf00b6f..8b82b401e67 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java @@ -20,6 +20,7 @@ package org.apache.lucene.search; import java.io.IOException; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; @@ -130,7 +131,7 @@ public class TestScoreCachingWrappingScorer extends LuceneTestCase { IndexReader ir = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(ir); - Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher, true); + Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher, PostingsEnum.FLAG_FREQS); Scorer s = new SimpleScorer(fake); ScoreCachingCollector scc = new ScoreCachingCollector(scores.length); scc.setScorer(s); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTermQueryPostings.java b/lucene/core/src/test/org/apache/lucene/search/TestTermQueryPostings.java new file mode 100644 index 00000000000..55305bb8f44 --- /dev/null +++ b/lucene/core/src/test/org/apache/lucene/search/TestTermQueryPostings.java @@ -0,0 +1,172 @@ +package org.apache.lucene.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.analysis.MockAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.FieldType; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.DirectoryReader; +import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; +import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.SlowCompositeReaderWrapper; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Scorer; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.Weight; +import org.apache.lucene.search.payloads.PayloadHelper; +import org.apache.lucene.search.similarities.DefaultSimilarity; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.LuceneTestCase; +import org.junit.Test; + +import java.io.IOException; + +public class TestTermQueryPostings extends LuceneTestCase { + + private static final String FIELD = "f"; + + private static final String[] DOC_FIELDS = new String[]{ + "a b c d", + "a a a a", + "c d e f", + "b d a g" + }; + + @Test + public void testTermQueryPositions() throws IOException { + + Directory directory = newDirectory(); + IndexWriterConfig config = newIndexWriterConfig(new MockAnalyzer(random())); + RandomIndexWriter writer = new RandomIndexWriter(random(), directory, config); + + for (String content : DOC_FIELDS) { + Document doc = new Document(); + doc.add(newField(FIELD, content, TextField.TYPE_NOT_STORED)); + writer.addDocument(doc); + } + + + IndexReader reader = SlowCompositeReaderWrapper.wrap(writer.getReader()); + IndexSearcher searcher = new IndexSearcher(reader); + writer.close(); + + TermQuery tq = new TermQuery(new Term(FIELD, "a")); + Weight weight = searcher.createNormalizedWeight(tq, PostingsEnum.FLAG_POSITIONS); + LeafReaderContext ctx = (LeafReaderContext) searcher.getTopReaderContext(); + Scorer scorer = weight.scorer(ctx, null); + + assertEquals(scorer.nextDoc(), 0); + assertEquals(scorer.nextPosition(), 0); + + assertEquals(scorer.nextDoc(), 1); + assertEquals(scorer.nextPosition(), 0); + assertEquals(scorer.nextPosition(), 1); + assertEquals(scorer.nextPosition(), 2); + assertEquals(scorer.nextPosition(), 3); + + assertEquals(scorer.nextDoc(), 3); + assertEquals(scorer.nextPosition(), 2); + + reader.close(); + directory.close(); + + } + + @Test + public void testTermQueryOffsets() throws IOException { + + Directory directory = newDirectory(); + IndexWriterConfig config = newIndexWriterConfig(new MockAnalyzer(random())); + RandomIndexWriter writer = new RandomIndexWriter(random(), directory, config); + + FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED); + fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); + + for (String content : DOC_FIELDS) { + Document doc = new Document(); + doc.add(newField(FIELD, content, fieldType)); + writer.addDocument(doc); + } + + + IndexReader reader = SlowCompositeReaderWrapper.wrap(writer.getReader()); + IndexSearcher searcher = new IndexSearcher(reader); + writer.close(); + + TermQuery tq = new TermQuery(new Term(FIELD, "a")); + Weight weight = searcher.createNormalizedWeight(tq, PostingsEnum.FLAG_OFFSETS); + LeafReaderContext ctx = (LeafReaderContext) searcher.getTopReaderContext(); + Scorer scorer = weight.scorer(ctx, null); + + assertEquals(scorer.nextDoc(), 0); + assertEquals(scorer.nextPosition(), 0); + assertEquals(scorer.startOffset(), 0); + assertEquals(scorer.endOffset(), 1); + + assertEquals(scorer.nextDoc(), 1); + assertEquals(scorer.nextPosition(), 0); + assertEquals(scorer.startOffset(), 0); + assertEquals(scorer.endOffset(), 1); + assertEquals(scorer.nextPosition(), 1); + assertEquals(scorer.startOffset(), 2); + assertEquals(scorer.endOffset(), 3); + assertEquals(scorer.nextPosition(), 2); + assertEquals(scorer.startOffset(), 4); + assertEquals(scorer.endOffset(), 5); + assertEquals(scorer.nextPosition(), 3); + assertEquals(scorer.startOffset(), 6); + assertEquals(scorer.endOffset(), 7); + + assertEquals(scorer.nextDoc(), 3); + assertEquals(scorer.nextPosition(), 2); + assertEquals(scorer.startOffset(), 4); + assertEquals(scorer.endOffset(), 5); + + reader.close(); + directory.close(); + + } + + @Test + public void testTermQueryPayloads() throws Exception { + + PayloadHelper helper = new PayloadHelper(); + IndexSearcher searcher = helper.setUp(random(), new DefaultSimilarity(), 1000); + + TermQuery tq = new TermQuery(new Term(PayloadHelper.FIELD, "seventy")); + Weight weight = searcher.createNormalizedWeight(tq, PostingsEnum.FLAG_PAYLOADS); + + for (LeafReaderContext ctx : searcher.leafContexts) { + Scorer scorer = weight.scorer(ctx, null); + if (scorer.nextDoc() == DocIdSetIterator.NO_MORE_DOCS) + continue; + scorer.nextPosition(); + BytesRef payload = scorer.getPayload(); + assertEquals(payload.length, 1); + } + + helper.tearDown(); + } +} diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTermScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestTermScorer.java index 751b3dd0771..2f3a2693fd7 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestTermScorer.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestTermScorer.java @@ -26,6 +26,7 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.SlowCompositeReaderWrapper; import org.apache.lucene.index.Term; @@ -75,7 +76,7 @@ public class TestTermScorer extends LuceneTestCase { Term allTerm = new Term(FIELD, "all"); TermQuery termQuery = new TermQuery(allTerm); - Weight weight = indexSearcher.createNormalizedWeight(termQuery, true); + Weight weight = indexSearcher.createNormalizedWeight(termQuery, PostingsEnum.FLAG_FREQS); assertTrue(indexSearcher.getTopReaderContext() instanceof LeafReaderContext); LeafReaderContext context = (LeafReaderContext)indexSearcher.getTopReaderContext(); BulkScorer ts = weight.bulkScorer(context, context.reader().getLiveDocs()); @@ -137,7 +138,7 @@ public class TestTermScorer extends LuceneTestCase { Term allTerm = new Term(FIELD, "all"); TermQuery termQuery = new TermQuery(allTerm); - Weight weight = indexSearcher.createNormalizedWeight(termQuery, true); + Weight weight = indexSearcher.createNormalizedWeight(termQuery, PostingsEnum.FLAG_FREQS); assertTrue(indexSearcher.getTopReaderContext() instanceof LeafReaderContext); LeafReaderContext context = (LeafReaderContext) indexSearcher.getTopReaderContext(); Scorer ts = weight.scorer(context, context.reader().getLiveDocs()); @@ -156,7 +157,7 @@ public class TestTermScorer extends LuceneTestCase { Term allTerm = new Term(FIELD, "all"); TermQuery termQuery = new TermQuery(allTerm); - Weight weight = indexSearcher.createNormalizedWeight(termQuery, true); + Weight weight = indexSearcher.createNormalizedWeight(termQuery, PostingsEnum.FLAG_FREQS); assertTrue(indexSearcher.getTopReaderContext() instanceof LeafReaderContext); LeafReaderContext context = (LeafReaderContext) indexSearcher.getTopReaderContext(); Scorer ts = weight.scorer(context, context.reader().getLiveDocs()); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestTopDocsMerge.java b/lucene/core/src/test/org/apache/lucene/search/TestTopDocsMerge.java index 24e784bbafd..242e18faa99 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestTopDocsMerge.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestTopDocsMerge.java @@ -26,6 +26,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.CompositeReaderContext; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.ReaderUtil; import org.apache.lucene.index.Term; @@ -251,7 +252,7 @@ public class TestTopDocsMerge extends LuceneTestCase { } // ... then all shards: - final Weight w = searcher.createNormalizedWeight(query, true); + final Weight w = searcher.createNormalizedWeight(query, PostingsEnum.FLAG_FREQS); final TopDocs[] shardHits; if (sort == null) { diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java b/lucene/core/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java index 5eb29cf92aa..db971d65dd4 100644 --- a/lucene/core/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java +++ b/lucene/core/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java @@ -181,7 +181,7 @@ public class TestNearSpansOrdered extends LuceneTestCase { */ public void testSpanNearScorerSkipTo1() throws Exception { SpanNearQuery q = makeQuery(); - Weight w = searcher.createNormalizedWeight(q, true); + Weight w = searcher.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); IndexReaderContext topReaderContext = searcher.getTopReaderContext(); LeafReaderContext leave = topReaderContext.leaves().get(0); Scorer s = w.scorer(leave, leave.reader().getLiveDocs()); diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/TestSpans.java b/lucene/core/src/test/org/apache/lucene/search/spans/TestSpans.java index 577679d0a41..de745f40d60 100644 --- a/lucene/core/src/test/org/apache/lucene/search/spans/TestSpans.java +++ b/lucene/core/src/test/org/apache/lucene/search/spans/TestSpans.java @@ -430,7 +430,7 @@ public class TestSpans extends LuceneTestCase { slop, ordered); - spanScorer = searcher.createNormalizedWeight(snq, true).scorer(ctx, ctx.reader().getLiveDocs()); + spanScorer = searcher.createNormalizedWeight(snq, PostingsEnum.FLAG_FREQS).scorer(ctx, ctx.reader().getLiveDocs()); } finally { searcher.setSimilarity(oldSim); } diff --git a/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysQuery.java b/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysQuery.java index 8cfd3d46fbf..75328e63e6a 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysQuery.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysQuery.java @@ -73,8 +73,8 @@ class DrillSidewaysQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - final Weight baseWeight = baseQuery.createWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + final Weight baseWeight = baseQuery.createWeight(searcher, postingsFlags); final Object[] drillDowns = new Object[drillDownQueries.length]; for(int dim=0;dim termStates = new HashMap<>(); diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java index 1cced2508dc..414583c2354 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java +++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java @@ -56,8 +56,8 @@ public class AssertingIndexSearcher extends IndexSearcher { /** Ensures, that the returned {@code Weight} is not normalized again, which may produce wrong scores. */ @Override - public Weight createNormalizedWeight(Query query, boolean needsScores) throws IOException { - final Weight w = super.createNormalizedWeight(query, needsScores); + public Weight createNormalizedWeight(Query query, int postingsFlags) throws IOException { + final Weight w = super.createNormalizedWeight(query, postingsFlags); return new AssertingWeight(random, w) { @Override diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingQuery.java b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingQuery.java index ae2f9a32602..844c6f2fb80 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingQuery.java +++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingQuery.java @@ -42,8 +42,8 @@ public class AssertingQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return AssertingWeight.wrap(new Random(random.nextLong()), in.createWeight(searcher, needsScores)); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return AssertingWeight.wrap(new Random(random.nextLong()), in.createWeight(searcher, postingsFlags)); } @Override diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java b/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java index 080353752b8..9382ebbad3e 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java +++ b/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java @@ -32,6 +32,7 @@ import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.MultiReader; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.store.Directory; import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.RAMDirectory; @@ -261,7 +262,7 @@ public class QueryUtils { lastDoc[0] = doc; try { if (scorer == null) { - Weight w = s.createNormalizedWeight(q, true); + Weight w = s.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); LeafReaderContext context = readerContextArray.get(leafPtr); scorer = w.scorer(context, context.reader().getLiveDocs()); } @@ -313,7 +314,7 @@ public class QueryUtils { final LeafReader previousReader = lastReader[0]; IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader); indexSearcher.setSimilarity(s.getSimilarity()); - Weight w = indexSearcher.createNormalizedWeight(q, true); + Weight w = indexSearcher.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); LeafReaderContext ctx = (LeafReaderContext)indexSearcher.getTopReaderContext(); Scorer scorer = w.scorer(ctx, ctx.reader().getLiveDocs()); if (scorer != null) { @@ -335,7 +336,7 @@ public class QueryUtils { final LeafReader previousReader = lastReader[0]; IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader, false); indexSearcher.setSimilarity(s.getSimilarity()); - Weight w = indexSearcher.createNormalizedWeight(q, true); + Weight w = indexSearcher.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); LeafReaderContext ctx = previousReader.getContext(); Scorer scorer = w.scorer(ctx, ctx.reader().getLiveDocs()); if (scorer != null) { @@ -367,7 +368,7 @@ public class QueryUtils { try { long startMS = System.currentTimeMillis(); for (int i=lastDoc[0]+1; i<=doc; i++) { - Weight w = s.createNormalizedWeight(q, true); + Weight w = s.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); Scorer scorer = w.scorer(context.get(leafPtr), liveDocs); Assert.assertTrue("query collected "+doc+" but skipTo("+i+") says no more docs!",scorer.advance(i) != DocIdSetIterator.NO_MORE_DOCS); Assert.assertEquals("query collected "+doc+" but skipTo("+i+") got to "+scorer.docID(),doc,scorer.docID()); @@ -400,7 +401,7 @@ public class QueryUtils { final LeafReader previousReader = lastReader[0]; IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader); indexSearcher.setSimilarity(s.getSimilarity()); - Weight w = indexSearcher.createNormalizedWeight(q, true); + Weight w = indexSearcher.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); Scorer scorer = w.scorer((LeafReaderContext)indexSearcher.getTopReaderContext(), previousReader.getLiveDocs()); if (scorer != null) { boolean more = scorer.advance(lastDoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS; @@ -421,7 +422,7 @@ public class QueryUtils { final LeafReader previousReader = lastReader[0]; IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader); indexSearcher.setSimilarity(s.getSimilarity()); - Weight w = indexSearcher.createNormalizedWeight(q, true); + Weight w = indexSearcher.createNormalizedWeight(q, PostingsEnum.FLAG_FREQS); Scorer scorer = w.scorer((LeafReaderContext)indexSearcher.getTopReaderContext(), previousReader.getLiveDocs()); if (scorer != null) { boolean more = scorer.advance(lastDoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS; @@ -432,7 +433,7 @@ public class QueryUtils { /** Check that the scorer and bulk scorer advance consistently. */ public static void checkBulkScorerSkipTo(Random r, Query query, IndexSearcher searcher) throws IOException { - Weight weight = searcher.createNormalizedWeight(query, true); + Weight weight = searcher.createNormalizedWeight(query, PostingsEnum.FLAG_FREQS); for (LeafReaderContext context : searcher.getIndexReader().leaves()) { final Scorer scorer = weight.scorer(context, context.reader().getLiveDocs()); final BulkScorer bulkScorer = weight.bulkScorer(context, context.reader().getLiveDocs()); diff --git a/solr/core/src/java/org/apache/solr/schema/LatLonType.java b/solr/core/src/java/org/apache/solr/schema/LatLonType.java index f9521fbed7a..5a02026f0db 100644 --- a/solr/core/src/java/org/apache/solr/schema/LatLonType.java +++ b/solr/core/src/java/org/apache/solr/schema/LatLonType.java @@ -563,7 +563,7 @@ class SpatialDistanceQuery extends ExtendedQueryBase implements PostFilter { @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { // if we were supposed to use bboxQuery, then we should have been rewritten using that query assert bboxQuery == null; return new SpatialWeight(searcher); diff --git a/solr/core/src/java/org/apache/solr/search/ExportQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/ExportQParserPlugin.java index ad0063c5766..efa455a0609 100644 --- a/solr/core/src/java/org/apache/solr/search/ExportQParserPlugin.java +++ b/solr/core/src/java/org/apache/solr/search/ExportQParserPlugin.java @@ -80,7 +80,7 @@ public class ExportQParserPlugin extends QParserPlugin { } public Weight createWeight(IndexSearcher searcher) throws IOException { - return mainQuery.createWeight(searcher, true); + return mainQuery.createWeight(searcher, PostingsEnum.FLAG_FREQS); } public Query rewrite(IndexReader reader) throws IOException { diff --git a/solr/core/src/java/org/apache/solr/search/JoinQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/JoinQParserPlugin.java index 544cf137ebc..c9c993731ff 100644 --- a/solr/core/src/java/org/apache/solr/search/JoinQParserPlugin.java +++ b/solr/core/src/java/org/apache/solr/search/JoinQParserPlugin.java @@ -210,7 +210,7 @@ class JoinQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { return new JoinQueryWeight((SolrIndexSearcher)searcher); } diff --git a/solr/core/src/java/org/apache/solr/search/ReRankQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/ReRankQParserPlugin.java index 8438aacaca4..b6fc24d7f84 100644 --- a/solr/core/src/java/org/apache/solr/search/ReRankQParserPlugin.java +++ b/solr/core/src/java/org/apache/solr/search/ReRankQParserPlugin.java @@ -27,6 +27,7 @@ import com.carrotsearch.hppc.IntFloatOpenHashMap; import com.carrotsearch.hppc.IntIntOpenHashMap; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.Term; import org.apache.lucene.search.Explanation; import org.apache.lucene.search.IndexSearcher; @@ -171,8 +172,9 @@ public class ReRankQParserPlugin extends QParserPlugin { } - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException{ - return new ReRankWeight(mainQuery, reRankQuery, reRankWeight, searcher, needsScores); + @Override + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException{ + return new ReRankWeight(mainQuery, reRankQuery, reRankWeight, searcher, postingsFlags); } } @@ -182,12 +184,12 @@ public class ReRankQParserPlugin extends QParserPlugin { private Weight mainWeight; private double reRankWeight; - public ReRankWeight(Query mainQuery, Query reRankQuery, double reRankWeight, IndexSearcher searcher, boolean needsScores) throws IOException { + public ReRankWeight(Query mainQuery, Query reRankQuery, double reRankWeight, IndexSearcher searcher, int postingsFlags) throws IOException { super(mainQuery); this.reRankQuery = reRankQuery; this.searcher = searcher; this.reRankWeight = reRankWeight; - this.mainWeight = mainQuery.createWeight(searcher, needsScores); + this.mainWeight = mainQuery.createWeight(searcher, postingsFlags); } public float getValueForNormalization() throws IOException { diff --git a/solr/core/src/java/org/apache/solr/search/SolrConstantScoreQuery.java b/solr/core/src/java/org/apache/solr/search/SolrConstantScoreQuery.java index 770d56f6291..b85f3176968 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrConstantScoreQuery.java +++ b/solr/core/src/java/org/apache/solr/search/SolrConstantScoreQuery.java @@ -229,7 +229,7 @@ public class SolrConstantScoreQuery extends ConstantScoreQuery implements Extend } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) { try { return new SolrConstantScoreQuery.ConstantWeight(searcher); } catch (IOException e) { diff --git a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java index ececb2ca658..4b5a5094b13 100644 --- a/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java +++ b/solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java @@ -1113,7 +1113,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable,SolrIn List weights = new ArrayList<>(notCached.size()); for (Query q : notCached) { Query qq = QueryUtils.makeQueryable(q); - weights.add(createNormalizedWeight(qq, true)); + weights.add(createNormalizedWeight(qq, PostingsEnum.FLAG_FREQS)); } pf.filter = new FilterImpl(answer, weights); } else { diff --git a/solr/core/src/java/org/apache/solr/search/WrappedQuery.java b/solr/core/src/java/org/apache/solr/search/WrappedQuery.java index 974e4d00f7a..f02087dcbd4 100644 --- a/solr/core/src/java/org/apache/solr/search/WrappedQuery.java +++ b/solr/core/src/java/org/apache/solr/search/WrappedQuery.java @@ -53,8 +53,8 @@ public class WrappedQuery extends ExtendedQueryBase { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - return q.createWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + return q.createWeight(searcher, postingsFlags); } @Override diff --git a/solr/core/src/java/org/apache/solr/search/join/IgnoreAcceptDocsQuery.java b/solr/core/src/java/org/apache/solr/search/join/IgnoreAcceptDocsQuery.java index ebc5c41c7d2..441f67cec2e 100644 --- a/solr/core/src/java/org/apache/solr/search/join/IgnoreAcceptDocsQuery.java +++ b/solr/core/src/java/org/apache/solr/search/join/IgnoreAcceptDocsQuery.java @@ -53,8 +53,8 @@ public class IgnoreAcceptDocsQuery extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { - Weight inner = q.createWeight(searcher, needsScores); + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { + Weight inner = q.createWeight(searcher, postingsFlags); return new IADWeight(inner); } diff --git a/solr/core/src/java/org/apache/solr/update/DeleteByQueryWrapper.java b/solr/core/src/java/org/apache/solr/update/DeleteByQueryWrapper.java index cd84d210294..29044d6bab8 100644 --- a/solr/core/src/java/org/apache/solr/update/DeleteByQueryWrapper.java +++ b/solr/core/src/java/org/apache/solr/update/DeleteByQueryWrapper.java @@ -64,10 +64,10 @@ final class DeleteByQueryWrapper extends Query { } @Override - public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + public Weight createWeight(IndexSearcher searcher, int postingsFlags) throws IOException { final LeafReader wrapped = wrap((LeafReader) searcher.getIndexReader()); final IndexSearcher privateContext = new IndexSearcher(wrapped); - final Weight inner = in.createWeight(privateContext, needsScores); + final Weight inner = in.createWeight(privateContext, postingsFlags); return new Weight(DeleteByQueryWrapper.this) { @Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { throw new UnsupportedOperationException(); } diff --git a/solr/core/src/test/org/apache/solr/search/TestRankQueryPlugin.java b/solr/core/src/test/org/apache/solr/search/TestRankQueryPlugin.java index 34f23c1de2c..3eb7d569128 100644 --- a/solr/core/src/test/org/apache/solr/search/TestRankQueryPlugin.java +++ b/solr/core/src/test/org/apache/solr/search/TestRankQueryPlugin.java @@ -32,6 +32,7 @@ import org.apache.lucene.index.DocValues; import org.apache.lucene.index.IndexReaderContext; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.NumericDocValues; +import org.apache.lucene.index.PostingsEnum; import org.apache.lucene.index.ReaderUtil; import org.apache.lucene.search.FieldComparator; import org.apache.lucene.search.IndexSearcher; @@ -114,8 +115,9 @@ public class TestRankQueryPlugin extends QParserPlugin { return false; } - public Weight createWeight(IndexSearcher indexSearcher, boolean needsScores) throws IOException{ - return q.createWeight(indexSearcher, needsScores); + @Override + public Weight createWeight(IndexSearcher indexSearcher, int postingsFlags) throws IOException{ + return q.createWeight(indexSearcher, postingsFlags); } public void setBoost(float boost) {