Apache Lucene open-source search software
LUCENE-8357: Fix function score explanations
Alan Woodward6f24be9457e4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex ca8fa582b03..8b18dd5b112 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -291,6 +291,9 @@ Bug Fixesposition increment when the preservePositionIncrement setting is false.(David Smiley, Jim Ferenczi)+* LUCENE-8357: FunctionScoreQuery.boostByQuery() and boostByValue() were+ producing truncated Explanations (Markus Jelsma, Alan Woodward)+Other* LUCENE-8301: Update randomizedtesting to 2.6.0. (Dawid Weiss)diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.javaindex de0e6d409f7..38b97329e73 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionScoreQuery.java@@ -270,6 +270,19 @@ public final class FunctionScoreQuery extends Query {return Objects.equals(boost, that.boost);}+ @Override+ public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {+ if (scoreExplanation.isMatch() == false) {+ return scoreExplanation;+ }+ Explanation boostExpl = boost.explain(ctx, docId, scoreExplanation);+ if (boostExpl.isMatch() == false) {+ return scoreExplanation;+ }+ return Explanation.match(scoreExplanation.getValue().doubleValue() * boostExpl.getValue().doubleValue(),+ "product of:", scoreExplanation, boostExpl);+ }+@Overridepublic int hashCode() {return Objects.hash(boost);@@ -345,5 +358,14 @@ public final class FunctionScoreQuery extends Query {public boolean isCacheable(LeafReaderContext ctx) {return query.isCacheable(ctx);}++ @Override+ public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {+ Explanation inner = query.explain(ctx, docId, scoreExplanation);+ if (inner.isMatch() == false) {+ return inner;+ }+ return Explanation.match(boost, "Matched boosting query " + query.toString());+ }}}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.javaindex b865cb71238..d97d5adbd80 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestFunctionScoreQuery.java@@ -134,9 +134,12 @@ public class TestFunctionScoreQuery extends FunctionTestSetup {assertEquals(plain.totalHits, docs.totalHits);for (int i = 0; i < expectedDocs.length; i++) {assertEquals(expectedDocs[i], docs.scoreDocs[i].doc);-}+ Explanation expl = searcher.explain(fq, 4);+ assertTrue(expl.toString().contains("first"));+ assertTrue(expl.toString().contains("iii"));+}// BoostingQuery equivalent@@ -155,8 +158,11 @@ public class TestFunctionScoreQuery extends FunctionTestSetup {assertEquals(plain.totalHits, docs.totalHits);for (int i = 0; i < expectedDocs.length; i++) {assertEquals(expectedDocs[i], docs.scoreDocs[i].doc);-}++ Explanation expl = searcher.explain(fq, 6);+ assertTrue(expl.toString().contains("rechecking"));+ assertTrue(expl.toString().contains("text"));}// check boosts with non-distributive score source