Apache Lucene open-source search software
LUCENE-8011: Improve similarity explanations
Mayya Sharipova6771886cf330
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex c2af45c81ca..5d31396b52c 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -47,6 +47,9 @@ ImprovementsAdd missing range checks for similarity parameters.Improve BM25 and ClassicSimilarity's explanations. (Robert Muir)+* LUCENE-8011: Improved similarity explanations.+ (Mayya Sharipova via Adrien Grand)+Optimizations* LUCENE-8040: Optimize IndexSearcher.collectionStatistics, avoiding MultiFields/MultiTermsdiff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectB.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectB.javaindex 6678cd9d721..66ee1b901eb 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectB.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectB.java@@ -39,10 +39,14 @@ public class AfterEffectB extends AfterEffect {public final Explanation explain(BasicStats stats, double tfn) {return Explanation.match((float) (scoreTimes1pTfn(stats) / (1 + tfn)),- getClass().getSimpleName() + ", computed from: ",- Explanation.match((float) tfn, "tfn"),- Explanation.match(stats.getTotalTermFreq(), "totalTermFreq"),- Explanation.match(stats.getDocFreq(), "docFreq"));+ getClass().getSimpleName()+ + ", computed as (F + 1) / (n * (tfn + 1)) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"),+ Explanation.match(stats.getTotalTermFreq(),+ "F, total number of occurrences of term across all documents + 1"),+ Explanation.match(stats.getDocFreq(),+ "n, number of documents containing term + 1"),+ Explanation.match((float) tfn, "tfn, normalized term frequency"));}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectL.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectL.javaindex 60a1b1dc9c7..283c3ac8ad0 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectL.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AfterEffectL.java@@ -37,8 +37,8 @@ public class AfterEffectL extends AfterEffect {public final Explanation explain(BasicStats stats, double tfn) {return Explanation.match((float) (scoreTimes1pTfn(stats) / (1 + tfn)),- getClass().getSimpleName() + ", computed from: ",- Explanation.match((float) tfn, "tfn"));+ getClass().getSimpleName() + ", computed as 1 / (tfn + 1) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"));}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/Axiomatic.java b/lucene/core/src/java/org/apache/lucene/search/similarities/Axiomatic.javaindex 2a7f3532fd8..403773e37f3 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/Axiomatic.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/Axiomatic.java@@ -118,16 +118,19 @@ public abstract class Axiomatic extends SimilarityBase {protected void explain(List<Explanation> subs, BasicStats stats, int doc,double freq, double docLen) {if (stats.getBoost() != 1.0d) {- subs.add(Explanation.match((float) stats.getBoost(), "boost"));+ subs.add(Explanation.match((float) stats.getBoost(),+ "boost, query boost"));}- subs.add(Explanation.match(this.k, "k"));- subs.add(Explanation.match(this.s, "s"));- subs.add(Explanation.match(this.queryLen, "queryLen"));- subs.add(Explanation.match((float) tf(stats, freq, docLen), "tf"));- subs.add(Explanation.match((float) ln(stats, freq, docLen), "ln"));- subs.add(Explanation.match((float) tfln(stats, freq, docLen), "tfln"));- subs.add(Explanation.match((float) idf(stats, freq, docLen), "idf"));+ subs.add(Explanation.match(this.k,+ "k, hyperparam for the primitive weighting function"));+ subs.add(Explanation.match(this.s,+ "s, hyperparam for the growth function"));+ subs.add(Explanation.match(this.queryLen, "queryLen, query length"));+ subs.add(tfExplain(stats, freq, docLen));+ subs.add(lnExplain(stats, freq, docLen));+ subs.add(tflnExplain(stats, freq, docLen));+ subs.add(idfExplain(stats, freq, docLen));subs.add(Explanation.match((float) gamma(stats, freq, docLen), "gamma"));super.explain(subs, stats, doc, freq, docLen);}@@ -162,4 +165,47 @@ public abstract class Axiomatic extends SimilarityBase {* compute the gamma component (only for F3EXp and F3LOG)*/protected abstract double gamma(BasicStats stats, double freq, double docLen);+++ /**+ * Explain the score of the term frequency component for a single document+ * @param stats the corpus level statistics+ * @param freq number of occurrences of term in the document+ * @param docLen the document length+ * @return Explanation of how the tf component was computed+ */+ protected abstract Explanation tfExplain(BasicStats stats,+ double freq, double docLen);++ /**+ * Explain the score of the document length component for a single document+ * @param stats the corpus level statistics+ * @param freq number of occurrences of term in the document+ * @param docLen the document length+ * @return Explanation of how the ln component was computed+ */+ protected abstract Explanation lnExplain(BasicStats stats,+ double freq, double docLen);++ /**+ * Explain the score of the mixed term frequency and+ * document length component for a single document+ * @param stats the corpus level statistics+ * @param freq number of occurrences of term in the document+ * @param docLen the document length+ * @return Explanation of how the tfln component was computed+ */+ protected abstract Explanation tflnExplain(BasicStats stats,+ double freq, double docLen);++ /**+ * Explain the score of the inverted document frequency component+ * for a single document+ * @param stats the corpus level statistics+ * @param freq number of occurrences of term in the document+ * @param docLen the document length+ * @return Explanation of how the idf component was computed+ */+ protected abstract Explanation idfExplain(BasicStats stats, double freq, double docLen);+}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1EXP.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1EXP.javaindex 0619b4e96f1..c026feb72bb 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1EXP.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1EXP.java@@ -16,6 +16,9 @@*/package org.apache.lucene.search.similarities;++import org.apache.lucene.search.Explanation;+/*** F1EXP is defined as Sum(tf(term_doc_freq)*ln(docLen)*IDF(term))* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq@@ -92,4 +95,37 @@ public class AxiomaticF1EXP extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return 0.0;}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency computed as 1 + log(1 + log(freq)) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"));+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length computed as (avgdl + s) / (avgdl + dl * s) from:",+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgdl, average length of field across all documents"),+ Explanation.match((float) docLen,+ "dl, length of field"));+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, equals to 1");+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as " ++ "Math.pow((N + 1) / n, k) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1LOG.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1LOG.javaindex f7a02da8c99..2e192557c68 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1LOG.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF1LOG.java@@ -16,6 +16,8 @@*/package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** F1LOG is defined as Sum(tf(term_doc_freq)*ln(docLen)*IDF(term))* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq@@ -85,4 +87,36 @@ public class AxiomaticF1LOG extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return 0.0;}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency computed as 1 + log(1 + log(freq)) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"));+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length computed as (avgdl + s) / (avgdl + dl * s) from:",+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgdl, average length of field across all documents"),+ Explanation.match((float) docLen,+ "dl, length of field"));+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, equals to 1");+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as log((N + 1) / n) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2EXP.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2EXP.javaindex 0a3e4ad7cc5..bd280481885 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2EXP.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2EXP.java@@ -16,6 +16,8 @@*/package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** F2EXP is defined as Sum(tfln(term_doc_freq, docLen)*IDF(term))* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq@@ -91,4 +93,38 @@ public class AxiomaticF2EXP extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return 0.0;}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency, equals to 1");+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length, equals to 1");+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, " ++ "computed as freq / (freq + s + s * dl / avgdl) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"),+ Explanation.match((float) docLen,+ "dl, length of field"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgdl, average length of field across all documents"));+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as " ++ "Math.pow((N + 1) / n, k) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2LOG.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2LOG.javaindex 2fc5e11a9c7..4780d1e14a9 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2LOG.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF2LOG.java@@ -16,6 +16,8 @@*/package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** F2EXP is defined as Sum(tfln(term_doc_freq, docLen)*IDF(term))* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq@@ -83,4 +85,37 @@ public class AxiomaticF2LOG extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return 0.0;}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency, equals to 1");+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length, equals to 1");+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, " ++ "computed as freq / (freq + s + s * dl / avgdl) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"),+ Explanation.match((float) docLen,+ "dl, length of field"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgdl, average length of field across all documents"));+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as log((N + 1) / n) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3EXP.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3EXP.javaindex 5c763178204..635dc68e3bd 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3EXP.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3EXP.java@@ -16,6 +16,8 @@*/package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** F3EXP is defined as Sum(tf(term_doc_freq)*IDF(term)-gamma(docLen, queryLen))* where IDF(t) = pow((N+1)/df(t), k) N=total num of docs, df=doc freq@@ -91,4 +93,33 @@ public class AxiomaticF3EXP extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return (docLen - this.queryLen) * this.s * this.queryLen / stats.getAvgFieldLength();}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency computed as 1 + log(1 + log(freq)) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"));+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length, equals to 1");+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, equals to 1");+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as " ++ "Math.pow((N + 1) / n, k) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3LOG.java b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3LOG.javaindex 22a50b1c78f..4753e4e9ffc 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3LOG.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/AxiomaticF3LOG.java@@ -16,6 +16,8 @@*/package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** F3EXP is defined as Sum(tf(term_doc_freq)*IDF(term)-gamma(docLen, queryLen))* where IDF(t) = ln((N+1)/df(t)) N=total num of docs, df=doc freq@@ -80,4 +82,32 @@ public class AxiomaticF3LOG extends Axiomatic {protected double gamma(BasicStats stats, double freq, double docLen) {return (docLen - this.queryLen) * this.s * this.queryLen / stats.getAvgFieldLength();}++ @Override+ protected Explanation tfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tf(stats, freq, docLen),+ "tf, term frequency computed as 1 + log(1 + log(freq)) from:",+ Explanation.match((float) freq,+ "freq, number of occurrences of term in the document"));+ };++ @Override+ protected Explanation lnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) ln(stats, freq, docLen),+ "ln, document length, equals to 1");+ };++ protected Explanation tflnExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) tfln(stats, freq, docLen),+ "tfln, mixed term frequency and document length, equals to 1");+ };++ protected Explanation idfExplain(BasicStats stats, double freq, double docLen){+ return Explanation.match((float) idf(stats, freq, docLen),+ "idf, inverted document frequency computed as log((N + 1) / n) from:",+ Explanation.match((float) stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match((float) stats.getDocFreq(),+ "n, number of documents containing term"));+ };}\ No newline at end of filediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModel.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModel.javaindex 51d4571e6a7..7bcdd24c330 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModel.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModel.java@@ -41,21 +41,13 @@ public abstract class BasicModel {* non-decreasing with {@code tfn}. */public abstract double score(BasicStats stats, double tfn, double aeTimes1pTfn);+/*** Returns an explanation for the score.- * <p>Most basic models use the number of documents and the total term- * frequency to compute Inf<sub>1</sub>. This method provides a generic- * explanation for such models. Subclasses that use other statistics must- * override this method.</p>+ * Subclasses must override this method.*/- public Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {- return Explanation.match(- (float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),- getClass().getSimpleName() + ", computed from: ",- Explanation.match(stats.getNumberOfDocuments(), "numberOfDocuments"),- Explanation.match(stats.getTotalTermFreq(), "totalTermFreq"));- }-+ public abstract Explanation explain (BasicStats stats, double tfn, double aeTimes1pTfn);+/*** Subclasses must override this method to return the code of the* basic model formula. Refer to the original paper for the list.diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelG.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelG.javaindex ce871967f9b..72c0219e6d8 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelG.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelG.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+import static org.apache.lucene.search.similarities.SimilarityBase.log2;/**@@ -48,6 +50,26 @@ public class BasicModelG extends BasicModel {return (B - (B - A) / (1 + tfn)) * aeTimes1pTfn;}+ @Override+ public Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {+ double F = stats.getTotalTermFreq() + 1;+ double N = stats.getNumberOfDocuments();+ double lambda = F / (N + F);+ Explanation explLambda = Explanation.match((float) lambda,+ "lambda, computed as F / (N + F) from:",+ Explanation.match((float) F,+ "F, total number of occurrences of term across all docs + 1"),+ Explanation.match((float) N,+ "N, total number of documents with field"));++ return Explanation.match(+ (float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),+ getClass().getSimpleName() + ", computed as " ++ "log2(lambda + 1) + tfn * log2((1 + lambda) / lambda) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"),+ explLambda);+ }+@Overridepublic String toString() {return "G";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIF.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIF.javaindex 16781cdf496..974420e59a1 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIF.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIF.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+import static org.apache.lucene.search.similarities.SimilarityBase.log2;/**@@ -42,6 +44,19 @@ public class BasicModelIF extends BasicModel {return A * aeTimes1pTfn * (1 - 1 / (1 + tfn));}+ @Override+ public Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {+ return Explanation.match(+ (float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),+ getClass().getSimpleName() + ", computed as " ++ "tfn * log2(1 + (N + 1) / (F + 0.5)) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"),+ Explanation.match(stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match(stats.getTotalTermFreq(),+ "F, total number of occurrences of term across all documents"));+ }+@Overridepublic String toString() {return "I(F)";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIn.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIn.javaindex 5f1e1814c81..82b65f42f96 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIn.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIn.java@@ -47,9 +47,13 @@ public class BasicModelIn extends BasicModel {public final Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {return Explanation.match((float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),- getClass().getSimpleName() + ", computed from: ",- Explanation.match(stats.getNumberOfDocuments(), "numberOfDocuments"),- Explanation.match(stats.getDocFreq(), "docFreq"));+ getClass().getSimpleName() ++ ", computed as tfn * log2((N + 1) / (n + 0.5)) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"),+ Explanation.match(stats.getNumberOfDocuments(),+ "N, total number of documents with field"),+ Explanation.match(stats.getDocFreq(),+ "n, number of documents containing term"));}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.javaindex fb755fa50dd..c84697694fe 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BasicModelIne.java@@ -17,6 +17,7 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;import static org.apache.lucene.search.similarities.SimilarityBase.log2;/**@@ -44,6 +45,26 @@ public class BasicModelIne extends BasicModel {return A * aeTimes1pTfn * (1 - 1 / (1 + tfn));}+ @Override+ public Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {+ double F = stats.getTotalTermFreq();+ double N = stats.getNumberOfDocuments();+ double ne = N * (1 - Math.pow((N - 1) / N, F));+ Explanation explNe = Explanation.match((float) ne,+ "ne, computed as N * (1 - Math.pow((N - 1) / N, F)) from:",+ Explanation.match((float) F,+ "F, total number of occurrences of term across all docs"),+ Explanation.match((float) N,+ "N, total number of documents with field"));++ return Explanation.match(+ (float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),+ getClass().getSimpleName() + ", computed as " ++ "tfn * log2((N + 1) / (ne + 0.5)) from:",+ Explanation.match((float) tfn, "tfn, normalized term frequency"),+ explNe);+ }+@Overridepublic String toString() {return "I(ne)";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/BooleanSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/BooleanSimilarity.javaindex 35f70834b41..713417233bc 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/BooleanSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/BooleanSimilarity.java@@ -77,7 +77,7 @@ public class BooleanSimilarity extends Similarity {@Overridepublic Explanation explain(int doc, Explanation freq) throws IOException {- Explanation queryBoostExpl = Explanation.match(boost, "boost");+ Explanation queryBoostExpl = Explanation.match(boost, "boost, query boost");return Explanation.match(queryBoostExpl.getValue(),"score(" + getClass().getSimpleName() + ", doc=" + doc + "), computed from:",diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/DFISimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/DFISimilarity.javaindex ca0f4aa4562..6ebe9cb8340 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/DFISimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/DFISimilarity.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** Implements the <em>Divergence from Independence (DFI)</em> model based on Chi-square statistics* (i.e., standardized Chi-squared distance from independence in term frequency tf).@@ -75,6 +77,38 @@ public class DFISimilarity extends SimilarityBase {return independence;}+ @Override+ protected Explanation explain(+ BasicStats stats, int doc, Explanation freq, double docLen) {+ final double expected = (stats.getTotalTermFreq() + 1) * docLen /+ (stats.getNumberOfFieldTokens() + 1);+ if (freq.getValue() <= expected){+ return Explanation.match((float) 0, "score(" ++ getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), equals to 0");+ }+ Explanation explExpected = Explanation.match((float) expected,+ "expected, computed as (F + 1) * dl / (T + 1) from:",+ Explanation.match(stats.getTotalTermFreq(),+ "F, total number of occurrences of term across all docs"),+ Explanation.match((float) docLen, "dl, length of field"),+ Explanation.match(stats.getNumberOfFieldTokens(),+ "T, total number of tokens in the field"));++ final double measure = independence.score(freq.getValue(), expected);+ Explanation explMeasure = Explanation.match((float) measure,+ "measure, computed as independence.score(freq, expected) from:",+ freq,+ explExpected);++ return Explanation.match(+ (float) score(stats, freq.getValue(), docLen),+ "score(" + getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), computed as boost * log2(measure + 1) from:",+ Explanation.match( (float)stats.getBoost(), "boost, query boost"),+ explMeasure);+ }+@Overridepublic String toString() {return "DFI(" + independence + ")";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/DFRSimilarity.javaindex 788f30a9a16..8c73e6eb271 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/DFRSimilarity.java@@ -17,6 +17,7 @@package org.apache.lucene.search.similarities;+import java.util.ArrayList;import java.util.List;import org.apache.lucene.search.Explanation;@@ -122,7 +123,7 @@ public class DFRSimilarity extends SimilarityBase {protected void explain(List<Explanation> subs,BasicStats stats, int doc, double freq, double docLen) {if (stats.getBoost() != 1.0d) {- subs.add(Explanation.match( (float)stats.getBoost(), "boost"));+ subs.add(Explanation.match( (float)stats.getBoost(), "boost, query boost"));}Explanation normExpl = normalization.explain(stats, freq, docLen);@@ -133,6 +134,20 @@ public class DFRSimilarity extends SimilarityBase {subs.add(afterEffect.explain(stats, tfn));}+ @Override+ protected Explanation explain(+ BasicStats stats, int doc, Explanation freq, double docLen) {+ List<Explanation> subs = new ArrayList<>();+ explain(subs, stats, doc, freq.getValue(), docLen);++ return Explanation.match(+ (float) score(stats, freq.getValue(), docLen),+ "score(" + getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), computed as boost * " ++ "basicModel.score(stats, tfn) * afterEffect.score(stats, tfn) from:",+ subs);+ }+@Overridepublic String toString() {return "DFR " + basicModel.toString() + afterEffect.toString()diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/IBSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/IBSimilarity.javaindex a71614ce374..3e9ba0c4c32 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/IBSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/IBSimilarity.java@@ -17,6 +17,7 @@package org.apache.lucene.search.similarities;+import java.util.ArrayList;import java.util.List;import org.apache.lucene.search.Explanation;@@ -113,7 +114,7 @@ public class IBSimilarity extends SimilarityBase {protected void explain(List<Explanation> subs, BasicStats stats, int doc, double freq, double docLen) {if (stats.getBoost() != 1.0d) {- subs.add(Explanation.match((float)stats.getBoost(), "boost"));+ subs.add(Explanation.match((float)stats.getBoost(), "boost, query boost"));}Explanation normExpl = normalization.explain(stats, freq, docLen);Explanation lambdaExpl = lambda.explain(stats);@@ -121,6 +122,22 @@ public class IBSimilarity extends SimilarityBase {subs.add(lambdaExpl);subs.add(distribution.explain(stats, normExpl.getValue(), lambdaExpl.getValue()));}++ @Override+ protected Explanation explain(+ BasicStats stats, int doc, Explanation freq, double docLen) {+ List<Explanation> subs = new ArrayList<>();+ explain(subs, stats, doc, freq.getValue(), docLen);++ return Explanation.match(+ (float) score(stats, freq.getValue(), docLen),+ "score(" + getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), computed as boost * " ++ "distribution.score(stats, normalization.tfn(stats, freq," ++ " docLen), lambda.lambda(stats)) from:",+ subs);+ }+/*** The name of IB methods follow the patterndiff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/LMDirichletSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/LMDirichletSimilarity.javaindex 2a4354e2f6c..be7329bec23 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/LMDirichletSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/LMDirichletSimilarity.java@@ -17,6 +17,7 @@package org.apache.lucene.search.similarities;+import java.util.ArrayList;import java.util.List;import java.util.Locale;@@ -86,20 +87,43 @@ public class LMDirichletSimilarity extends LMSimilarity {protected void explain(List<Explanation> subs, BasicStats stats, int doc,double freq, double docLen) {if (stats.getBoost() != 1.0d) {- subs.add(Explanation.match((float) stats.getBoost(), "boost"));+ subs.add(Explanation.match((float) stats.getBoost(), "query boost"));}+ double p = ((LMStats)stats).getCollectionProbability();+ Explanation explP = Explanation.match((float) p,+ "P, probability that the current term is generated by the collection");+ Explanation explFreq = Explanation.match((float) freq,+ "freq, number of occurrences of term in the document");subs.add(Explanation.match(mu, "mu"));Explanation weightExpl = Explanation.match((float)Math.log(1 + freq /(mu * ((LMStats)stats).getCollectionProbability())),- "term weight");+ "term weight, computed as log(1 + freq /(mu * P)) from:",+ explFreq,+ explP);subs.add(weightExpl);subs.add(Explanation.match(- (float)Math.log(mu / (docLen + mu)), "document norm"));+ (float)Math.log(mu / (docLen + mu)),+ "document norm, computed as log(mu / (dl + mu))"));+ subs.add(Explanation.match((float) docLen,"dl, length of field"));super.explain(subs, stats, doc, freq, docLen);}+ @Override+ protected Explanation explain(+ BasicStats stats, int doc, Explanation freq, double docLen) {+ List<Explanation> subs = new ArrayList<>();+ explain(subs, stats, doc, freq.getValue(), docLen);++ return Explanation.match(+ (float) score(stats, freq.getValue(), docLen),+ "score(" + getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), computed as boost * " ++ "(term weight + document norm) from:",+ subs);+ }+/** Returns the μ parameter. */public float getMu() {return mu;diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.java b/lucene/core/src/java/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.javaindex fa0ebcf883c..f6db238d44b 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.java@@ -17,6 +17,7 @@package org.apache.lucene.search.similarities;+import java.util.ArrayList;import java.util.List;import java.util.Locale;@@ -79,9 +80,31 @@ public class LMJelinekMercerSimilarity extends LMSimilarity {subs.add(Explanation.match((float) stats.getBoost(), "boost"));}subs.add(Explanation.match(lambda, "lambda"));+ double p = ((LMStats)stats).getCollectionProbability();+ Explanation explP = Explanation.match((float) p,+ "P, probability that the current term is generated by the collection");+ subs.add(explP);+ Explanation explFreq = Explanation.match((float) freq,+ "freq, number of occurrences of term in the document");+ subs.add(explFreq);+ subs.add(Explanation.match((float) docLen,"dl, length of field"));super.explain(subs, stats, doc, freq, docLen);}+ @Override+ protected Explanation explain(+ BasicStats stats, int doc, Explanation freq, double docLen) {+ List<Explanation> subs = new ArrayList<>();+ explain(subs, stats, doc, freq.getValue(), docLen);++ return Explanation.match(+ (float) score(stats, freq.getValue(), docLen),+ "score(" + getClass().getSimpleName() + ", doc=" + doc + ", freq=" ++ freq.getValue() +"), computed as boost * " ++ "log(1 + ((1 - lambda) * freq / dl) /(lambda * P)) from:",+ subs);+ }+/** Returns the λ parameter. */public float getLambda() {return lambda;diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaDF.java b/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaDF.javaindex ef87c8ae159..7dc320d7083 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaDF.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaDF.java@@ -37,9 +37,12 @@ public class LambdaDF extends Lambda {public final Explanation explain(BasicStats stats) {return Explanation.match(lambda(stats),- getClass().getSimpleName() + ", computed from: ",- Explanation.match(stats.getDocFreq(), "docFreq"),- Explanation.match(stats.getNumberOfDocuments(), "numberOfDocuments"));+ getClass().getSimpleName()+ + ", computed as (n + 1) / (N + 1) from:",+ Explanation.match(stats.getDocFreq(),+ "n, number of documents containing term"),+ Explanation.match(stats.getNumberOfDocuments(),+ "N, total number of documents with field"));}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaTTF.java b/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaTTF.javaindex 20fe652f776..6dc54a3030d 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaTTF.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/LambdaTTF.java@@ -33,15 +33,17 @@ public class LambdaTTF extends Lambda {return (stats.getTotalTermFreq()+1F) / (stats.getNumberOfDocuments()+1F);}- @Overridepublic final Explanation explain(BasicStats stats) {return Explanation.match(lambda(stats),- getClass().getSimpleName() + ", computed from: ",- Explanation.match(stats.getTotalTermFreq(), "totalTermFreq"),- Explanation.match(stats.getNumberOfDocuments(), "numberOfDocuments"));+ getClass().getSimpleName()+ + ", computed as (F + 1) / (N + 1) from:",+ Explanation.match(stats.getTotalTermFreq(),+ "F, total number of occurrences of term across all documents"),+ Explanation.match(stats.getNumberOfDocuments(),+ "N, total number of documents with field"));}-+@Overridepublic String toString() {return "L";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/Normalization.java b/lucene/core/src/java/org/apache/lucene/search/similarities/Normalization.javaindex e20ca020da0..6bde17d34ba 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/Normalization.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/Normalization.java@@ -47,10 +47,12 @@ public abstract class Normalization {public Explanation explain(BasicStats stats, double tf, double len) {return Explanation.match((float) tfn(stats, tf, len),- getClass().getSimpleName() + ", computed from: ",- Explanation.match((float) tf, "tf"),- Explanation.match((float) stats.getAvgFieldLength(), "avgFieldLength"),- Explanation.match((float) len, "len"));+ getClass().getSimpleName() + ", computed from:",+ Explanation.match((float) tf,+ "tf, number of occurrences of term in the document"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgfl, average length of field across all documents"),+ Explanation.match((float) len, "fl, field length of the document"));}/** Implementation used when there is no normalization. */diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH1.java b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH1.javaindex 8e5a28fcaf9..5fa4f1f298b 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH1.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH1.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** Normalization model that assumes a uniform distribution of the term frequency.* <p>While this model is parameterless in the@@ -55,6 +57,21 @@ public class NormalizationH1 extends Normalization {return tf * c * (stats.getAvgFieldLength() / len);}+ @Override+ public Explanation explain(BasicStats stats, double tf, double len) {+ return Explanation.match(+ (float) tfn(stats, tf, len),+ getClass().getSimpleName()+ + ", computed as tf * c * (avgfl / fl) from:",+ Explanation.match((float) tf,+ "tf, number of occurrences of term in the document"),+ Explanation.match(c,+ "c, hyper-parameter"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgfl, average length of field across all documents"),+ Explanation.match((float) len, "fl, field length of the document"));+ }+@Overridepublic String toString() {return "1";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH2.java b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH2.javaindex 24fb74ea2aa..a30bd4b2fb0 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH2.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH2.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+import static org.apache.lucene.search.similarities.SimilarityBase.log2;/**@@ -57,6 +59,21 @@ public class NormalizationH2 extends Normalization {return tf * log2(1 + c * stats.getAvgFieldLength() / len);}+ @Override+ public Explanation explain(BasicStats stats, double tf, double len) {+ return Explanation.match(+ (float) tfn(stats, tf, len),+ getClass().getSimpleName()+ + ", computed as tf * log2(1 + c * avgfl / fl) from:",+ Explanation.match((float) tf,+ "tf, number of occurrences of term in the document"),+ Explanation.match(c,+ "c, hyper-parameter"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgfl, average length of field across all documents"),+ Explanation.match((float) len, "fl, field length of the document"));+ }+@Overridepublic String toString() {return "2";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH3.java b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH3.javaindex 0bbea496b70..64c9ec3f044 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH3.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationH3.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** Dirichlet Priors normalization* @lucene.experimental@@ -47,6 +49,23 @@ public class NormalizationH3 extends Normalization {return (tf + mu * ((stats.getTotalTermFreq()+1F) / (stats.getNumberOfFieldTokens()+1F))) / (len + mu) * mu;}+ @Override+ public Explanation explain(BasicStats stats, double tf, double len) {+ return Explanation.match(+ (float) tfn(stats, tf, len),+ getClass().getSimpleName()+ + ", computed as (tf + mu * ((F+1) / (T+1))) / (fl + mu) * mu from:",+ Explanation.match((float) tf,+ "tf, number of occurrences of term in the document"),+ Explanation.match(mu,+ "mu, smoothing parameter"),+ Explanation.match((float) stats.getTotalTermFreq(),+ "F, total number of occurrences of term across all documents"),+ Explanation.match((float) stats.getNumberOfFieldTokens(),+ "T, total number of tokens of the field across all documents"),+ Explanation.match((float) len, "fl, field length of the document"));+ }+@Overridepublic String toString() {return "3(" + mu + ")";diff --git a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationZ.java b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationZ.javaindex dabf9c906bc..ca7108c53ae 100644--- a/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationZ.java+++ b/lucene/core/src/java/org/apache/lucene/search/similarities/NormalizationZ.java@@ -17,6 +17,8 @@package org.apache.lucene.search.similarities;+import org.apache.lucene.search.Explanation;+/*** Pareto-Zipf Normalization* @lucene.experimental@@ -48,6 +50,20 @@ public class NormalizationZ extends Normalization {return tf * Math.pow(stats.avgFieldLength / len, z);}+ @Override+ public Explanation explain(BasicStats stats, double tf, double len) {+ return Explanation.match(+ (float) tfn(stats, tf, len),+ getClass().getSimpleName()+ + ", computed as tf * Math.pow(avgfl / fl, z) from:",+ Explanation.match((float) tf,+ "tf, number of occurrences of term in the document"),+ Explanation.match((float) stats.getAvgFieldLength(),+ "avgfl, average length of field across all documents"),+ Explanation.match((float) len, "fl, field length of the document"),+ Explanation.match(z, "z, relates to specificity of the language"));+ }+@Overridepublic String toString() {return "Z(" + z + ")";