Index: src/java/org/apache/lucene/search/BooleanScorer.java =================================================================== --- src/java/org/apache/lucene/search/BooleanScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/BooleanScorer.java (working copy) @@ -117,8 +117,6 @@ public int docID() { return doc; } - public Explanation explain(int doc) throws IOException { return null; } - public int nextDoc() throws IOException { return NO_MORE_DOCS; } public float score() throws IOException { return score; } @@ -276,10 +274,6 @@ return doc; } - public Explanation explain(int doc) { - throw new UnsupportedOperationException(); - } - public int nextDoc() throws IOException { boolean more; do { Index: src/java/org/apache/lucene/search/BooleanScorer2.java =================================================================== --- src/java/org/apache/lucene/search/BooleanScorer2.java (revision 830100) +++ src/java/org/apache/lucene/search/BooleanScorer2.java (working copy) @@ -133,9 +133,6 @@ public int advance(int target) throws IOException { return scorer.advance(target); } - public Explanation explain(int docNr) throws IOException { - return scorer.explain(docNr); - } } private Scorer countingDisjunctionSumScorer(final List scorers, @@ -262,7 +259,6 @@ /** Scores and collects all matching documents. * @param collector The collector to which all matching documents are passed through. - *
When this method is used the {@link #explain(int)} method should not be used. */ public void score(Collector collector) throws IOException { collector.setScorer(this); @@ -298,19 +294,6 @@ public int advance(int target) throws IOException { return doc = countingSumScorer.advance(target); } - - /** Throws an UnsupportedOperationException. - * TODO: Implement an explanation of the coordination factor. - * @param doc The document number for the explanation. - * @throws UnsupportedOperationException - */ - public Explanation explain(int doc) { - throw new UnsupportedOperationException(); - /* How to explain the coordination factor? - initCountingSumScorer(); - return countingSumScorer.explain(doc); // misses coord factor. - */ - } } Index: src/java/org/apache/lucene/search/ConjunctionScorer.java =================================================================== --- src/java/org/apache/lucene/search/ConjunctionScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/ConjunctionScorer.java (working copy) @@ -113,10 +113,6 @@ return lastDoc; } - public Explanation explain(int doc) { - throw new UnsupportedOperationException(); - } - public int nextDoc() throws IOException { if (lastDoc == NO_MORE_DOCS) { return lastDoc; Index: src/java/org/apache/lucene/search/ConstantScoreQuery.java =================================================================== --- src/java/org/apache/lucene/search/ConstantScoreQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/ConstantScoreQuery.java (working copy) @@ -139,10 +139,6 @@ public int advance(int target) throws IOException { return docIdSetIterator.advance(target); } - - public Explanation explain(int doc) throws IOException { - throw new UnsupportedOperationException(); - } } public Weight createWeight(Searcher searcher) { Index: src/java/org/apache/lucene/search/DisjunctionMaxScorer.java =================================================================== --- src/java/org/apache/lucene/search/DisjunctionMaxScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/DisjunctionMaxScorer.java (working copy) @@ -120,14 +120,6 @@ return doc = subScorers[0].docID(); } - /** Explain a score that we computed. UNSUPPORTED -- see explanation capability in DisjunctionMaxQuery. - * @param doc the number of a document we scored - * @return the Explanation for our score - */ - public Explanation explain(int doc) throws IOException { - throw new UnsupportedOperationException(); - } - // Organize subScorers into a min heap with scorers generating the earliest document on top. private void heapify() { for (int i = (numScorers >> 1) - 1; i >= 0; i--) { Index: src/java/org/apache/lucene/search/DisjunctionSumScorer.java =================================================================== --- src/java/org/apache/lucene/search/DisjunctionSumScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/DisjunctionSumScorer.java (working copy) @@ -106,7 +106,6 @@ /** Scores and collects all matching documents. * @param collector The collector to which all matching documents are passed through. - *
When this method is used the {@link #explain(int)} method should not be used. */ @Override public void score(Collector collector) throws IOException { @@ -209,8 +208,6 @@ /** * Advances to the first match beyond the current whose document number is * greater than or equal to a given target.
- * When this method is used the {@link #explain(int)} method should not be - * used.
* The implementation uses the skipTo() method on the subscorers. * * @param target @@ -236,31 +233,4 @@ } } while (true); } - - /** @return An explanation for the score of a given document. */ - @Override - public Explanation explain(int doc) throws IOException { - Explanation res = new Explanation(); - float sumScore = 0.0f; - int nrMatches = 0; - for (Scorer se : subScorers) { - Explanation es = se.explain(doc); - if (es.getValue() > 0.0f) { // indicates match - sumScore += es.getValue(); - nrMatches++; - } - res.addDetail(es); - } - if (nrMatchers >= minimumNrMatchers) { - res.setValue(sumScore); - res.setDescription("sum over at least " + minimumNrMatchers - + " of " + subScorers.size() + ":"); - } else { - res.setValue(0.0f); - res.setDescription(nrMatches + " match(es) but at least " - + minimumNrMatchers + " of " - + subScorers.size() + " needed"); - } - return res; - } } Index: src/java/org/apache/lucene/search/FilteredQuery.java =================================================================== --- src/java/org/apache/lucene/search/FilteredQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/FilteredQuery.java (working copy) @@ -148,20 +148,6 @@ } public float score() throws IOException { return getBoost() * scorer.score(); } - - // add an explanation about whether the document was filtered - public Explanation explain (int i) throws IOException { - Explanation exp = scorer.explain(i); - - if (docIdSetIterator.advance(i) == i) { - exp.setDescription ("allowed by filter: "+exp.getDescription()); - exp.setValue(getBoost() * exp.getValue()); - } else { - exp.setDescription ("removed by filter: "+exp.getDescription()); - exp.setValue(0.0f); - } - return exp; - } }; } }; Index: src/java/org/apache/lucene/search/function/CustomScoreQuery.java =================================================================== --- src/java/org/apache/lucene/search/function/CustomScoreQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/function/CustomScoreQuery.java (working copy) @@ -348,18 +348,14 @@ } private Explanation doExplain(IndexReader reader, int doc) throws IOException { - Scorer[] valSrcScorers = new Scorer[valSrcWeights.length]; - for(int i = 0; i < valSrcScorers.length; i++) { - valSrcScorers[i] = valSrcWeights[i].scorer(reader, true, false); - } Explanation subQueryExpl = subQueryWeight.explain(reader, doc); if (!subQueryExpl.isMatch()) { return subQueryExpl; } // match - Explanation[] valSrcExpls = new Explanation[valSrcScorers.length]; - for(int i = 0; i < valSrcScorers.length; i++) { - valSrcExpls[i] = valSrcScorers[i].explain(doc); + Explanation[] valSrcExpls = new Explanation[valSrcWeights.length]; + for(int i = 0; i < valSrcWeights.length; i++) { + valSrcExpls[i] = valSrcWeights[i].explain(reader, doc); } Explanation customExp = customExplain(doc,subQueryExpl,valSrcExpls); float sc = getValue() * customExp.getValue(); @@ -433,27 +429,6 @@ } return doc; } - - // TODO: remove in 3.0 - /*(non-Javadoc) @see org.apache.lucene.search.Scorer#explain(int) */ - public Explanation explain(int doc) throws IOException { - Explanation subQueryExpl = weight.subQueryWeight.explain(reader,doc); - if (!subQueryExpl.isMatch()) { - return subQueryExpl; - } - // match - Explanation[] valSrcExpls = new Explanation[valSrcScorers.length]; - for(int i = 0; i < valSrcScorers.length; i++) { - valSrcExpls[i] = valSrcScorers[i].explain(doc); - } - Explanation customExp = customExplain(doc,subQueryExpl,valSrcExpls); - float sc = qWeight * customExp.getValue(); - Explanation res = new ComplexExplanation( - true, sc, CustomScoreQuery.this.toString() + ", product of:"); - res.addDetail(customExp); - res.addDetail(new Explanation(qWeight, "queryBoost")); // actually using the q boost as q weight (== weight value) - return res; - } } public Weight createWeight(Searcher searcher) throws IOException { Index: src/java/org/apache/lucene/search/function/ValueSourceQuery.java =================================================================== --- src/java/org/apache/lucene/search/function/ValueSourceQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/function/ValueSourceQuery.java (working copy) @@ -100,7 +100,16 @@ /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */ public Explanation explain(IndexReader reader, int doc) throws IOException { - return new ValueSourceScorer(similarity, reader, this).explain(doc); + DocValues vals = valSrc.getValues(reader); + float sc = queryWeight * vals.floatVal(doc); + + Explanation result = new ComplexExplanation( + true, sc, ValueSourceQuery.this.toString() + ", product of:"); + + result.addDetail(vals.explain(doc)); + result.addDetail(new Explanation(getBoost(), "boost")); + result.addDetail(new Explanation(queryNorm,"queryNorm")); + return result; } } @@ -143,19 +152,6 @@ public float score() throws IOException { return qWeight * vals.floatVal(termDocs.doc()); } - - /*(non-Javadoc) @see org.apache.lucene.search.Scorer#explain(int) */ - public Explanation explain(int doc) throws IOException { - float sc = qWeight * vals.floatVal(doc); - - Explanation result = new ComplexExplanation( - true, sc, ValueSourceQuery.this.toString() + ", product of:"); - - result.addDetail(vals.explain(doc)); - result.addDetail(new Explanation(getBoost(), "boost")); - result.addDetail(new Explanation(weight.queryNorm,"queryNorm")); - return result; - } } public Weight createWeight(Searcher searcher) { Index: src/java/org/apache/lucene/search/MatchAllDocsQuery.java =================================================================== --- src/java/org/apache/lucene/search/MatchAllDocsQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/MatchAllDocsQuery.java (working copy) @@ -58,10 +58,6 @@ this.norms = norms; } - public Explanation explain(int doc) { - return null; // not called... see MatchAllDocsWeight.explain() - } - public int docID() { return doc; } Index: src/java/org/apache/lucene/search/MultiPhraseQuery.java =================================================================== --- src/java/org/apache/lucene/search/MultiPhraseQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/MultiPhraseQuery.java (working copy) @@ -215,12 +215,16 @@ fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc+ "), product of:"); - Scorer scorer = scorer(reader, true, false); + PhraseScorer scorer = (PhraseScorer) scorer(reader, true, false); if (scorer == null) { return new Explanation(0.0f, "no matching docs"); } - Explanation tfExpl = scorer.explain(doc); - fieldExpl.addDetail(tfExpl); + Explanation tfExplanation = new Explanation(); + int d = scorer.advance(doc); + float phraseFreq = (d == doc) ? scorer.currentFreq() : 0.0f; + tfExplanation.setValue(similarity.tf(phraseFreq)); + tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")"); + fieldExpl.addDetail(tfExplanation); fieldExpl.addDetail(idfExpl); Explanation fieldNormExpl = new Explanation(); @@ -231,8 +235,8 @@ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")"); fieldExpl.addDetail(fieldNormExpl); - fieldExpl.setMatch(Boolean.valueOf(tfExpl.isMatch())); - fieldExpl.setValue(tfExpl.getValue() * + fieldExpl.setMatch(Boolean.valueOf(tfExplanation.isMatch())); + fieldExpl.setValue(tfExplanation.getValue() * idfExpl.getValue() * fieldNormExpl.getValue()); Index: src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java =================================================================== --- src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java (working copy) @@ -223,7 +223,7 @@ } @Override - public Explanation explain(int doc) throws IOException { + protected Explanation explain(int doc) throws IOException { Explanation result = new Explanation(); Explanation nonPayloadExpl = super.explain(doc); result.addDetail(nonPayloadExpl); Index: src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java =================================================================== --- src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java (working copy) @@ -130,6 +130,7 @@ * @return {@link #getSpanScore()} * {@link #getPayloadScore()} * @throws IOException */ + @Override public float score() throws IOException { return includeSpanScore ? getSpanScore() * getPayloadScore() @@ -160,7 +161,8 @@ return function.docScore(doc, term.field(), payloadsSeen, payloadScore); } - public Explanation explain(final int doc) throws IOException { + @Override + protected Explanation explain(final int doc) throws IOException { ComplexExplanation result = new ComplexExplanation(); Explanation nonPayloadExpl = super.explain(doc); result.addDetail(nonPayloadExpl); Index: src/java/org/apache/lucene/search/PhraseQuery.java =================================================================== --- src/java/org/apache/lucene/search/PhraseQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/PhraseQuery.java (working copy) @@ -123,22 +123,29 @@ idf = idfExp.getIdf(); } + @Override public String toString() { return "weight(" + PhraseQuery.this + ")"; } + @Override public Query getQuery() { return PhraseQuery.this; } + + @Override public float getValue() { return value; } + @Override public float sumOfSquaredWeights() { queryWeight = idf * getBoost(); // compute query weight return queryWeight * queryWeight; // square it } + @Override public void normalize(float queryNorm) { this.queryNorm = queryNorm; queryWeight *= queryNorm; // normalize query weight value = queryWeight * idf; // idf for document } + @Override public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException { if (terms.size() == 0) // optimize zero-term case return null; @@ -161,6 +168,7 @@ } + @Override public Explanation explain(IndexReader reader, int doc) throws IOException { @@ -208,12 +216,17 @@ fieldExpl.setDescription("fieldWeight("+field+":"+query+" in "+doc+ "), product of:"); - Scorer scorer = scorer(reader, true, false); + PhraseScorer scorer = (PhraseScorer) scorer(reader, true, false); if (scorer == null) { return new Explanation(0.0f, "no matching docs"); } - Explanation tfExpl = scorer.explain(doc); - fieldExpl.addDetail(tfExpl); + Explanation tfExplanation = new Explanation(); + int d = scorer.advance(doc); + float phraseFreq = (d == doc) ? scorer.currentFreq() : 0.0f; + tfExplanation.setValue(similarity.tf(phraseFreq)); + tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")"); + + fieldExpl.addDetail(tfExplanation); fieldExpl.addDetail(idfExpl); Explanation fieldNormExpl = new Explanation(); @@ -224,7 +237,7 @@ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")"); fieldExpl.addDetail(fieldNormExpl); - fieldExpl.setValue(tfExpl.getValue() * + fieldExpl.setValue(tfExplanation.getValue() * idfExpl.getValue() * fieldNormExpl.getValue()); @@ -240,6 +253,7 @@ } } + @Override public Weight createWeight(Searcher searcher) throws IOException { if (terms.size() == 1) { // optimize one-term case Term term = terms.get(0); @@ -253,11 +267,13 @@ /** * @see org.apache.lucene.search.Query#extractTerms(Set) */ + @Override public void extractTerms(Set queryTerms) { queryTerms.addAll(terms); } /** Prints a user-readable version of this query. */ + @Override public String toString(String f) { StringBuilder buffer = new StringBuilder(); if (field != null && !field.equals(f)) { @@ -301,6 +317,7 @@ } /** Returns true iff o is equal to this. */ + @Override public boolean equals(Object o) { if (!(o instanceof PhraseQuery)) return false; @@ -312,6 +329,7 @@ } /** Returns a hash code value for this object.*/ + @Override public int hashCode() { return Float.floatToIntBits(getBoost()) ^ slop Index: src/java/org/apache/lucene/search/PhraseScorer.java =================================================================== --- src/java/org/apache/lucene/search/PhraseScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/PhraseScorer.java (working copy) @@ -69,8 +69,10 @@ first.doc = -1; } + @Override public int docID() { return first.doc; } + @Override public int nextDoc() throws IOException { if (firstTime) { init(); @@ -104,12 +106,14 @@ return false; // no more matches } + @Override public float score() throws IOException { //System.out.println("scoring " + first.doc); float raw = getSimilarity().tf(freq) * value; // raw score return norms == null ? raw : raw * Similarity.decodeNorm(norms[first.doc]); // normalize } + @Override public int advance(int target) throws IOException { firstTime = false; for (PhrasePositions pp = first; more && pp != null; pp = pp.next) { @@ -125,6 +129,11 @@ } /** + * phrase frequency in current doc as computed by phraseFreq(). + */ + public float currentFreq() { return freq; } + + /** * For a document containing all the phrase query terms, compute the * frequency of the phrase in that document. * A non zero frequency means a match. @@ -170,17 +179,6 @@ last.next = null; } - public Explanation explain(final int doc) throws IOException { - Explanation tfExplanation = new Explanation(); - - int d = advance(doc); - float phraseFreq = (d == doc) ? freq : 0.0f; - tfExplanation.setValue(getSimilarity().tf(phraseFreq)); - tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")"); - - return tfExplanation; - } - public String toString() { return "scorer(" + weight + ")"; } } Index: src/java/org/apache/lucene/search/ReqExclScorer.java =================================================================== --- src/java/org/apache/lucene/search/ReqExclScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/ReqExclScorer.java (working copy) @@ -113,15 +113,4 @@ } return doc = toNonExcluded(); } - - public Explanation explain(int doc) throws IOException { - Explanation res = new Explanation(); - if (exclDisi.advance(doc) == doc) { - res.setDescription("excluded"); - } else { - res.setDescription("not excluded"); - res.addDetail(reqScorer.explain(doc)); - } - return res; - } } Index: src/java/org/apache/lucene/search/ReqOptSumScorer.java =================================================================== --- src/java/org/apache/lucene/search/ReqOptSumScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/ReqOptSumScorer.java (working copy) @@ -76,16 +76,5 @@ return optScorerDoc == curDoc ? reqScore + optScorer.score() : reqScore; } - /** Explain the score of a document. - * TODO: Also show the total score. - * See BooleanScorer.explain() on how to do this. - */ - public Explanation explain(int doc) throws IOException { - Explanation res = new Explanation(); - res.setDescription("required, optional"); - res.addDetail(reqScorer.explain(doc)); - res.addDetail(optScorer.explain(doc)); - return res; - } } Index: src/java/org/apache/lucene/search/ScoreCachingWrappingScorer.java =================================================================== --- src/java/org/apache/lucene/search/ScoreCachingWrappingScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/ScoreCachingWrappingScorer.java (working copy) @@ -50,10 +50,6 @@ return scorer.getSimilarity(); } - public Explanation explain(int doc) throws IOException { - return scorer.explain(doc); - } - public float score() throws IOException { int doc = scorer.docID(); if (doc != curDoc) { Index: src/java/org/apache/lucene/search/Scorer.java =================================================================== --- src/java/org/apache/lucene/search/Scorer.java (revision 830100) +++ src/java/org/apache/lucene/search/Scorer.java (working copy) @@ -56,7 +56,6 @@ /** Scores and collects all matching documents. * @param collector The collector to which all matching documents are passed. - *
When this method is used the {@link #explain(int)} method should not be used. */ public void score(Collector collector) throws IOException { collector.setScorer(this); @@ -97,16 +96,4 @@ */ public abstract float score() throws IOException; - /** Returns an explanation of the score for a document. - *
When this method is used, the {@link #next()}, {@link #skipTo(int)} and - * {@link #score(HitCollector)} methods should not be used. - * @param doc The document number for the explanation. - * - * @deprecated Please use {@link IndexSearcher#explain} - * or {@link Weight#explain} instead. - */ - public Explanation explain(int doc) throws IOException { - throw new UnsupportedOperationException(); - } - } Index: src/java/org/apache/lucene/search/spans/SpanScorer.java =================================================================== --- src/java/org/apache/lucene/search/spans/SpanScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/spans/SpanScorer.java (working copy) @@ -53,6 +53,7 @@ } } + @Override public int nextDoc() throws IOException { if (!setFreqCurrentDoc()) { doc = NO_MORE_DOCS; @@ -60,6 +61,7 @@ return doc; } + @Override public int advance(int target) throws IOException { if (!more) { return doc = NO_MORE_DOCS; @@ -87,14 +89,18 @@ return true; } + @Override public int docID() { return doc; } + @Override public float score() throws IOException { float raw = getSimilarity().tf(freq) * value; // raw score return norms == null? raw : raw * Similarity.decodeNorm(norms[doc]); // normalize } - public Explanation explain(final int doc) throws IOException { + /** This method is no longer an official member of {@link Scorer}, + * but it is needed by SpanWeight to build an explanation. */ + protected Explanation explain(final int doc) throws IOException { Explanation tfExplanation = new Explanation(); int expDoc = advance(doc); Index: src/java/org/apache/lucene/search/spans/SpanWeight.java =================================================================== --- src/java/org/apache/lucene/search/spans/SpanWeight.java (revision 830100) +++ src/java/org/apache/lucene/search/spans/SpanWeight.java (working copy) @@ -104,7 +104,7 @@ fieldExpl.setDescription("fieldWeight("+field+":"+query.toString(field)+ " in "+doc+"), product of:"); - Explanation tfExpl = scorer(reader, true, false).explain(doc); + Explanation tfExpl = ((SpanScorer)scorer(reader, true, false)).explain(doc); fieldExpl.addDetail(tfExpl); fieldExpl.addDetail(idfExpl); Index: src/java/org/apache/lucene/search/TermQuery.java =================================================================== --- src/java/org/apache/lucene/search/TermQuery.java (revision 830100) +++ src/java/org/apache/lucene/search/TermQuery.java (working copy) @@ -104,8 +104,24 @@ fieldExpl.setDescription("fieldWeight("+term+" in "+doc+ "), product of:"); - Explanation tfExpl = scorer(reader, true, false).explain(doc); - fieldExpl.addDetail(tfExpl); + Explanation tfExplanation = new Explanation(); + int tf = 0; + TermDocs termDocs = reader.termDocs(term); + if (termDocs != null) { + try { + if (termDocs.skipTo(doc) && termDocs.doc() == doc) { + tf = termDocs.freq(); + } + } finally { + termDocs.close(); + } + tfExplanation.setValue(similarity.tf(tf)); + tfExplanation.setDescription("tf(termFreq("+term+")="+tf+")"); + } else { + tfExplanation.setValue(0.0f); + tfExplanation.setDescription("no matching term"); + } + fieldExpl.addDetail(tfExplanation); fieldExpl.addDetail(expl); Explanation fieldNormExpl = new Explanation(); @@ -116,8 +132,8 @@ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")"); fieldExpl.addDetail(fieldNormExpl); - fieldExpl.setMatch(Boolean.valueOf(tfExpl.isMatch())); - fieldExpl.setValue(tfExpl.getValue() * + fieldExpl.setMatch(Boolean.valueOf(tfExplanation.isMatch())); + fieldExpl.setValue(tfExplanation.getValue() * expl.getValue() * fieldNormExpl.getValue()); Index: src/java/org/apache/lucene/search/TermScorer.java =================================================================== --- src/java/org/apache/lucene/search/TermScorer.java (revision 830100) +++ src/java/org/apache/lucene/search/TermScorer.java (working copy) @@ -155,34 +155,6 @@ return doc; } - /** Returns an explanation of the score for a document. - * @param doc The document number for the explanation. - */ - public Explanation explain(int doc) throws IOException { - TermQuery query = (TermQuery) weight.getQuery(); - Explanation tfExplanation = new Explanation(); - int tf = 0; - while (pointer < pointerMax) { - if (docs[pointer] == doc) - tf = freqs[pointer]; - pointer++; - } - if (tf == 0) { - if (termDocs.skipTo(doc)) - { - if (termDocs.doc() == doc) - { - tf = termDocs.freq(); - } - } - } - termDocs.close(); - tfExplanation.setValue(getSimilarity().tf(tf)); - tfExplanation.setDescription("tf(termFreq("+query.getTerm()+")="+tf+")"); - - return tfExplanation; - } - /** Returns a string representation of this TermScorer. */ public String toString() { return "scorer(" + weight + ")"; } } Index: src/test/org/apache/lucene/search/JustCompileSearch.java =================================================================== --- src/test/org/apache/lucene/search/JustCompileSearch.java (revision 830100) +++ src/test/org/apache/lucene/search/JustCompileSearch.java (working copy) @@ -308,10 +308,6 @@ throw new UnsupportedOperationException(UNSUPPORTED_MSG); } - public Explanation explain(int doc) throws IOException { - throw new UnsupportedOperationException(UNSUPPORTED_MSG); - } - public float score() throws IOException { throw new UnsupportedOperationException(UNSUPPORTED_MSG); } Index: src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java =================================================================== --- src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java (revision 830100) +++ src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java (working copy) @@ -169,9 +169,7 @@ */ public void testSpanNearScorerExplain() throws Exception { SpanNearQuery q = makeQuery(); - Weight w = q.weight(searcher); - Scorer s = w.scorer(searcher.getIndexReader(), true, false); - Explanation e = s.explain(1); + Explanation e = q.weight(searcher).explain(searcher.getIndexReader(), 1); assertTrue("Scorer explanation value for doc#1 isn't positive: " + e.toString(), 0.0f < e.getValue()); Index: src/test/org/apache/lucene/search/TestBooleanScorer.java =================================================================== --- src/test/org/apache/lucene/search/TestBooleanScorer.java (revision 830100) +++ src/test/org/apache/lucene/search/TestBooleanScorer.java (working copy) @@ -80,15 +80,14 @@ Similarity sim = Similarity.getDefault(); Scorer[] scorers = new Scorer[] {new Scorer(sim) { private int doc = -1; - public Explanation explain(int doc) throws IOException { return null; } - public float score() throws IOException { return 0; } - public int docID() { return doc; } + @Override public float score() throws IOException { return 0; } + @Override public int docID() { return doc; } - public int nextDoc() throws IOException { + @Override public int nextDoc() throws IOException { return doc = doc == -1 ? 3000 : NO_MORE_DOCS; } - public int advance(int target) throws IOException { + @Override public int advance(int target) throws IOException { return doc = target <= 3000 ? 3000 : NO_MORE_DOCS; } Index: src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java =================================================================== --- src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java (revision 830100) +++ src/test/org/apache/lucene/search/TestPositiveScoresOnlyCollector.java (working copy) @@ -30,19 +30,17 @@ super(null); } - public Explanation explain(int doc) throws IOException { return null; } - - public float score() throws IOException { + @Override public float score() throws IOException { return idx == scores.length ? Float.NaN : scores[idx]; } - public int docID() { return idx; } + @Override public int docID() { return idx; } - public int nextDoc() throws IOException { + @Override public int nextDoc() throws IOException { return ++idx != scores.length ? idx : NO_MORE_DOCS; } - public int advance(int target) throws IOException { + @Override public int advance(int target) throws IOException { idx = target; return idx < scores.length ? idx : NO_MORE_DOCS; } Index: src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java =================================================================== --- src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java (revision 830100) +++ src/test/org/apache/lucene/search/TestScoreCachingWrappingScorer.java (working copy) @@ -32,9 +32,7 @@ super(null); } - public Explanation explain(int doc) throws IOException { return null; } - - public float score() throws IOException { + @Override public float score() throws IOException { // advance idx on purpose, so that consecutive calls to score will get // different results. This is to emulate computation of a score. If // ScoreCachingWrappingScorer is used, this should not be called more than @@ -42,13 +40,13 @@ return idx == scores.length ? Float.NaN : scores[idx++]; } - public int docID() { return doc; } + @Override public int docID() { return doc; } - public int nextDoc() throws IOException { + @Override public int nextDoc() throws IOException { return ++doc < scores.length ? doc : NO_MORE_DOCS; } - public int advance(int target) throws IOException { + @Override public int advance(int target) throws IOException { doc = target; return doc < scores.length ? doc : NO_MORE_DOCS; } @@ -65,7 +63,7 @@ mscores = new float[numToCollect]; } - public void collect(int doc) throws IOException { + @Override public void collect(int doc) throws IOException { // just a sanity check to avoid IOOB. if (idx == mscores.length) { return; @@ -78,15 +76,15 @@ ++idx; } - public void setNextReader(IndexReader reader, int docBase) + @Override public void setNextReader(IndexReader reader, int docBase) throws IOException { } - public void setScorer(Scorer scorer) throws IOException { + @Override public void setScorer(Scorer scorer) throws IOException { this.scorer = new ScoreCachingWrappingScorer(scorer); } - public boolean acceptsDocsOutOfOrder() { + @Override public boolean acceptsDocsOutOfOrder() { return true; } Index: src/test/org/apache/lucene/search/TestTermScorer.java =================================================================== --- src/test/org/apache/lucene/search/TestTermScorer.java (revision 830100) +++ src/test/org/apache/lucene/search/TestTermScorer.java (working copy) @@ -158,49 +158,6 @@ assertTrue("doc should be number 5", ts.docID() == 5); } - public void testExplain() throws Exception - { - Term allTerm = new Term(FIELD, "all"); - TermQuery termQuery = new TermQuery(allTerm); - - Weight weight = termQuery.weight(indexSearcher); - - TermScorer ts = new TermScorer(weight, - indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), - indexReader.norms(FIELD)); - Explanation explanation = ts.explain(0); - assertTrue("explanation is null and it shouldn't be", explanation != null); - //System.out.println("Explanation: " + explanation.toString()); - //All this Explain does is return the term frequency - assertTrue("term frq is not 1", explanation.getValue() == 1); - explanation = ts.explain(1); - assertTrue("explanation is null and it shouldn't be", explanation != null); - //System.out.println("Explanation: " + explanation.toString()); - //All this Explain does is return the term frequency - assertTrue("term frq is not 0", explanation.getValue() == 0); - - Term dogsTerm = new Term(FIELD, "dogs"); - termQuery = new TermQuery(dogsTerm); - weight = termQuery.weight(indexSearcher); - - ts = new TermScorer(weight, indexReader.termDocs(dogsTerm), indexSearcher.getSimilarity(), - indexReader.norms(FIELD)); - explanation = ts.explain(1); - assertTrue("explanation is null and it shouldn't be", explanation != null); - //System.out.println("Explanation: " + explanation.toString()); - //All this Explain does is return the term frequency - float sqrtTwo = (float)Math.sqrt(2.0f); - assertTrue("term frq: " + explanation.getValue() + " is not the square root of 2", explanation.getValue() == sqrtTwo); - - explanation = ts.explain(10);//try a doc out of range - assertTrue("explanation is null and it shouldn't be", explanation != null); - //System.out.println("Explanation: " + explanation.toString()); - //All this Explain does is return the term frequency - - assertTrue("term frq: " + explanation.getValue() + " is not 0", explanation.getValue() == 0); - - } - private class TestHit { public int doc; public float score;