Apache Lucene open-source search software
Revised explanation format so that it better corresponds to a dot product of tf*idf weights.
Doug Cutting4642f95eb93d
diff --git a/src/java/org/apache/lucene/search/BooleanQuery.java b/src/java/org/apache/lucene/search/BooleanQuery.javaindex 6d408454833..67b7f12048e 100644--- a/src/java/org/apache/lucene/search/BooleanQuery.java+++ b/src/java/org/apache/lucene/search/BooleanQuery.java@@ -150,13 +150,48 @@ public class BooleanQuery extends Query {return result;}- public Explanation explain() throws IOException {- Explanation result = new Explanation();- result.setDescription("boost(" + getQuery() + ")");- result.setValue(getBoost());- return result;- }+ public Explanation explain(IndexReader reader, int doc)+ throws IOException {+ Explanation sumExpl = new Explanation();+ sumExpl.setDescription("sum of:");+ int coord = 0;+ int maxCoord = 0;+ float sum = 0.0f;+ for (int i = 0 ; i < weights.size(); i++) {+ BooleanClause c = (BooleanClause)clauses.elementAt(0);+ Weight w = (Weight)weights.elementAt(i);+ Explanation e = w.explain(reader, doc);+ if (!c.prohibited) maxCoord++;+ if (e.getValue() > 0) {+ if (!c.prohibited) {+ sumExpl.addDetail(e);+ sum += e.getValue();+ coord++;+ } else {+ return new Explanation(0.0f, "match prohibited");+ }+ } else if (c.required) {+ return new Explanation(0.0f, "match required");+ }+ }+ sumExpl.setValue(sum);++ if (coord == 1) // only one clause matched+ sumExpl = sumExpl.getDetails()[0]; // eliminate wrapper+ float coordFactor = searcher.getSimilarity().coord(coord, maxCoord);+ if (coordFactor == 1.0f) // coord is no-op+ return sumExpl; // eliminate wrapper+ else {+ Explanation result = new Explanation();+ result.setDescription("product of:");+ result.addDetail(sumExpl);+ result.addDetail(new Explanation(coordFactor,+ "coord("+coord+"/"+maxCoord+")"));+ result.setValue(sum*coordFactor);+ return result;+ }+ }}protected Weight createWeight(Searcher searcher) {diff --git a/src/java/org/apache/lucene/search/BooleanScorer.java b/src/java/org/apache/lucene/search/BooleanScorer.javaindex cd696b68f8d..8d2bf0eae84 100644--- a/src/java/org/apache/lucene/search/BooleanScorer.java+++ b/src/java/org/apache/lucene/search/BooleanScorer.java@@ -208,41 +208,7 @@ final class BooleanScorer extends Scorer {}public Explanation explain(int doc) throws IOException {- Explanation sumExpl = new Explanation();- sumExpl.setDescription("sum of:");- int coord = 0;- float sum = 0.0f;- for (SubScorer s = scorers; s != null; s = s.next) {- Explanation e = s.scorer.explain(doc);- if (e.getValue() > 0) {- if (!s.prohibited) {- sumExpl.addDetail(e);- sum += e.getValue();- coord++;- } else {- return new Explanation(0.0f, "match prohibited");- }- } else if (s.required) {- return new Explanation(0.0f, "match required");- }- }- sumExpl.setValue(sum);-- if (coord == 1) // only one clause matched- sumExpl = sumExpl.getDetails()[0]; // eliminate wrapper-- float coordFactor = getSimilarity().coord(coord, maxCoord-1);- if (coordFactor == 1.0f) // coord is no-op- return sumExpl; // eliminate wrapper- else {- Explanation result = new Explanation();- result.setDescription("product of:");- result.addDetail(sumExpl);- result.addDetail(new Explanation(coordFactor,- "coord("+coord+"/"+(maxCoord-1)+")"));- result.setValue(sum*coordFactor);- return result;- }+ throw new UnsupportedOperationException();}}diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.javaindex 1a3e5c17ef9..86a8496035d 100644--- a/src/java/org/apache/lucene/search/IndexSearcher.java+++ b/src/java/org/apache/lucene/search/IndexSearcher.java@@ -197,7 +197,7 @@ public class IndexSearcher extends Searcher implements Searchable {}public Explanation explain(Query query, int doc) throws IOException {- return query.weight(this).scorer(reader).explain(doc);+ return query.weight(this).explain(reader, doc);}}diff --git a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java b/src/java/org/apache/lucene/search/PhrasePrefixQuery.javaindex 62cfa44bfe4..c90d14c60d5 100644--- a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java+++ b/src/java/org/apache/lucene/search/PhrasePrefixQuery.java@@ -125,6 +125,7 @@ public class PhrasePrefixQuery extends Query {private float value;private float idf;private float queryNorm;+ private float queryWeight;public PhrasePrefixWeight(Searcher searcher) {this.searcher = searcher;@@ -141,14 +142,14 @@ public class PhrasePrefixQuery extends Query {idf += searcher.getSimilarity().idf(terms[j], searcher);}- value = idf * getBoost();- return value * value;+ queryWeight = idf * getBoost(); // compute query weight+ return queryWeight * queryWeight; // square it}- public void normalize(float norm) {- queryNorm = norm;- queryNorm *= idf; // factor from document- value *= queryNorm; // normalize for query+ public void normalize(float queryNorm) {+ this.queryNorm = queryNorm;+ queryWeight *= queryNorm; // normalize query weight+ value = queryWeight * idf; // idf for document}public Scorer scorer(IndexReader reader) throws IOException {@@ -179,25 +180,57 @@ public class PhrasePrefixQuery extends Query {slop, reader.norms(field));}- public Explanation explain() throws IOException {- Query q = getQuery();-+ public Explanation explain(IndexReader reader, int doc)+ throws IOException {Explanation result = new Explanation();- result.setDescription("weight(" + getQuery() + "), product of:");+ result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");++ Explanation idfExpl = new Explanation(idf, "idf("+getQuery()+")");++ // explain query weight+ Explanation queryExpl = new Explanation();+ queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");Explanation boostExpl = new Explanation(getBoost(), "boost");if (getBoost() != 1.0f)- result.addDetail(boostExpl);+ queryExpl.addDetail(boostExpl);++ queryExpl.addDetail(idfExpl);++ Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");+ queryExpl.addDetail(queryNormExpl);- Explanation idfExpl = new Explanation(idf, "idf");- result.addDetail(idfExpl);+ queryExpl.setValue(boostExpl.getValue() *+ idfExpl.getValue() *+ queryNormExpl.getValue());++ result.addDetail(queryExpl);++ // explain field weight+ Explanation fieldExpl = new Explanation();+ fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc++ "), product of:");++ Explanation tfExpl = scorer(reader).explain(doc);+ fieldExpl.addDetail(tfExpl);+ fieldExpl.addDetail(idfExpl);++ Explanation fieldNormExpl = new Explanation();+ fieldNormExpl.setValue(Similarity.decodeNorm(reader.norms(field)[doc]));+ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");+ fieldExpl.addDetail(fieldNormExpl);++ fieldExpl.setValue(tfExpl.getValue() *+ idfExpl.getValue() *+ fieldNormExpl.getValue());- Explanation normExpl = new Explanation(queryNorm, "queryNorm");- result.addDetail(normExpl);+ result.addDetail(fieldExpl);++ // combine them+ result.setValue(queryExpl.getValue() * fieldExpl.getValue());- result.setValue(boostExpl.getValue() *- idfExpl.getValue() *- normExpl.getValue());+ if (queryExpl.getValue() == 1.0f)+ return fieldExpl;return result;}diff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.javaindex be5262f4e0e..5c2bb4f1350 100644--- a/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/src/java/org/apache/lucene/search/PhraseQuery.java@@ -112,6 +112,7 @@ public class PhraseQuery extends Query {private float value;private float idf;private float queryNorm;+ private float queryWeight;public PhraseWeight(Searcher searcher) {this.searcher = searcher;@@ -122,14 +123,14 @@ public class PhraseQuery extends Query {public float sumOfSquaredWeights() throws IOException {idf = searcher.getSimilarity().idf(terms, searcher);- value = idf * getBoost();- return value * value; // square term weights+ queryWeight = idf * getBoost(); // compute query weight+ return queryWeight * queryWeight; // square it}- public void normalize(float norm) {- queryNorm = norm;- queryNorm *= idf; // factor from document- value *= queryNorm; // normalize for query+ public void normalize(float queryNorm) {+ this.queryNorm = queryNorm;+ queryWeight *= queryNorm; // normalize query weight+ value = queryWeight * idf; // idf for document}public Scorer scorer(IndexReader reader) throws IOException {@@ -154,33 +155,77 @@ public class PhraseQuery extends Query {}- public Explanation explain() throws IOException {- Query q = getQuery();+ public Explanation explain(IndexReader reader, int doc)+ throws IOException {Explanation result = new Explanation();- result.setDescription("weight(" + getQuery() + "), product of:");+ result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");- Explanation boostExpl = new Explanation(getBoost(), "boost");- if (getBoost() != 1.0f)- result.addDetail(boostExpl);-StringBuffer docFreqs = new StringBuffer();+ StringBuffer query = new StringBuffer();+ query.append('\"');for (int i = 0; i < terms.size(); i++) {- if (i != 0) docFreqs.append(" ");- docFreqs.append(((Term)terms.elementAt(i)).text());+ if (i != 0) {+ docFreqs.append(" ");+ query.append(" ");+ }++ Term term = (Term)terms.elementAt(i);++ docFreqs.append(term.text());docFreqs.append("=");- docFreqs.append(searcher.docFreq((Term)terms.elementAt(i)));+ docFreqs.append(searcher.docFreq(term));++ query.append(term.text());}+ query.append('\"');+Explanation idfExpl =new Explanation(idf, "idf(" + field + ": " + docFreqs + ")");- result.addDetail(idfExpl);- Explanation normExpl = new Explanation(queryNorm, "queryNorm");- result.addDetail(normExpl);+ // explain query weight+ Explanation queryExpl = new Explanation();+ queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");++ Explanation boostExpl = new Explanation(getBoost(), "boost");+ if (getBoost() != 1.0f)+ queryExpl.addDetail(boostExpl);+ queryExpl.addDetail(idfExpl);++ Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");+ queryExpl.addDetail(queryNormExpl);++ queryExpl.setValue(boostExpl.getValue() *+ idfExpl.getValue() *+ queryNormExpl.getValue());++ result.addDetail(queryExpl);++ // explain field weight+ Explanation fieldExpl = new Explanation();+ fieldExpl.setDescription("fieldWeight("+field+":"+query+" in "+doc++ "), product of:");++ Explanation tfExpl = scorer(reader).explain(doc);+ fieldExpl.addDetail(tfExpl);+ fieldExpl.addDetail(idfExpl);++ Explanation fieldNormExpl = new Explanation();+ fieldNormExpl.setValue(Similarity.decodeNorm(reader.norms(field)[doc]));+ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");+ fieldExpl.addDetail(fieldNormExpl);++ fieldExpl.setValue(tfExpl.getValue() *+ idfExpl.getValue() *+ fieldNormExpl.getValue());++ result.addDetail(fieldExpl);++ // combine them+ result.setValue(queryExpl.getValue() * fieldExpl.getValue());- result.setValue(boostExpl.getValue() *- idfExpl.getValue() *- normExpl.getValue());+ if (queryExpl.getValue() == 1.0f)+ return fieldExpl;return result;}diff --git a/src/java/org/apache/lucene/search/PhraseScorer.java b/src/java/org/apache/lucene/search/PhraseScorer.javaindex 2bd5ee5b6b7..f316c4621b0 100644--- a/src/java/org/apache/lucene/search/PhraseScorer.java+++ b/src/java/org/apache/lucene/search/PhraseScorer.java@@ -130,15 +130,8 @@ abstract class PhraseScorer extends Scorer {}public Explanation explain(final int doc) throws IOException {- Explanation result = new Explanation();- PhraseQuery query = (PhraseQuery)weight.getQuery();-- result.setDescription("phraseScore(" + query + "), product of:");-- Explanation weightExplanation = weight.explain();- result.addDetail(weightExplanation);-Explanation tfExplanation = new Explanation();+score(new HitCollector() {public final void collect(int d, float score) {}}, doc+1);@@ -146,19 +139,8 @@ abstract class PhraseScorer extends Scorer {float phraseFreq = (first.doc == doc) ? freq : 0.0f;tfExplanation.setValue(getSimilarity().tf(phraseFreq));tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");- result.addDetail(tfExplanation);-- Explanation normExplanation = new Explanation();- normExplanation.setValue(Similarity.decodeNorm(norms[doc]));- String field = query.getTerms()[0].field();- normExplanation.setDescription("norm(field="+field + ", doc="+doc + ")");- result.addDetail(normExplanation);-- result.setValue(weightExplanation.getValue() *- tfExplanation.getValue() *- normExplanation.getValue());-- return result;++ return tfExplanation;}}diff --git a/src/java/org/apache/lucene/search/TermQuery.java b/src/java/org/apache/lucene/search/TermQuery.javaindex 572e854be57..53094506d21 100644--- a/src/java/org/apache/lucene/search/TermQuery.java+++ b/src/java/org/apache/lucene/search/TermQuery.java@@ -70,6 +70,7 @@ public class TermQuery extends Query {private float value;private float idf;private float queryNorm;+ private float queryWeight;public TermWeight(Searcher searcher) {this.searcher = searcher;@@ -79,15 +80,15 @@ public class TermQuery extends Query {public float getValue() { return value; }public float sumOfSquaredWeights() throws IOException {- idf = searcher.getSimilarity().idf(term, searcher);- value = idf * getBoost();- return value * value; // square term weights+ idf = searcher.getSimilarity().idf(term, searcher); // compute idf+ queryWeight = idf * getBoost(); // compute query weight+ return queryWeight * queryWeight; // square it}- public void normalize(float norm) {- queryNorm = norm;- queryNorm *= idf; // factor from document- value *= queryNorm; // normalize for query+ public void normalize(float queryNorm) {+ this.queryNorm = queryNorm;+ queryWeight *= queryNorm; // normalize query weight+ value = queryWeight * idf; // idf for document}public Scorer scorer(IndexReader reader) throws IOException {@@ -100,26 +101,59 @@ public class TermQuery extends Query {reader.norms(term.field()));}- public Explanation explain() throws IOException {- Query q = getQuery();+ public Explanation explain(IndexReader reader, int doc)+ throws IOException {Explanation result = new Explanation();- result.setDescription("weight(" + getQuery() + "), product of:");+ result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");++ Explanation idfExpl =+ new Explanation(idf, "idf(docFreq=" + searcher.docFreq(term) + ")");++ // explain query weight+ Explanation queryExpl = new Explanation();+ queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");Explanation boostExpl = new Explanation(getBoost(), "boost");if (getBoost() != 1.0f)- result.addDetail(boostExpl);+ queryExpl.addDetail(boostExpl);+ queryExpl.addDetail(idfExpl);- Explanation idfExpl =- new Explanation(idf, "idf(docFreq=" + searcher.docFreq(term) + ")");- result.addDetail(idfExpl);+ Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");+ queryExpl.addDetail(queryNormExpl);- Explanation normExpl = new Explanation(queryNorm,"queryNorm");- result.addDetail(normExpl);+ queryExpl.setValue(boostExpl.getValue() *+ idfExpl.getValue() *+ queryNormExpl.getValue());++ result.addDetail(queryExpl);++ // explain field weight+ String field = term.field();+ Explanation fieldExpl = new Explanation();+ fieldExpl.setDescription("fieldWeight("+term+" in "+doc++ "), product of:");++ Explanation tfExpl = scorer(reader).explain(doc);+ fieldExpl.addDetail(tfExpl);+ fieldExpl.addDetail(idfExpl);++ Explanation fieldNormExpl = new Explanation();+ fieldNormExpl.setValue(Similarity.decodeNorm(reader.norms(field)[doc]));+ fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");+ fieldExpl.addDetail(fieldNormExpl);++ fieldExpl.setValue(tfExpl.getValue() *+ idfExpl.getValue() *+ fieldNormExpl.getValue());++ result.addDetail(fieldExpl);++ // combine them+ result.setValue(queryExpl.getValue() * fieldExpl.getValue());- result.setValue(boostExpl.getValue() *- idfExpl.getValue() *- normExpl.getValue());+ if (queryExpl.getValue() == 1.0f)+ return fieldExpl;return result;}diff --git a/src/java/org/apache/lucene/search/TermScorer.java b/src/java/org/apache/lucene/search/TermScorer.javaindex 45b1cdde066..4e1c20f0a63 100644--- a/src/java/org/apache/lucene/search/TermScorer.java+++ b/src/java/org/apache/lucene/search/TermScorer.java@@ -124,14 +124,7 @@ final class TermScorer extends Scorer {}public Explanation explain(int doc) throws IOException {- Explanation result = new Explanation();TermQuery query = (TermQuery)weight.getQuery();-- result.setDescription("termScore(" + query + "), product of:");-- Explanation weightExplanation = weight.explain();- result.addDetail(weightExplanation);-Explanation tfExplanation = new Explanation();int tf = 0;while (pointer < pointerMax) {@@ -149,18 +142,7 @@ final class TermScorer extends Scorer {termDocs.close();tfExplanation.setValue(getSimilarity().tf(tf));tfExplanation.setDescription("tf(termFreq("+query.getTerm()+")="+tf+")");- result.addDetail(tfExplanation);-- Explanation normExplanation = new Explanation();- normExplanation.setValue(Similarity.decodeNorm(norms[doc]));- String field = query.getTerm().field();- normExplanation.setDescription("norm(field="+field + ", doc="+doc + ")");- result.addDetail(normExplanation);-- result.setValue(weightExplanation.getValue() *- tfExplanation.getValue() *- normExplanation.getValue());- return result;+ return tfExplanation;}}diff --git a/src/java/org/apache/lucene/search/Weight.java b/src/java/org/apache/lucene/search/Weight.javaindex 6cf58b13237..9784a277502 100644--- a/src/java/org/apache/lucene/search/Weight.java+++ b/src/java/org/apache/lucene/search/Weight.java@@ -83,6 +83,6 @@ public interface Weight extends java.io.Serializable {/** Constructs a scorer for this. */Scorer scorer(IndexReader reader) throws IOException;- /** An explanation of this weight computation. */- Explanation explain() throws IOException;+ /** An explanation of the score computation for the named document. */+ Explanation explain(IndexReader reader, int doc) throws IOException;}