You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by mi...@apache.org on 2009/06/23 20:11:43 UTC
svn commit: r787772 [1/2] - in /lucene/java/trunk: ./
contrib/miscellaneous/src/test/org/apache/lucene/index/
contrib/miscellaneous/src/test/org/apache/lucene/misc/
contrib/remote/src/java/org/apache/lucene/search/
src/java/org/apache/lucene/index/ src...
Author: mikemccand
Date: Tue Jun 23 18:11:42 2009
New Revision: 787772
URL: http://svn.apache.org/viewvc?rev=787772&view=rev
Log:
LUCENE-1630: switch from Weight (interface) to QueryWeight (abstract class); mate in/out-of docID order scoring between Collector & Scorer
Added:
lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java (with props)
lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java (with props)
Modified:
lucene/java/trunk/CHANGES.txt
lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java
lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java
lucene/java/trunk/contrib/remote/src/java/org/apache/lucene/search/RemoteSearchable.java
lucene/java/trunk/src/java/org/apache/lucene/index/DocumentsWriter.java
lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer2.java
lucene/java/trunk/src/java/org/apache/lucene/search/Collector.java
lucene/java/trunk/src/java/org/apache/lucene/search/ConstantScoreQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/ExactPhraseScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/HitCollectorWrapper.java
lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java
lucene/java/trunk/src/java/org/apache/lucene/search/IndexSearcher.java
lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java
lucene/java/trunk/src/java/org/apache/lucene/search/ParallelMultiSearcher.java
lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/PhraseScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/PositiveScoresOnlyCollector.java
lucene/java/trunk/src/java/org/apache/lucene/search/Query.java
lucene/java/trunk/src/java/org/apache/lucene/search/QueryWrapperFilter.java
lucene/java/trunk/src/java/org/apache/lucene/search/Searchable.java
lucene/java/trunk/src/java/org/apache/lucene/search/Searcher.java
lucene/java/trunk/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/TermQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/TermScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/TimeLimitingCollector.java
lucene/java/trunk/src/java/org/apache/lucene/search/TopFieldCollector.java
lucene/java/trunk/src/java/org/apache/lucene/search/TopScoreDocCollector.java
lucene/java/trunk/src/java/org/apache/lucene/search/Weight.java
lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/payloads/BoostingTermQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/spans/SpanQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/spans/SpanScorer.java
lucene/java/trunk/src/java/org/apache/lucene/search/spans/SpanWeight.java
lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderReopen.java
lucene/java/trunk/src/test/org/apache/lucene/index/TestOmitTf.java
lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java
lucene/java/trunk/src/test/org/apache/lucene/search/JustCompileSearch.java
lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestCustomSearcherSort.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestDocBoost.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiTermConstantScore.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestScorerPerf.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestSetNorm.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestSimilarity.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestTermScorer.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestTimeLimitingCollector.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestTopDocsCollector.java
lucene/java/trunk/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java
lucene/java/trunk/src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java
Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Tue Jun 23 18:11:42 2009
@@ -53,6 +53,17 @@
which was unlikely done, because there is no possibility to change
Lucene's FieldCache implementation. (Grant Ingersoll, Uwe Schindler)
+ 3. LUCENE-1630: Deprecate Weight in favor of QueryWeight: added
+ matching methods to Searcher to take QueryWeight and deprecated
+ those taking Weight. If you have a Weight implementation, you can
+ turn it into a QueryWeight with QueryWeightWrapper (will be
+ removed in 3.0). All of the Weight-based methods were implemented
+ by calling the QueryWeight variants by wrapping the given Weight.
+ Going forward Searchable will be kept for convenience only and may
+ be changed between minor releases without any deprecation
+ process. It is not recommended to implement it, but rather extend
+ Searcher. (Shai Erera via Mike McCandless)
+
Changes in runtime behavior
1. LUCENE-1424: QueryParser now by default uses constant score query
@@ -225,6 +236,24 @@
NumericRangeQuery and its new indexing format for numeric or
date values. (Uwe Schindler)
+23. LUCENE-1630: Deprecate Weight in favor of QueryWeight, which adds
+ a scorer(IndexReader, boolean /* scoreDocsInOrder */, boolean /*
+ topScorer */) method instead of scorer(IndexReader) (now
+ deprecated). The new method is used by IndexSearcher to mate
+ between Collector and Scorer orderness of doc IDs. Some Scorers
+ (like BooleanScorer) are much more efficient if out-of-order
+ documents scoring is allowed by a Collector. Collector must now
+ implement acceptsDocsOutOfOrder. If you write a Collector which
+ does not care about doc ID orderness, it is recommended that you
+ return true. QueryWeight has the scoresDocsOutOfOrder method,
+ which by default returns false. If you create a QueryWeight which
+ will score documents out of order if that's requested, you should
+ override that method to return true. Also deprecated
+ BooleanQuery's setAllowDocsOutOfOrder and getAllowDocsOutOfOrder
+ as they are not needed anymore. BooleanQuery will now score docs
+ out of order when used with a Collector that can accept docs out
+ of order. (Shai Erera via Mike McCandless)
+
Bug fixes
1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()
Modified: lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java (original)
+++ lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/index/TestFieldNormModifier.java Tue Jun 23 18:11:42 2009
@@ -146,6 +146,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
});
searcher.close();
@@ -174,6 +177,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
});
searcher.close();
@@ -219,6 +225,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
});
searcher.close();
Modified: lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java (original)
+++ lucene/java/trunk/contrib/miscellaneous/src/test/org/apache/lucene/misc/TestLengthNormModifier.java Tue Jun 23 18:11:42 2009
@@ -153,6 +153,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
});
searcher.close();
@@ -187,6 +190,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
});
searcher.close();
Modified: lucene/java/trunk/contrib/remote/src/java/org/apache/lucene/search/RemoteSearchable.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/remote/src/java/org/apache/lucene/search/RemoteSearchable.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/contrib/remote/src/java/org/apache/lucene/search/RemoteSearchable.java (original)
+++ lucene/java/trunk/contrib/remote/src/java/org/apache/lucene/search/RemoteSearchable.java Tue Jun 23 18:11:42 2009
@@ -48,11 +48,16 @@
/** @deprecated use {@link #search(Weight, Filter, Collector)} instead. */
public void search(Weight weight, Filter filter, HitCollector results)
throws IOException {
- local.search(weight, filter, results);
+ search(new QueryWeightWrapper(weight), filter, new HitCollectorWrapper(results));
}
public void search(Weight weight, Filter filter, Collector results)
throws IOException {
+ search(new QueryWeightWrapper(weight), filter, results);
+ }
+
+ public void search(QueryWeight weight, Filter filter, Collector results)
+ throws IOException {
local.search(weight, filter, results);
}
@@ -74,11 +79,19 @@
}
public TopDocs search(Weight weight, Filter filter, int n) throws IOException {
+ return search(new QueryWeightWrapper(weight), filter, n);
+ }
+
+ public TopDocs search(QueryWeight weight, Filter filter, int n) throws IOException {
return local.search(weight, filter, n);
}
-
- public TopFieldDocs search (Weight weight, Filter filter, int n, Sort sort)
+ public TopFieldDocs search(Weight weight, Filter filter, int n, Sort sort)
+ throws IOException {
+ return search(new QueryWeightWrapper(weight), filter, n, sort);
+ }
+
+ public TopFieldDocs search(QueryWeight weight, Filter filter, int n, Sort sort)
throws IOException {
return local.search (weight, filter, n, sort);
}
@@ -96,6 +109,10 @@
}
public Explanation explain(Weight weight, int doc) throws IOException {
+ return explain(new QueryWeightWrapper(weight), doc);
+ }
+
+ public Explanation explain(QueryWeight weight, int doc) throws IOException {
return local.explain(weight, doc);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/index/DocumentsWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/DocumentsWriter.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/DocumentsWriter.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/DocumentsWriter.java Tue Jun 23 18:11:42 2009
@@ -17,27 +17,27 @@
* limitations under the License.
*/
-import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.search.Similarity;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Scorer;
-import org.apache.lucene.search.Weight;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.AlreadyClosedException;
-import org.apache.lucene.util.ArrayUtil;
-
import java.io.IOException;
import java.io.PrintStream;
+import java.text.NumberFormat;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map.Entry;
-import java.text.NumberFormat;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.QueryWeight;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Similarity;
+import org.apache.lucene.store.AlreadyClosedException;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.ArrayUtil;
/**
* This class accepts multiple added documents and directly
@@ -172,7 +172,7 @@
void setNext(DocWriter next) {
this.next = next;
}
- };
+ }
/**
* The IndexingChain must define the {@link #getChain(DocumentsWriter)} method
@@ -303,7 +303,7 @@
synchronized void setAllowMinus1Position() {
for(int i=0;i<threadStates.length;i++)
- threadStates[i].docState.allowMinus1Position = true;;
+ threadStates[i].docState.allowMinus1Position = true;
}
/** Set how much RAM we can use before flushing. */
@@ -989,8 +989,8 @@
Entry entry = (Entry) iter.next();
Query query = (Query) entry.getKey();
int limit = ((Integer) entry.getValue()).intValue();
- Weight weight = query.weight(searcher);
- Scorer scorer = weight.scorer(reader);
+ QueryWeight weight = query.queryWeight(searcher);
+ Scorer scorer = weight.scorer(reader, true, false);
while(true) {
int doc = scorer.nextDoc();
if (((long) docIDStart) + doc >= limit)
@@ -1144,7 +1144,7 @@
/* Initial chunks size of the shared byte[] blocks used to
store postings data */
final static int BYTE_BLOCK_SHIFT = 15;
- final static int BYTE_BLOCK_SIZE = (int) (1 << BYTE_BLOCK_SHIFT);
+ final static int BYTE_BLOCK_SIZE = 1 << BYTE_BLOCK_SHIFT;
final static int BYTE_BLOCK_MASK = BYTE_BLOCK_SIZE - 1;
final static int BYTE_BLOCK_NOT_MASK = ~BYTE_BLOCK_MASK;
@@ -1187,7 +1187,7 @@
/* Initial chunks size of the shared int[] blocks used to
store postings data */
final static int INT_BLOCK_SHIFT = 13;
- final static int INT_BLOCK_SIZE = (int) (1 << INT_BLOCK_SHIFT);
+ final static int INT_BLOCK_SIZE = 1 << INT_BLOCK_SHIFT;
final static int INT_BLOCK_MASK = INT_BLOCK_SIZE - 1;
private ArrayList freeIntBlocks = new ArrayList();
@@ -1234,7 +1234,7 @@
/* Initial chunk size of the shared char[] blocks used to
store term text */
final static int CHAR_BLOCK_SHIFT = 14;
- final static int CHAR_BLOCK_SIZE = (int) (1 << CHAR_BLOCK_SHIFT);
+ final static int CHAR_BLOCK_SIZE = 1 << CHAR_BLOCK_SHIFT;
final static int CHAR_BLOCK_MASK = CHAR_BLOCK_SIZE - 1;
final static int MAX_TERM_LENGTH = CHAR_BLOCK_SIZE-1;
@@ -1283,7 +1283,7 @@
void balanceRAM() {
// We flush when we've used our target usage
- final long flushTrigger = (long) ramBufferSize;
+ final long flushTrigger = ramBufferSize;
if (numBytesAlloc > freeTrigger) {
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java Tue Jun 23 18:11:42 2009
@@ -30,7 +30,6 @@
*/
public class BooleanQuery extends Query {
-
private static int maxClauseCount = 1024;
/** Thrown when an attempt is made to add more than {@link
@@ -173,7 +172,7 @@
/** Returns the list of clauses in this query. */
public List clauses() { return clauses; }
- private class BooleanWeight implements Weight {
+ private class BooleanWeight extends QueryWeight {
protected Similarity similarity;
protected ArrayList weights;
@@ -183,7 +182,7 @@
weights = new ArrayList(clauses.size());
for (int i = 0 ; i < clauses.size(); i++) {
BooleanClause c = (BooleanClause)clauses.get(i);
- weights.add(c.getQuery().createWeight(searcher));
+ weights.add(c.getQuery().createQueryWeight(searcher));
}
}
@@ -194,7 +193,7 @@
float sum = 0.0f;
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.get(i);
- Weight w = (Weight)weights.get(i);
+ QueryWeight w = (QueryWeight)weights.get(i);
// call sumOfSquaredWeights for all clauses in case of side effects
float s = w.sumOfSquaredWeights(); // sum sub weights
if (!c.isProhibited())
@@ -210,39 +209,13 @@
public void normalize(float norm) {
norm *= getBoost(); // incorporate boost
- for (int i = 0 ; i < weights.size(); i++) {
- Weight w = (Weight)weights.get(i);
+ for (Iterator iter = weights.iterator(); iter.hasNext();) {
+ QueryWeight w = (QueryWeight) iter.next();
// normalize all clauses, (even if prohibited in case of side affects)
w.normalize(norm);
}
}
- /**
- * @return Returns BooleanScorer2 that uses and provides advance(), and
- * scores documents in document number order.
- */
- public Scorer scorer(IndexReader reader) throws IOException {
- // TODO (3.0): instantiate either BS or BS2, according to
- // allowDocsOutOfOrder (basically, try to inline BS2.score(Collector)'s
- // logic.
-
- BooleanScorer2 result = new BooleanScorer2(similarity,
- minNrShouldMatch,
- allowDocsOutOfOrder);
-
- for (int i = 0 ; i < weights.size(); i++) {
- BooleanClause c = (BooleanClause)clauses.get(i);
- Weight w = (Weight)weights.get(i);
- Scorer subScorer = w.scorer(reader);
- if (subScorer != null)
- result.add(subScorer, c.isRequired(), c.isProhibited());
- else if (c.isRequired())
- return null;
- }
-
- return result;
- }
-
public Explanation explain(IndexReader reader, int doc)
throws IOException {
final int minShouldMatch =
@@ -256,7 +229,7 @@
int shouldMatchCount = 0;
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.get(i);
- Weight w = (Weight)weights.get(i);
+ QueryWeight w = (QueryWeight)weights.get(i);
Explanation e = w.explain(reader, doc);
if (!c.isProhibited()) maxCoord++;
if (e.isMatch()) {
@@ -310,41 +283,101 @@
return result;
}
}
+
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer)
+ throws IOException {
+ List required = new ArrayList();
+ List prohibited = new ArrayList();
+ List optional = new ArrayList();
+ for (Iterator wIter = weights.iterator(), cIter = clauses.iterator(); wIter.hasNext();) {
+ QueryWeight w = (QueryWeight) wIter.next();
+ BooleanClause c = (BooleanClause) cIter.next();
+ Scorer subScorer = w.scorer(reader, true, false);
+ if (subScorer == null) {
+ return null;
+ } else if (c.isRequired()) {
+ required.add(subScorer);
+ } else if (c.isProhibited()) {
+ prohibited.add(subScorer);
+ } else {
+ optional.add(subScorer);
+ }
+ }
+
+ // Check if we can return a BooleanScorer
+ scoreDocsInOrder |= !allowDocsOutOfOrder; // until it is removed, factor in the static setting.
+ if (!scoreDocsInOrder && topScorer && required.size() == 0 && prohibited.size() < 32) {
+ return new BooleanScorer(similarity, minNrShouldMatch, optional, prohibited);
+ }
+
+ // Return a BooleanScorer2
+ return new BooleanScorer2(similarity, minNrShouldMatch, required, prohibited, optional);
+ }
+
+ public boolean scoresDocsOutOfOrder() {
+ int numProhibited = 0;
+ for (Iterator cIter = clauses.iterator(); cIter.hasNext();) {
+ BooleanClause c = (BooleanClause) cIter.next();
+ if (c.isRequired()) {
+ return false; // BS2 (in-order) will be used by scorer()
+ } else if (c.isProhibited()) {
+ ++numProhibited;
+ }
+ }
+
+ if (numProhibited > 32) { // cannot use BS
+ return false;
+ }
+
+ // scorer() will return an out-of-order scorer if requested.
+ return true;
+ }
+
}
- /** Whether hit docs may be collected out of docid order. */
- private static boolean allowDocsOutOfOrder = false;
+ /**
+ * Whether hit docs may be collected out of docid order.
+ *
+ * @deprecated this will not be needed anymore, as
+ * {@link QueryWeight#scoresDocsOutOfOrder()} is used.
+ */
+ private static boolean allowDocsOutOfOrder = true;
/**
- * Expert: Indicates whether hit docs may be collected out of docid
- * order.
- *
+ * Expert: Indicates whether hit docs may be collected out of docid order.
+ *
* <p>
* Background: although the contract of the Scorer class requires that
* documents be iterated in order of doc id, this was not true in early
- * versions of Lucene. Many pieces of functionality in the current
- * Lucene code base have undefined behavior if this contract is not
- * upheld, but in some specific simple cases may be faster. (For
- * example: disjunction queries with less than 32 prohibited clauses;
- * This setting has no effect for other queries.)
+ * versions of Lucene. Many pieces of functionality in the current Lucene code
+ * base have undefined behavior if this contract is not upheld, but in some
+ * specific simple cases may be faster. (For example: disjunction queries with
+ * less than 32 prohibited clauses; This setting has no effect for other
+ * queries.)
* </p>
- *
+ *
* <p>
- * Specifics: By setting this option to true, docid N might be scored
- * for a single segment before docid N-1. Across multiple segments,
- * docs may be scored out of order regardless of this setting - it only
- * applies to scoring a single segment.
+ * Specifics: By setting this option to true, docid N might be scored for a
+ * single segment before docid N-1. Across multiple segments, docs may be
+ * scored out of order regardless of this setting - it only applies to scoring
+ * a single segment.
*
* Being static, this setting is system wide.
* </p>
+ *
+ * @deprecated this is not needed anymore, as
+ * {@link QueryWeight#scoresDocsOutOfOrder()} is used.
*/
public static void setAllowDocsOutOfOrder(boolean allow) {
allowDocsOutOfOrder = allow;
- }
-
+ }
+
/**
* Whether hit docs may be collected out of docid order.
+ *
* @see #setAllowDocsOutOfOrder(boolean)
+ * @deprecated this is not needed anymore, as
+ * {@link QueryWeight#scoresDocsOutOfOrder()} is used.
*/
public static boolean getAllowDocsOutOfOrder() {
return allowDocsOutOfOrder;
@@ -364,7 +397,7 @@
return getAllowDocsOutOfOrder();
}
- protected Weight createWeight(Searcher searcher) throws IOException {
+ public QueryWeight createQueryWeight(Searcher searcher) throws IOException {
return new BooleanWeight(searcher);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer.java Tue Jun 23 18:11:42 2009
@@ -92,9 +92,15 @@
public void setNextReader(IndexReader reader, int docBase) {
// not needed by this implementation
}
+
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+
+ public boolean acceptsDocsOutOfOrder() {
+ return true;
+ }
+
}
// An internal class which is used in score(Collector, int) for setting the
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer2.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer2.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer2.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/BooleanScorer2.java Tue Jun 23 18:11:42 2009
@@ -30,9 +30,10 @@
* <br>Uses ConjunctionScorer, DisjunctionScorer, ReqOptScorer and ReqExclScorer.
*/
class BooleanScorer2 extends Scorer {
- private ArrayList requiredScorers = new ArrayList();
- private ArrayList optionalScorers = new ArrayList();
- private ArrayList prohibitedScorers = new ArrayList();
+
+ private final List requiredScorers;
+ private final List optionalScorers;
+ private final List prohibitedScorers;
private class Coordinator {
float[] coordFactors = null;
@@ -54,90 +55,52 @@
/** The scorer to which all scoring will be delegated,
* except for computing and using the coordination factor.
*/
- private Scorer countingSumScorer = null;
+ private final Scorer countingSumScorer;
/** The number of optionalScorers that need to match (if there are any) */
private final int minNrShouldMatch;
- /** Whether it is allowed to return documents out of order.
- * This can accelerate the scoring of disjunction queries.
- */
- private boolean allowDocsOutOfOrder;
-
private int doc = -1;
- /** Create a BooleanScorer2.
- * @param similarity The similarity to be used.
- * @param minNrShouldMatch The minimum number of optional added scorers
- * that should match during the search.
- * In case no required scorers are added,
- * at least one of the optional scorers will have to
- * match during the search.
- * @param allowDocsOutOfOrder Whether it is allowed to return documents out of order.
- * This can accelerate the scoring of disjunction queries.
+ /**
+ * Creates a {@link Scorer} with the given similarity and lists of required,
+ * prohibited and optional scorers. In no required scorers are added, at least
+ * one of the optional scorers will have to match during the search.
+ *
+ * @param similarity
+ * The similarity to be used.
+ * @param minNrShouldMatch
+ * The minimum number of optional added scorers that should match
+ * during the search. In case no required scorers are added, at least
+ * one of the optional scorers will have to match during the search.
+ * @param required
+ * the list of required scorers.
+ * @param prohibited
+ * the list of prohibited scorers.
+ * @param optional
+ * the list of optional scorers.
*/
- public BooleanScorer2(Similarity similarity, int minNrShouldMatch, boolean allowDocsOutOfOrder) throws IOException {
+ public BooleanScorer2(Similarity similarity, int minNrShouldMatch,
+ List required, List prohibited, List optional) throws IOException {
super(similarity);
if (minNrShouldMatch < 0) {
throw new IllegalArgumentException("Minimum number of optional scorers should not be negative");
}
coordinator = new Coordinator();
this.minNrShouldMatch = minNrShouldMatch;
- this.allowDocsOutOfOrder = allowDocsOutOfOrder;
- }
-
- /** Create a BooleanScorer2.
- * In no required scorers are added,
- * at least one of the optional scorers will have to match during the search.
- * @param similarity The similarity to be used.
- * @param minNrShouldMatch The minimum number of optional added scorers
- * that should match during the search.
- * In case no required scorers are added,
- * at least one of the optional scorers will have to
- * match during the search.
- */
- public BooleanScorer2(Similarity similarity, int minNrShouldMatch) throws IOException {
- this(similarity, minNrShouldMatch, false);
- }
-
- /** Create a BooleanScorer2.
- * In no required scorers are added,
- * at least one of the optional scorers will have to match during the search.
- * @param similarity The similarity to be used.
- */
- public BooleanScorer2(Similarity similarity) throws IOException {
- this(similarity, 0, false);
- }
- public void add(final Scorer scorer, boolean required, boolean prohibited) throws IOException {
- if (!prohibited) {
- coordinator.maxCoord++;
- }
-
- if (required) {
- if (prohibited) {
- throw new IllegalArgumentException("scorer cannot be required and prohibited");
- }
- requiredScorers.add(scorer);
- } else if (prohibited) {
- prohibitedScorers.add(scorer);
- } else {
- optionalScorers.add(scorer);
- }
- }
+ optionalScorers = optional;
+ coordinator.maxCoord += optional.size();
- /** Initialize the match counting scorer that sums all the
- * scores. <p>
- * When "counting" is used in a name it means counting the number
- * of matching scorers.<br>
- * When "sum" is used in a name it means score value summing
- * over the matching scorers
- */
- private void initCountingSumScorer() throws IOException {
+ requiredScorers = required;
+ coordinator.maxCoord += required.size();
+
+ prohibitedScorers = prohibited;
+
coordinator.init();
countingSumScorer = makeCountingSumScorer();
}
-
+
/** Count a scorer as a single match. */
private class SingleMatchScorer extends Scorer {
private Scorer scorer;
@@ -333,19 +296,10 @@
* <br>When this method is used the {@link #explain(int)} method should not be used.
*/
public void score(Collector collector) throws IOException {
- if (allowDocsOutOfOrder && requiredScorers.size() == 0
- && prohibitedScorers.size() < 32) {
- new BooleanScorer(getSimilarity(), minNrShouldMatch, optionalScorers,
- prohibitedScorers).score(collector);
- } else {
- if (countingSumScorer == null) {
- initCountingSumScorer();
- }
- collector.setScorer(this);
- int doc;
- while ((doc = countingSumScorer.nextDoc()) != NO_MORE_DOCS) {
- collector.collect(doc);
- }
+ collector.setScorer(this);
+ int doc;
+ while ((doc = countingSumScorer.nextDoc()) != NO_MORE_DOCS) {
+ collector.collect(doc);
}
}
@@ -386,9 +340,6 @@
}
public int nextDoc() throws IOException {
- if (countingSumScorer == null) {
- initCountingSumScorer();
- }
return doc = countingSumScorer.nextDoc();
}
@@ -404,9 +355,6 @@
}
public int advance(int target) throws IOException {
- if (countingSumScorer == null) {
- initCountingSumScorer();
- }
return doc = countingSumScorer.advance(target);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/Collector.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/Collector.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/Collector.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/Collector.java Tue Jun 23 18:11:42 2009
@@ -121,6 +121,8 @@
*
* <p><b>NOTE:</b> This API is experimental and might change
* in incompatible ways in the next release.</p>
+ *
+ * @since 2.9
*/
public abstract class Collector {
@@ -156,5 +158,17 @@
* @param docBase
*/
public abstract void setNextReader(IndexReader reader, int docBase) throws IOException;
+
+ /**
+ * Returns true iff this {@link Collector} can accept documents given to
+ * {@link #collect(int)} out of order.
+ * <p>
+ * NOTE: some collectors can work in either mode, with a more efficient
+ * implementation for in-order docs collection. If your collector can work in
+ * either mode, it is recommended that you create two variants of it, since
+ * some queries work much faster if out-of-order collection is supported by a
+ * {@link Collector}.
+ */
+ public abstract boolean acceptsDocsOutOfOrder();
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/ConstantScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/ConstantScoreQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/ConstantScoreQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/ConstantScoreQuery.java Tue Jun 23 18:11:42 2009
@@ -50,11 +50,11 @@
// but may not be OK for highlighting
}
- protected class ConstantWeight implements Weight {
+ protected class ConstantWeight extends QueryWeight {
private Similarity similarity;
private float queryNorm;
private float queryWeight;
-
+
public ConstantWeight(Searcher searcher) {
this.similarity = getSimilarity(searcher);
}
@@ -77,13 +77,13 @@
queryWeight *= this.queryNorm;
}
- public Scorer scorer(IndexReader reader) throws IOException {
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
return new ConstantScorer(similarity, reader, this);
}
public Explanation explain(IndexReader reader, int doc) throws IOException {
- ConstantScorer cs = (ConstantScorer)scorer(reader);
+ ConstantScorer cs = (ConstantScorer) scorer(reader, true, false);
boolean exists = cs.docIdSetIterator.advance(doc) == doc;
ComplexExplanation result = new ComplexExplanation();
@@ -110,7 +110,7 @@
final float theScore;
int doc = -1;
- public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException {
+ public ConstantScorer(Similarity similarity, IndexReader reader, QueryWeight w) throws IOException {
super(similarity);
theScore = w.getValue();
docIdSetIterator = filter.getDocIdSet(reader).iterator();
@@ -152,7 +152,7 @@
}
}
- protected Weight createWeight(Searcher searcher) {
+ public QueryWeight createQueryWeight(Searcher searcher) {
return new ConstantScoreQuery.ConstantWeight(searcher);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java Tue Jun 23 18:11:42 2009
@@ -86,7 +86,7 @@
}
/* The Weight for DisjunctionMaxQuery's, used to normalize, score and explain these queries */
- private class DisjunctionMaxWeight implements Weight {
+ private class DisjunctionMaxWeight extends QueryWeight {
private Similarity similarity; // The similarity which we are associated.
private ArrayList weights = new ArrayList(); // The Weight's for our subqueries, in 1-1 correspondence with disjuncts
@@ -94,8 +94,9 @@
/* Construct the Weight for this Query searched by searcher. Recursively construct subquery weights. */
public DisjunctionMaxWeight(Searcher searcher) throws IOException {
this.similarity = searcher.getSimilarity();
- for (int i = 0; i < disjuncts.size(); i++)
- weights.add(((Query) disjuncts.get(i)).createWeight(searcher));
+ for (Iterator iter = disjuncts.iterator(); iter.hasNext();) {
+ weights.add(((Query) iter.next()).createQueryWeight(searcher));
+ }
}
/* Return our associated DisjunctionMaxQuery */
@@ -107,28 +108,32 @@
/* Compute the sub of squared weights of us applied to our subqueries. Used for normalization. */
public float sumOfSquaredWeights() throws IOException {
float max = 0.0f, sum = 0.0f;
- for (int i = 0; i < weights.size(); i++) {
- float sub = ((Weight) weights.get(i)).sumOfSquaredWeights();
+ for (Iterator iter = weights.iterator(); iter.hasNext();) {
+ float sub = ((QueryWeight) iter.next()).sumOfSquaredWeights();
sum += sub;
max = Math.max(max, sub);
+
}
- return (((sum - max) * tieBreakerMultiplier * tieBreakerMultiplier) + max) * getBoost() * getBoost();
+ float boost = getBoost();
+ return (((sum - max) * tieBreakerMultiplier * tieBreakerMultiplier) + max) * boost * boost;
}
/* Apply the computed normalization factor to our subqueries */
public void normalize(float norm) {
norm *= getBoost(); // Incorporate our boost
- for (int i = 0 ; i < weights.size(); i++)
- ((Weight) weights.get(i)).normalize(norm);
+ for (Iterator iter = weights.iterator(); iter.hasNext();) {
+ ((QueryWeight) iter.next()).normalize(norm);
+ }
}
/* Create the scorer used to score our associated DisjunctionMaxQuery */
- public Scorer scorer(IndexReader reader) throws IOException {
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
+ boolean topScorer) throws IOException {
Scorer[] scorers = new Scorer[weights.size()];
int idx = 0;
for (Iterator iter = weights.iterator(); iter.hasNext();) {
- Weight w = (Weight) iter.next();
- Scorer subScorer = w.scorer(reader);
+ QueryWeight w = (QueryWeight) iter.next();
+ Scorer subScorer = w.scorer(reader, true, false);
if (subScorer == null) {
return null;
} else if (subScorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
@@ -142,12 +147,12 @@
/* Explain the score we computed for doc */
public Explanation explain(IndexReader reader, int doc) throws IOException {
- if ( disjuncts.size() == 1) return ((Weight) weights.get(0)).explain(reader,doc);
+ if (disjuncts.size() == 1) return ((QueryWeight) weights.get(0)).explain(reader,doc);
ComplexExplanation result = new ComplexExplanation();
float max = 0.0f, sum = 0.0f;
result.setDescription(tieBreakerMultiplier == 0.0f ? "max of:" : "max plus " + tieBreakerMultiplier + " times others of:");
- for (int i = 0 ; i < weights.size(); i++) {
- Explanation e = ((Weight) weights.get(i)).explain(reader, doc);
+ for (Iterator iter = weights.iterator(); iter.hasNext();) {
+ Explanation e = ((QueryWeight) iter.next()).explain(reader, doc);
if (e.isMatch()) {
result.setMatch(Boolean.TRUE);
result.addDetail(e);
@@ -155,14 +160,14 @@
max = Math.max(max, e.getValue());
}
}
- result.setValue(max + (sum - max)*tieBreakerMultiplier);
+ result.setValue(max + (sum - max) * tieBreakerMultiplier);
return result;
}
-
+
} // end of DisjunctionMaxWeight inner class
- /* Create the Weight used to score us */
- protected Weight createWeight(Searcher searcher) throws IOException {
+ /* Create the QueryWeight used to score us */
+ public QueryWeight createQueryWeight(Searcher searcher) throws IOException {
return new DisjunctionMaxWeight(searcher);
}
@@ -170,7 +175,8 @@
* @param reader the IndexReader we query
* @return an optimized copy of us (which may not be a copy if there is nothing to optimize) */
public Query rewrite(IndexReader reader) throws IOException {
- if (disjuncts.size() == 1) {
+ int numDisjunctions = disjuncts.size();
+ if (numDisjunctions == 1) {
Query singleton = (Query) disjuncts.get(0);
Query result = singleton.rewrite(reader);
if (getBoost() != 1.0f) {
@@ -180,7 +186,7 @@
return result;
}
DisjunctionMaxQuery clone = null;
- for (int i = 0 ; i < disjuncts.size(); i++) {
+ for (int i = 0 ; i < numDisjunctions; i++) {
Query clause = (Query) disjuncts.get(i);
Query rewrite = clause.rewrite(reader);
if (rewrite != clause) {
@@ -200,15 +206,13 @@
return clone;
}
-
// inherit javadoc
public void extractTerms(Set terms) {
- for (int i = 0; i < disjuncts.size(); i++) {
- ((Query)disjuncts.get(i)).extractTerms(terms);
- }
+ for (Iterator iter = disjuncts.iterator(); iter.hasNext();) {
+ ((Query) iter.next()).extractTerms(terms);
+ }
}
-
/** Prettyprint us.
* @param field the field to which we are applied
* @return a string that shows what we do, of the form "(disjunct1 | disjunct2 | ... | disjunctn)^boost"
@@ -216,7 +220,8 @@
public String toString(String field) {
StringBuffer buffer = new StringBuffer();
buffer.append("(");
- for (int i = 0 ; i < disjuncts.size(); i++) {
+ int numDisjunctions = disjuncts.size();
+ for (int i = 0 ; i < numDisjunctions; i++) {
Query subquery = (Query) disjuncts.get(i);
if (subquery instanceof BooleanQuery) { // wrap sub-bools in parens
buffer.append("(");
@@ -224,7 +229,7 @@
buffer.append(")");
}
else buffer.append(subquery.toString(field));
- if (i != disjuncts.size()-1) buffer.append(" | ");
+ if (i != numDisjunctions-1) buffer.append(" | ");
}
buffer.append(")");
if (tieBreakerMultiplier != 0.0f) {
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/ExactPhraseScorer.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/ExactPhraseScorer.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/ExactPhraseScorer.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/ExactPhraseScorer.java Tue Jun 23 18:11:42 2009
@@ -22,8 +22,8 @@
final class ExactPhraseScorer extends PhraseScorer {
- ExactPhraseScorer(Weight weight, TermPositions[] tps, int[] offsets, Similarity similarity,
- byte[] norms) {
+ ExactPhraseScorer(QueryWeight weight, TermPositions[] tps, int[] offsets,
+ Similarity similarity, byte[] norms) {
super(weight, tps, offsets, similarity, norms);
}
@@ -43,13 +43,13 @@
while (first.position < last.position) { // scan forward in first
do {
if (!first.nextPosition())
- return (float)freq;
+ return freq;
} while (first.position < last.position);
firstToLast();
}
freq++; // all equal: a match
} while (last.nextPosition());
- return (float)freq;
+ return freq;
}
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java Tue Jun 23 18:11:42 2009
@@ -54,16 +54,14 @@
this.filter = filter;
}
-
-
/**
* Returns a Weight that applies the filter to the enclosed query's Weight.
* This is accomplished by overriding the Scorer returned by the Weight.
*/
- protected Weight createWeight (final Searcher searcher) throws IOException {
- final Weight weight = query.createWeight (searcher);
+ public QueryWeight createQueryWeight(final Searcher searcher) throws IOException {
+ final QueryWeight weight = query.createQueryWeight (searcher);
final Similarity similarity = query.getSimilarity(searcher);
- return new Weight() {
+ return new QueryWeight() {
private float value;
// pass these methods through to enclosed query's weight
@@ -99,8 +97,9 @@
public Query getQuery() { return FilteredQuery.this; }
// return a filtering scorer
- public Scorer scorer (IndexReader indexReader) throws IOException {
- final Scorer scorer = weight.scorer(indexReader);
+ public Scorer scorer(IndexReader indexReader, boolean scoreDocsInOrder, boolean topScorer)
+ throws IOException {
+ final Scorer scorer = weight.scorer(indexReader, scoreDocsInOrder, false);
final DocIdSetIterator docIdSetIterator = filter.getDocIdSet(indexReader).iterator();
return new Scorer(similarity) {
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/HitCollectorWrapper.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/HitCollectorWrapper.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/HitCollectorWrapper.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/HitCollectorWrapper.java Tue Jun 23 18:11:42 2009
@@ -25,7 +25,9 @@
* Wrapper for ({@link HitCollector}) implementations, which
* simply re-bases the incoming docID before calling {@link
* HitCollector#collect}.
- * @deprecated this class will be removed when {@link HitCollector} is removed.
+ * @deprecated this class will be removed when {@link
+ * HitCollector} is removed. Please migrate custom
+ * HitCollectors to the new {@link Collector} class.
*/
public class HitCollectorWrapper extends Collector {
private HitCollector collector;
@@ -47,4 +49,9 @@
public void setScorer(Scorer scorer) throws IOException {
this.scorer = scorer;
}
+
+ public boolean acceptsDocsOutOfOrder() {
+ return false;
+ }
+
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/Hits.java Tue Jun 23 18:11:42 2009
@@ -19,8 +19,8 @@
import java.io.IOException;
import java.util.ConcurrentModificationException;
-import java.util.Vector;
import java.util.Iterator;
+import java.util.Vector;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
@@ -53,7 +53,7 @@
* </pre>
*/
public final class Hits {
- private Weight weight;
+ private QueryWeight weight;
private Searcher searcher;
private Filter filter = null;
private Sort sort = null;
@@ -73,7 +73,7 @@
boolean debugCheckedForDeletions = false; // for test purposes.
Hits(Searcher s, Query q, Filter f) throws IOException {
- weight = q.weight(s);
+ weight = q.queryWeight(s);
searcher = s;
filter = f;
nDeletions = countDeletions(s);
@@ -82,7 +82,7 @@
}
Hits(Searcher s, Query q, Filter f, Sort o) throws IOException {
- weight = q.weight(s);
+ weight = q.queryWeight(s);
searcher = s;
filter = f;
sort = o;
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/IndexSearcher.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/IndexSearcher.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/IndexSearcher.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/IndexSearcher.java Tue Jun 23 18:11:42 2009
@@ -161,38 +161,33 @@
}
// inherit javadoc
- public TopDocs search(Weight weight, Filter filter, final int nDocs)
- throws IOException {
+ public TopDocs search(QueryWeight weight, Filter filter, final int nDocs) throws IOException {
- if (nDocs <= 0) // null might be returned from hq.top() below.
+ if (nDocs <= 0) {
throw new IllegalArgumentException("nDocs must be > 0");
+ }
- // TODO: The following should be changed to first obtain a Scorer and then ask it
- // if it's going to return in-order or out-of-order docs, and create TSDC
- // accordingly.
- TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, false);
+ TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, !weight.scoresDocsOutOfOrder());
search(weight, filter, collector);
return collector.topDocs();
}
- // inherit javadoc
- public TopFieldDocs search(Weight weight, Filter filter, final int nDocs,
- Sort sort)
- throws IOException {
+ public TopFieldDocs search(QueryWeight weight, Filter filter,
+ final int nDocs, Sort sort) throws IOException {
return search(weight, filter, nDocs, sort, true);
}
/**
- * Just like {@link #search(Weight, Filter, int, Sort)}, but you choose
+ * Just like {@link #search(QueryWeight, Filter, int, Sort)}, but you choose
* whether or not the fields in the returned {@link FieldDoc} instances should
* be set by specifying fillFields.<br>
* <b>NOTE:</b> currently, this method tracks document scores and sets them in
* the returned {@link FieldDoc}, however in 3.0 it will move to not track
* document scores. If document scores tracking is still needed, you can use
- * {@link #search(Weight, Filter, Collector)} and pass in a
+ * {@link #search(QueryWeight, Filter, Collector)} and pass in a
* {@link TopFieldCollector} instance.
*/
- public TopFieldDocs search(Weight weight, Filter filter, final int nDocs,
+ public TopFieldDocs search(QueryWeight weight, Filter filter, final int nDocs,
Sort sort, boolean fillFields)
throws IOException {
@@ -222,51 +217,51 @@
TopDocCollector collector = new TopFieldDocCollector(reader, sort, nDocs);
HitCollectorWrapper hcw = new HitCollectorWrapper(collector);
hcw.setNextReader(reader, 0);
- doSearch(reader, weight, filter, hcw);
+ if (filter == null) {
+ Scorer scorer = weight.scorer(reader, true, true);
+ scorer.score(hcw);
+ } else {
+ searchWithFilter(reader, weight, filter, hcw);
+ }
return (TopFieldDocs) collector.topDocs();
}
- // Search each sub-reader
- // TODO: The following should be changed to first obtain a Scorer and then ask it
- // if it's going to return in-order or out-of-order docs, and create TSDC
- // accordingly.
+
TopFieldCollector collector = TopFieldCollector.create(sort, nDocs,
- fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, false);
+ fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.scoresDocsOutOfOrder());
search(weight, filter, collector);
return (TopFieldDocs) collector.topDocs();
}
- // inherit javadoc
- /** @deprecated use {@link #search(Weight, Filter, Collector)} instead. */
- public void search(Weight weight, Filter filter, HitCollector results)
- throws IOException {
- search(weight, filter, new HitCollectorWrapper(results));
- }
-
- // inherit javadoc
- public void search(Weight weight, Filter filter, Collector collector)
+ public void search(QueryWeight weight, Filter filter, Collector collector)
throws IOException {
- for (int i = 0; i < subReaders.length; i++) { // search each subreader
- collector.setNextReader(subReaders[i], docStarts[i]);
- doSearch(subReaders[i], weight, filter, collector);
+ if (filter == null) {
+ for (int i = 0; i < subReaders.length; i++) { // search each subreader
+ collector.setNextReader(subReaders[i], docStarts[i]);
+ Scorer scorer = weight.scorer(subReaders[i], !collector.acceptsDocsOutOfOrder(), true);
+ scorer.score(collector);
+ }
+ } else {
+ for (int i = 0; i < subReaders.length; i++) { // search each subreader
+ collector.setNextReader(subReaders[i], docStarts[i]);
+ searchWithFilter(subReaders[i], weight, filter, collector);
+ }
}
}
-
- private void doSearch(IndexReader reader, Weight weight, Filter filter,
- final Collector collector) throws IOException {
- Scorer scorer = weight.scorer(reader);
- if (scorer == null)
- return;
+ private void searchWithFilter(IndexReader reader, QueryWeight weight,
+ final Filter filter, final Collector collector) throws IOException {
- int docID = scorer.docID();
- assert docID == -1 || docID == DocIdSetIterator.NO_MORE_DOCS;
+ assert filter != null;
- if (filter == null) {
- scorer.score(collector);
+ Scorer scorer = weight.scorer(reader, true, false);
+ if (scorer == null) {
return;
}
+ int docID = scorer.docID();
+ assert docID == -1 || docID == DocIdSetIterator.NO_MORE_DOCS;
+
// CHECKME: use ConjunctionScorer here?
DocIdSetIterator filterIter = filter.getDocIdSet(reader).iterator();
@@ -300,7 +295,7 @@
return query;
}
- public Explanation explain(Weight weight, int doc) throws IOException {
+ public Explanation explain(QueryWeight weight, int doc) throws IOException {
return weight.explain(reader, doc);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java Tue Jun 23 18:11:42 2009
@@ -49,7 +49,7 @@
final byte[] norms;
private int doc = -1;
- MatchAllScorer(IndexReader reader, Similarity similarity, Weight w,
+ MatchAllScorer(IndexReader reader, Similarity similarity, QueryWeight w,
byte[] norms) throws IOException {
super(similarity);
this.termDocs = reader.termDocs(null);
@@ -93,7 +93,7 @@
}
}
- private class MatchAllDocsWeight implements Weight {
+ private class MatchAllDocsWeight extends QueryWeight {
private Similarity similarity;
private float queryWeight;
private float queryNorm;
@@ -124,7 +124,7 @@
queryWeight *= this.queryNorm;
}
- public Scorer scorer(IndexReader reader) throws IOException {
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
return new MatchAllScorer(reader, similarity, this,
normsField != null ? reader.norms(normsField) : null);
}
@@ -142,7 +142,7 @@
}
}
- protected Weight createWeight(Searcher searcher) {
+ public QueryWeight createQueryWeight(Searcher searcher) {
return new MatchAllDocsWeight(searcher);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MultiPhraseQuery.java Tue Jun 23 18:11:42 2009
@@ -123,7 +123,7 @@
}
- private class MultiPhraseWeight implements Weight {
+ private class MultiPhraseWeight extends QueryWeight {
private Similarity similarity;
private float value;
private float idf;
@@ -158,7 +158,7 @@
value = queryWeight * idf; // idf for document
}
- public Scorer scorer(IndexReader reader) throws IOException {
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
if (termArrays.size() == 0) // optimize zero-term case
return null;
@@ -217,7 +217,7 @@
fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc+
"), product of:");
- Explanation tfExpl = scorer(reader).explain(doc);
+ Explanation tfExpl = scorer(reader, true, false).explain(doc);
fieldExpl.addDetail(tfExpl);
fieldExpl.addDetail(idfExpl);
@@ -261,7 +261,7 @@
}
}
- protected Weight createWeight(Searcher searcher) throws IOException {
+ public QueryWeight createQueryWeight(Searcher searcher) throws IOException {
return new MultiPhraseWeight(searcher);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java Tue Jun 23 18:11:42 2009
@@ -35,11 +35,12 @@
* or {@link #search(Query,Filter)} methods.
*/
public class MultiSearcher extends Searcher {
- /**
- * Document Frequency cache acting as a Dummy-Searcher.
- * This class is no full-fledged Searcher, but only supports
- * the methods necessary to initialize Weights.
- */
+
+ /**
+ * Document Frequency cache acting as a Dummy-Searcher. This class is no
+ * full-fledged Searcher, but only supports the methods necessary to
+ * initialize Weights.
+ */
private static class CachedDfSource extends Searcher {
private Map dfMap; // Map from Terms to corresponding doc freqs
private int maxDoc; // document count
@@ -93,34 +94,28 @@
throw new UnsupportedOperationException();
}
- public Explanation explain(Weight weight,int doc) {
+ public Explanation explain(QueryWeight weight,int doc) {
throw new UnsupportedOperationException();
}
- /** @deprecated use {@link #search(Weight, Filter, Collector)} instead. */
- public void search(Weight weight, Filter filter, HitCollector results) {
+ public void search(QueryWeight weight, Filter filter, Collector results) {
throw new UnsupportedOperationException();
}
- public void search(Weight weight, Filter filter, Collector collector) {
- throw new UnsupportedOperationException();
- }
-
- public TopDocs search(Weight weight,Filter filter,int n) {
+ public TopDocs search(QueryWeight weight,Filter filter,int n) {
throw new UnsupportedOperationException();
}
- public TopFieldDocs search(Weight weight,Filter filter,int n,Sort sort) {
+ public TopFieldDocs search(QueryWeight weight,Filter filter,int n,Sort sort) {
throw new UnsupportedOperationException();
}
}
-
private Searchable[] searchables;
private int[] starts;
private int maxDoc = 0;
- /** Creates a searcher which searches <i>searchables</i>. */
+ /** Creates a searcher which searches <i>searchers</i>. */
public MultiSearcher(Searchable[] searchables) throws IOException {
this.searchables = searchables;
@@ -136,7 +131,7 @@
public Searchable[] getSearchables() {
return searchables;
}
-
+
protected int[] getStarts() {
return starts;
}
@@ -200,8 +195,8 @@
return maxDoc;
}
- public TopDocs search(Weight weight, Filter filter, int nDocs)
- throws IOException {
+ public TopDocs search(QueryWeight weight, Filter filter, int nDocs)
+ throws IOException {
HitQueue hq = new HitQueue(nDocs, false);
int totalHits = 0;
@@ -211,10 +206,10 @@
totalHits += docs.totalHits; // update totalHits
ScoreDoc[] scoreDocs = docs.scoreDocs;
for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq
- ScoreDoc scoreDoc = scoreDocs[j];
+ ScoreDoc scoreDoc = scoreDocs[j];
scoreDoc.doc += starts[i]; // convert doc
if(!hq.insert(scoreDoc))
- break; // no more scores > minScore
+ break; // no more scores > minScore
}
}
@@ -227,7 +222,7 @@
return new TopDocs(totalHits, scoreDocs, maxScore);
}
- public TopFieldDocs search (Weight weight, Filter filter, int n, Sort sort)
+ public TopFieldDocs search (QueryWeight weight, Filter filter, int n, Sort sort)
throws IOException {
FieldDocSortedHitQueue hq = null;
int totalHits = 0;
@@ -269,14 +264,7 @@
}
// inherit javadoc
- /** @deprecated use {@link #search(Weight, Filter, Collector)} instead. */
- public void search(Weight weight, Filter filter, final HitCollector results)
- throws IOException {
- search(weight, filter, new HitCollectorWrapper(results));
- }
-
- // inherit javadoc
- public void search(Weight weight, Filter filter, final Collector collector)
+ public void search(QueryWeight weight, Filter filter, final Collector collector)
throws IOException {
for (int i = 0; i < searchables.length; i++) {
@@ -292,6 +280,9 @@
public void setNextReader(IndexReader reader, int docBase) throws IOException {
collector.setNextReader(reader, start + docBase);
}
+ public boolean acceptsDocsOutOfOrder() {
+ return collector.acceptsDocsOutOfOrder();
+ }
};
searchables[i].search(weight, filter, hc);
@@ -306,9 +297,9 @@
return queries[0].combine(queries);
}
- public Explanation explain(Weight weight, int doc) throws IOException {
+ public Explanation explain(QueryWeight weight, int doc) throws IOException {
int i = subSearcher(doc); // find searcher index
- return searchables[i].explain(weight,doc-starts[i]); // dispatch to searcher
+ return searchables[i].explain(weight, doc - starts[i]); // dispatch to searcher
}
/**
@@ -326,7 +317,7 @@
*
* @return rewritten queries
*/
- protected Weight createWeight(Query original) throws IOException {
+ protected QueryWeight createQueryWeight(Query original) throws IOException {
// step 1
Query rewrittenQuery = rewrite(original);
@@ -354,7 +345,7 @@
int numDocs = maxDoc();
CachedDfSource cacheSim = new CachedDfSource(dfMap, numDocs, getSimilarity());
- return rewrittenQuery.weight(cacheSim);
+ return rewrittenQuery.queryWeight(cacheSim);
}
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/ParallelMultiSearcher.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/ParallelMultiSearcher.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/ParallelMultiSearcher.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/ParallelMultiSearcher.java Tue Jun 23 18:11:42 2009
@@ -33,11 +33,11 @@
private Searchable[] searchables;
private int[] starts;
- /** Creates a searcher which searches <i>searchables</i>. */
+ /** Creates a searchable which searches <i>searchables</i>. */
public ParallelMultiSearcher(Searchable[] searchables) throws IOException {
super(searchables);
- this.searchables=searchables;
- this.starts=getStarts();
+ this.searchables = searchables;
+ this.starts = getStarts();
}
/**
@@ -52,24 +52,16 @@
* Searchable, waits for each search to complete and merge
* the results back together.
*/
- public TopDocs search(Weight weight, Filter filter, int nDocs)
+ public TopDocs search(QueryWeight weight, Filter filter, int nDocs)
throws IOException {
HitQueue hq = new HitQueue(nDocs, false);
int totalHits = 0;
MultiSearcherThread[] msta =
new MultiSearcherThread[searchables.length];
- for (int i = 0; i < searchables.length; i++) { // search each searcher
+ for (int i = 0; i < searchables.length; i++) { // search each searchable
// Assume not too many searchables and cost of creating a thread is by far inferior to a search
- msta[i] =
- new MultiSearcherThread(
- searchables[i],
- weight,
- filter,
- nDocs,
- hq,
- i,
- starts,
- "MultiSearcher thread #" + (i + 1));
+ msta[i] = new MultiSearcherThread(searchables[i], weight, filter, nDocs,
+ hq, i, starts, "MultiSearcher thread #" + (i + 1));
msta[i].start();
}
@@ -105,25 +97,16 @@
* Searchable, waits for each search to complete and merges
* the results back together.
*/
- public TopFieldDocs search(Weight weight, Filter filter, int nDocs, Sort sort)
+ public TopFieldDocs search(QueryWeight weight, Filter filter, int nDocs, Sort sort)
throws IOException {
// don't specify the fields - we'll wait to do this until we get results
FieldDocSortedHitQueue hq = new FieldDocSortedHitQueue (null, nDocs);
int totalHits = 0;
MultiSearcherThread[] msta = new MultiSearcherThread[searchables.length];
- for (int i = 0; i < searchables.length; i++) { // search each searcher
+ for (int i = 0; i < searchables.length; i++) { // search each searchable
// Assume not too many searchables and cost of creating a thread is by far inferior to a search
- msta[i] =
- new MultiSearcherThread(
- searchables[i],
- weight,
- filter,
- nDocs,
- hq,
- sort,
- i,
- starts,
- "MultiSearcher thread #" + (i + 1));
+ msta[i] = new MultiSearcherThread(searchables[i], weight, filter, nDocs,
+ hq, sort, i, starts, "MultiSearcher thread #" + (i + 1));
msta[i].start();
}
@@ -156,28 +139,6 @@
}
/** Lower-level search API.
- *
- * <p>{@link HitCollector#collect(int,float)} is called for every matching
- * document.
- *
- * <p>Applications should only use this if they need <i>all</i> of the
- * matching documents. The high-level search API ({@link
- * Searcher#search(Query)}) is usually more efficient, as it skips
- * non-high-scoring hits.
- *
- * @param weight to match documents
- * @param filter if non-null, a bitset used to eliminate some documents
- * @param results to receive hits
- *
- * @todo parallelize this one too
- * @deprecated use {@link #search(Weight, Filter, Collector)} instead.
- */
- public void search(Weight weight, Filter filter, final HitCollector results)
- throws IOException {
- search(weight, filter, new HitCollectorWrapper(results));
- }
-
- /** Lower-level search API.
*
* <p>{@link Collector#collect(int)} is called for every matching document.
*
@@ -192,7 +153,7 @@
*
* @todo parallelize this one too
*/
- public void search(Weight weight, Filter filter, final Collector collector)
+ public void search(QueryWeight weight, Filter filter, final Collector collector)
throws IOException {
for (int i = 0; i < searchables.length; i++) {
@@ -205,10 +166,12 @@
public void collect(int doc) throws IOException {
collector.collect(doc);
}
-
public void setNextReader(IndexReader reader, int docBase) throws IOException {
collector.setNextReader(reader, start + docBase);
}
+ public boolean acceptsDocsOutOfOrder() {
+ return collector.acceptsDocsOutOfOrder();
+ }
};
searchables[i].search(weight, filter, hc);
@@ -231,7 +194,7 @@
class MultiSearcherThread extends Thread {
private Searchable searchable;
- private Weight weight;
+ private QueryWeight weight;
private Filter filter;
private int nDocs;
private TopDocs docs;
@@ -241,15 +204,8 @@
private IOException ioe;
private Sort sort;
- public MultiSearcherThread(
- Searchable searchable,
- Weight weight,
- Filter filter,
- int nDocs,
- HitQueue hq,
- int i,
- int[] starts,
- String name) {
+ public MultiSearcherThread(Searchable searchable, QueryWeight weight, Filter filter,
+ int nDocs, HitQueue hq, int i, int[] starts, String name) {
super(name);
this.searchable = searchable;
this.weight = weight;
@@ -260,16 +216,9 @@
this.starts = starts;
}
- public MultiSearcherThread(
- Searchable searchable,
- Weight weight,
- Filter filter,
- int nDocs,
- FieldDocSortedHitQueue hq,
- Sort sort,
- int i,
- int[] starts,
- String name) {
+ public MultiSearcherThread(Searchable searchable, QueryWeight weight,
+ Filter filter, int nDocs, FieldDocSortedHitQueue hq, Sort sort, int i,
+ int[] starts, String name) {
super(name);
this.searchable = searchable;
this.weight = weight;
@@ -298,7 +247,7 @@
TopFieldDocs docsFields = (TopFieldDocs) docs;
// If one of the Sort fields is FIELD_DOC, need to fix its values, so that
// it will break ties by doc Id properly. Otherwise, it will compare to
- // 'relative' doc Ids, that belong to two different searchers.
+ // 'relative' doc Ids, that belong to two different searchables.
for (int j = 0; j < docsFields.fields.length; j++) {
if (docsFields.fields[j].getType() == SortField.DOC) {
// iterate over the score docs and change their fields value
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java Tue Jun 23 18:11:42 2009
@@ -106,7 +106,7 @@
return result;
}
- private class PhraseWeight implements Weight {
+ private class PhraseWeight extends QueryWeight {
private Similarity similarity;
private float value;
private float idf;
@@ -136,7 +136,7 @@
value = queryWeight * idf; // idf for document
}
- public Scorer scorer(IndexReader reader) throws IOException {
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
if (terms.size() == 0) // optimize zero-term case
return null;
@@ -209,7 +209,7 @@
fieldExpl.setDescription("fieldWeight("+field+":"+query+" in "+doc+
"), product of:");
- Explanation tfExpl = scorer(reader).explain(doc);
+ Explanation tfExpl = scorer(reader, true, false).explain(doc);
fieldExpl.addDetail(tfExpl);
fieldExpl.addDetail(idfExpl);
@@ -237,12 +237,12 @@
}
}
- protected Weight createWeight(Searcher searcher) throws IOException {
+ public QueryWeight createQueryWeight(Searcher searcher) throws IOException {
if (terms.size() == 1) { // optimize one-term case
Term term = (Term)terms.get(0);
Query termQuery = new TermQuery(term);
termQuery.setBoost(getBoost());
- return termQuery.createWeight(searcher);
+ return termQuery.createQueryWeight(searcher);
}
return new PhraseWeight(searcher);
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/PhraseScorer.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/PhraseScorer.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/PhraseScorer.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/PhraseScorer.java Tue Jun 23 18:11:42 2009
@@ -19,7 +19,7 @@
import java.io.IOException;
-import org.apache.lucene.index.*;
+import org.apache.lucene.index.TermPositions;
/** Expert: Scoring functionality for phrase queries.
* <br>A document is considered matching if it contains the phrase-query terms
@@ -32,7 +32,7 @@
* means a match.
*/
abstract class PhraseScorer extends Scorer {
- private Weight weight;
+ private QueryWeight weight;
protected byte[] norms;
protected float value;
@@ -43,7 +43,7 @@
private float freq; //prhase frequency in current doc as computed by phraseFreq().
- PhraseScorer(Weight weight, TermPositions[] tps, int[] offsets,
+ PhraseScorer(QueryWeight weight, TermPositions[] tps, int[] offsets,
Similarity similarity, byte[] norms) {
super(similarity);
this.norms = norms;
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/PositiveScoresOnlyCollector.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/PositiveScoresOnlyCollector.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/PositiveScoresOnlyCollector.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/PositiveScoresOnlyCollector.java Tue Jun 23 18:11:42 2009
@@ -26,7 +26,6 @@
* {@link Collector} and makes sure only documents with
* scores > 0 are collected.
*/
-
public class PositiveScoresOnlyCollector extends Collector {
final private Collector c;
@@ -53,4 +52,8 @@
c.setScorer(this.scorer);
}
+ public boolean acceptsDocsOutOfOrder() {
+ return c.acceptsDocsOutOfOrder();
+ }
+
}
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/Query.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/Query.java?rev=787772&r1=787771&r2=787772&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/Query.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/Query.java Tue Jun 23 18:11:42 2009
@@ -80,24 +80,50 @@
return toString("");
}
- /** Expert: Constructs an appropriate Weight implementation for this query.
- *
- * <p>Only implemented by primitive queries, which re-write to themselves.
+ /**
+ * Expert: Constructs an appropriate Weight implementation for this query.
+ *
+ * <p>
+ * Only implemented by primitive queries, which re-write to themselves.
+ * @deprecated use {@link #createQueryWeight(Searcher)} instead.
*/
protected Weight createWeight(Searcher searcher) throws IOException {
+ return createQueryWeight(searcher);
+ }
+
+ /**
+ * Expert: Constructs an appropriate {@link QueryWeight} implementation for
+ * this query.
+ *
+ * <p>
+ * Only implemented by primitive queries, which re-write to themselves.
+ */
+ public QueryWeight createQueryWeight(Searcher searcher) throws IOException {
throw new UnsupportedOperationException();
}
- /** Expert: Constructs and initializes a Weight for a top-level query. */
- public Weight weight(Searcher searcher)
- throws IOException {
+ /**
+ * Expert: Constructs and initializes a Weight for a top-level query.
+ *
+ * @deprecated use {@link #queryWeight(Searcher)} instead.
+ */
+ public Weight weight(Searcher searcher) throws IOException {
+ return queryWeight(searcher);
+ }
+
+ /**
+ * Expert: Constructs and initializes a {@link QueryWeight} for a top-level
+ * query.
+ */
+ public QueryWeight queryWeight(Searcher searcher) throws IOException {
Query query = searcher.rewrite(this);
- Weight weight = query.createWeight(searcher);
+ QueryWeight weight = query.createQueryWeight(searcher);
float sum = weight.sumOfSquaredWeights();
float norm = getSimilarity(searcher).queryNorm(sum);
weight.normalize(norm);
return weight;
}
+
/** Expert: called to re-write queries into primitive queries. For example,
* a PrefixQuery will be rewritten into a BooleanQuery that consists
@@ -106,6 +132,7 @@
public Query rewrite(IndexReader reader) throws IOException {
return this;
}
+
/** Expert: called when re-writing queries under MultiSearcher.
*
@@ -151,6 +178,7 @@
result.add((Query) it.next(), BooleanClause.Occur.SHOULD);
return result;
}
+
/**
* Expert: adds all terms occuring in this query to the terms set. Only
@@ -162,6 +190,7 @@
// needs to be implemented by query subclasses
throw new UnsupportedOperationException();
}
+
/** Expert: merges the clauses of a set of BooleanQuery's into a single
@@ -187,6 +216,7 @@
}
return result;
}
+
/** Expert: Returns the Similarity implementation to be used for this query.
* Subclasses may override this method to specify their own Similarity
@@ -199,7 +229,7 @@
/** Returns a clone of this query. */
public Object clone() {
try {
- return (Query)super.clone();
+ return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone not supported: " + e.getMessage());
}
Added: lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java?rev=787772&view=auto
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java (added)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java Tue Jun 23 18:11:42 2009
@@ -0,0 +1,119 @@
+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 java.io.IOException;
+import java.io.Serializable;
+
+import org.apache.lucene.index.IndexReader;
+
+/**
+ * Expert: Calculate query weights and build query scorers.
+ * <p>
+ * The purpose of {@link QueryWeight} is to ensure searching does not
+ * modify a {@link Query}, so that a {@link Query} instance can be reused. <br>
+ * {@link Searcher} dependent state of the query should reside in the
+ * {@link QueryWeight}. <br>
+ * {@link IndexReader} dependent state should reside in the {@link Scorer}.
+ * <p>
+ * A <code>QueryWeight</code> is used in the following way:
+ * <ol>
+ * <li>A <code>QueryWeight</code> is constructed by a top-level query, given a
+ * <code>Searcher</code> ({@link Query#createWeight(Searcher)}).
+ * <li>The {@link #sumOfSquaredWeights()} method is called on the
+ * <code>QueryWeight</code> to compute the query normalization factor
+ * {@link Similarity#queryNorm(float)} of the query clauses contained in the
+ * query.
+ * <li>The query normalization factor is passed to {@link #normalize(float)}. At
+ * this point the weighting is complete.
+ * <li>A <code>Scorer</code> is constructed by {@link #scorer(IndexReader)}.
+ * </ol>
+ *
+ * @since 2.9
+ */
+public abstract class QueryWeight implements Weight, Serializable {
+
+ /** An explanation of the score computation for the named document. */
+ public abstract Explanation explain(IndexReader reader, int doc) throws IOException;
+
+ /** The query that this concerns. */
+ public abstract Query getQuery();
+
+ /** The weight for this query. */
+ public abstract float getValue();
+
+ /** Assigns the query normalization factor to this. */
+ public abstract void normalize(float norm);
+
+ /**
+ * @deprecated use {@link #scorer(IndexReader, boolean, boolean)} instead.
+ * Currently this defaults to asking a scorer in out-of-order
+ * mode, but will be removed in 3.0.
+ */
+ public Scorer scorer(IndexReader reader) throws IOException {
+ return scorer(reader, true, false);
+ }
+
+ /**
+ * Returns a {@link Scorer} which scores documents in/out-of order according
+ * to <code>scoreDocsInOrder</code>.
+ * <p>
+ * <b>NOTE:</b> even if <code>scoreDocsInOrder</code> is false, it is
+ * recommended to check whether the returned <code>Scorer</code> indeed scores
+ * documents out of order (i.e., call {@link #scoresDocsOutOfOrder()}), as some
+ * <code>Scorer</code> implementations will always return documents in-order.
+ *
+ * @param reader
+ * the {@link IndexReader} for which to return the {@link Scorer}.
+ * @param scoreDocsInOrder
+ * specifies whether in-order scoring of documents is required. Note
+ * that if set to false (i.e., out-of-order scoring is required),
+ * this method can return whatever scoring mode it supports, as every
+ * in-order scorer is also an out-of-order one. However, an
+ * out-of-order scorer may not support {@link Scorer#nextDoc()}
+ * and/or {@link Scorer#advance(int)}, therfore it is recommended to
+ * request an in-order scorer if use of these methods is required.
+ * @param topScorer
+ * specifies whether the returned {@link Scorer} will be used as a
+ * top scorer or as in iterator. I.e., if true,
+ * {@link Scorer#score(Collector)} will be called; if false,
+ * {@link Scorer#nextDoc()} and/or {@link Scorer#advance(int)} will
+ * be called.
+ * @return a {@link Scorer} which scores documents in/out-of order.
+ * @throws IOException
+ */
+ public abstract Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
+ boolean topScorer) throws IOException;
+
+ /** The sum of squared weights of contained query clauses. */
+ public abstract float sumOfSquaredWeights() throws IOException;
+
+ /**
+ * Returns true iff this implementation scores docs only out of order. This
+ * method is used in conjunction with {@link Collector}'s
+ * {@link Collector#acceptsDocsOutOfOrder() acceptsDocsOutOfOrder} and
+ * {@link #scorer(org.apache.lucene.index.IndexReader, boolean, boolean)} to
+ * create a matching {@link Scorer} instance for a given {@link Collector}, or
+ * vice versa.
+ * <p>
+ * <b>NOTE:</b> the default implementation returns <code>false</code>, i.e.
+ * the <code>Scorer</code> scores documents in-order.
+ */
+ public boolean scoresDocsOutOfOrder() { return false; }
+
+}
Propchange: lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeight.java
------------------------------------------------------------------------------
svn:eol-styte = native
Added: lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java?rev=787772&view=auto
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java (added)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java Tue Jun 23 18:11:42 2009
@@ -0,0 +1,68 @@
+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 java.io.IOException;
+
+import org.apache.lucene.index.IndexReader;
+
+/**
+ * A wrapper class for the deprecated {@link Weight}.
+ * Please re-implement any custom Weight classes as {@link
+ * QueryWeight} instead.
+ *
+ * @deprecated will be removed in 3.0
+ */
+public class QueryWeightWrapper extends QueryWeight {
+
+ private Weight weight;
+
+ public QueryWeightWrapper(Weight weight) {
+ this.weight = weight;
+ }
+
+ public Explanation explain(IndexReader reader, int doc) throws IOException {
+ return weight.explain(reader, doc);
+ }
+
+ public Query getQuery() {
+ return weight.getQuery();
+ }
+
+ public float getValue() {
+ return weight.getValue();
+ }
+
+ public void normalize(float norm) {
+ weight.normalize(norm);
+ }
+
+ public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer)
+ throws IOException {
+ return weight.scorer(reader);
+ }
+
+ public float sumOfSquaredWeights() throws IOException {
+ return weight.sumOfSquaredWeights();
+ }
+
+ public Scorer scorer(IndexReader reader) throws IOException {
+ return weight.scorer(reader);
+ }
+
+}
Propchange: lucene/java/trunk/src/java/org/apache/lucene/search/QueryWeightWrapper.java
------------------------------------------------------------------------------
svn:eol-styte = native