Index: src/java/org/apache/lucene/search/BooleanScorer2.java =================================================================== --- src/java/org/apache/lucene/search/BooleanScorer2.java (revision 829476) +++ 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, Index: src/java/org/apache/lucene/search/DisjunctionSumScorer.java =================================================================== --- src/java/org/apache/lucene/search/DisjunctionSumScorer.java (revision 829476) +++ src/java/org/apache/lucene/search/DisjunctionSumScorer.java (working copy) @@ -236,31 +236,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 829476) +++ src/java/org/apache/lucene/search/FilteredQuery.java (working copy) @@ -149,19 +149,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/MultiPhraseQuery.java =================================================================== --- src/java/org/apache/lucene/search/MultiPhraseQuery.java (revision 829476) +++ src/java/org/apache/lucene/search/MultiPhraseQuery.java (working copy) @@ -219,7 +219,7 @@ if (scorer == null) { return new Explanation(0.0f, "no matching docs"); } - Explanation tfExpl = scorer.explain(doc); + Explanation tfExpl = ((PhraseScorer)scorer).explain(doc); fieldExpl.addDetail(tfExpl); fieldExpl.addDetail(idfExpl); Index: src/java/org/apache/lucene/search/PhraseQuery.java =================================================================== --- src/java/org/apache/lucene/search/PhraseQuery.java (revision 829476) +++ src/java/org/apache/lucene/search/PhraseQuery.java (working copy) @@ -208,7 +208,7 @@ 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"); } Index: src/java/org/apache/lucene/search/PhraseScorer.java =================================================================== --- src/java/org/apache/lucene/search/PhraseScorer.java (revision 829476) +++ src/java/org/apache/lucene/search/PhraseScorer.java (working copy) @@ -170,7 +170,7 @@ last.next = null; } - public Explanation explain(final int doc) throws IOException { + Explanation explain(final int doc) throws IOException { Explanation tfExplanation = new Explanation(); int d = advance(doc); Index: src/java/org/apache/lucene/search/ReqExclScorer.java =================================================================== --- src/java/org/apache/lucene/search/ReqExclScorer.java (revision 829476) +++ 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 829476) +++ src/java/org/apache/lucene/search/ReqOptSumScorer.java (working copy) @@ -75,17 +75,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 829476) +++ src/java/org/apache/lucene/search/ScoreCachingWrappingScorer.java (working copy) @@ -49,10 +49,6 @@ public Similarity getSimilarity() { return scorer.getSimilarity(); } - - public Explanation explain(int doc) throws IOException { - return scorer.explain(doc); - } public float score() throws IOException { int doc = scorer.docID(); Index: src/java/org/apache/lucene/search/Scorer.java =================================================================== --- src/java/org/apache/lucene/search/Scorer.java (revision 829476) +++ src/java/org/apache/lucene/search/Scorer.java (working copy) @@ -97,16 +97,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/TermQuery.java =================================================================== --- src/java/org/apache/lucene/search/TermQuery.java (revision 829476) +++ src/java/org/apache/lucene/search/TermQuery.java (working copy) @@ -104,7 +104,28 @@ fieldExpl.setDescription("fieldWeight("+term+" in "+doc+ "), product of:"); - Explanation tfExpl = scorer(reader, true, false).explain(doc); + + + Explanation tfExplanation = new Explanation(); + int tf = 0; + TermDocs termDocs = reader.termDocs(term); + + if (termDocs == null) { + tfExplanation = null; + } else { + if (tf == 0) { + if (termDocs.skipTo(doc)) { + if (termDocs.doc() == doc) { + tf = termDocs.freq(); + } + } + } + termDocs.close(); + tfExplanation.setValue(similarity.tf(tf)); + tfExplanation.setDescription("tf(termFreq(" + term + ")=" + tf + ")"); + } + + Explanation tfExpl = tfExplanation; fieldExpl.addDetail(tfExpl); fieldExpl.addDetail(expl); Index: src/java/org/apache/lucene/search/TermScorer.java =================================================================== --- src/java/org/apache/lucene/search/TermScorer.java (revision 829476) +++ src/java/org/apache/lucene/search/TermScorer.java (working copy) @@ -154,34 +154,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/java/org/apache/lucene/search/function/CustomScoreQuery.java =================================================================== --- src/java/org/apache/lucene/search/function/CustomScoreQuery.java (revision 829476) +++ src/java/org/apache/lucene/search/function/CustomScoreQuery.java (working copy) @@ -348,18 +348,15 @@ } 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 +430,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 829476) +++ 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/spans/SpanWeight.java =================================================================== --- src/java/org/apache/lucene/search/spans/SpanWeight.java (revision 829476) +++ 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/test/org/apache/lucene/search/TestTermScorer.java =================================================================== --- src/test/org/apache/lucene/search/TestTermScorer.java (revision 829476) +++ src/test/org/apache/lucene/search/TestTermScorer.java (working copy) @@ -168,12 +168,12 @@ TermScorer ts = new TermScorer(weight, indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), indexReader.norms(FIELD)); - Explanation explanation = ts.explain(0); + Explanation explanation = weight.explain(indexReader, 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); + explanation = weight.explain(indexReader, 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 @@ -185,14 +185,14 @@ ts = new TermScorer(weight, indexReader.termDocs(dogsTerm), indexSearcher.getSimilarity(), indexReader.norms(FIELD)); - explanation = ts.explain(1); + explanation = weight.explain(indexReader, 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 + explanation = weight.explain(indexReader, 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 Index: src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java =================================================================== --- src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java (revision 829476) +++ src/test/org/apache/lucene/search/spans/TestNearSpansOrdered.java (working copy) @@ -20,6 +20,7 @@ import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.QueryParser; @@ -170,8 +171,8 @@ 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); + IndexReader reader = searcher.getIndexReader(); + Explanation e = w.explain(reader, 1); assertTrue("Scorer explanation value for doc#1 isn't positive: " + e.toString(), 0.0f < e.getValue());