Apache Lucene open-source search software
LUCENE-7209: Fixed explanations of FunctionScoreQuery.
Adrien Grand858082c4cae5
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a0814d0e055..7e58dabdcc1 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -88,6 +88,8 @@ Bug Fixesmatch the underlying queries' (lower|upper)Term optionality logic.(Kaneshanathan Srivisagan, Christine Poerschke)+* LUCENE-7209: Fixed explanations of FunctionScoreQuery. (Adrien Grand)+Documentation* LUCENE-7223: Improve XXXPoint javadocs to make it clear that youdiff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.javaindex 6f9ecd8dd39..85eac268dfd 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java@@ -55,7 +55,7 @@ public class FunctionQuery extends Query {protected class FunctionWeight extends Weight {protected final IndexSearcher searcher;- protected float queryNorm = 1f;+ protected float queryNorm, boost, queryWeight;protected final Map context;public FunctionWeight(IndexSearcher searcher) throws IOException {@@ -63,6 +63,7 @@ public class FunctionQuery extends Query {this.searcher = searcher;this.context = ValueSource.newContext(searcher);func.createWeight(context, searcher);+ normalize(1f, 1f);;}@Override@@ -70,22 +71,24 @@ public class FunctionQuery extends Query {@Overridepublic float getValueForNormalization() throws IOException {- return queryNorm * queryNorm;+ return queryWeight * queryWeight;}@Overridepublic void normalize(float norm, float boost) {- this.queryNorm = norm * boost;+ this.queryNorm = norm;+ this.boost = boost;+ this.queryWeight = norm * boost;}@Overridepublic Scorer scorer(LeafReaderContext context) throws IOException {- return new AllScorer(context, this, queryNorm);+ return new AllScorer(context, this, queryWeight);}@Overridepublic Explanation explain(LeafReaderContext context, int doc) throws IOException {- return ((AllScorer)scorer(context)).explain(doc, queryNorm);+ return ((AllScorer)scorer(context)).explain(doc);}}@@ -132,13 +135,13 @@ public class FunctionQuery extends Query {return 1;}- public Explanation explain(int doc, float queryNorm) throws IOException {+ public Explanation explain(int doc) throws IOException {float sc = qWeight * vals.floatVal(doc);return Explanation.match(sc, "FunctionQuery(" + func + "), product of:",vals.explain(doc),- Explanation.match(queryNorm, "boost"),- Explanation.match(weight.queryNorm = 1f, "queryNorm"));+ Explanation.match(weight.boost, "boost"),+ Explanation.match(weight.queryNorm, "queryNorm"));}}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java b/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.javaindex fc649984523..9ab90a4a0b7 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java@@ -16,6 +16,8 @@*/package org.apache.lucene.queries;+import java.io.IOException;+import org.apache.lucene.index.Term;import org.apache.lucene.queries.function.FunctionQuery;import org.apache.lucene.queries.function.valuesource.ConstValueSource;@@ -23,9 +25,14 @@ import org.apache.lucene.search.BaseExplanationTestCase;import org.apache.lucene.search.BooleanClause;import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.BoostQuery;+import org.apache.lucene.search.Explanation;+import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.Query;import org.apache.lucene.search.TermQuery;+import org.apache.lucene.search.similarities.BM25Similarity;+import org.apache.lucene.search.similarities.ClassicSimilarity;+import org.apache.lucene.search.similarities.Similarity;public class TestCustomScoreExplanations extends BaseExplanationTestCase {public void testOneTerm() throws Exception {@@ -49,4 +56,43 @@ public class TestCustomScoreExplanations extends BaseExplanationTestCase {BooleanQuery bq = bqB.build();qtest(new BoostQuery(bq, 6), new int[] { 0,1,2,3 });}++ public void testSubExplanations() throws IOException {+ Query query = new FunctionQuery(new ConstValueSource(5));+ IndexSearcher searcher = newSearcher(BaseExplanationTestCase.searcher.getIndexReader());+ searcher.setSimilarity(new BM25Similarity());++ Explanation expl = searcher.explain(query, 0);+ // function+ assertEquals(5f, expl.getDetails()[0].getValue(), 0f);+ // boost+ assertEquals("boost", expl.getDetails()[1].getDescription());+ assertEquals(1f, expl.getDetails()[1].getValue(), 0f);+ // norm+ assertEquals("queryNorm", expl.getDetails()[2].getDescription());+ assertEquals(1f, expl.getDetails()[2].getValue(), 0f);++ query = new BoostQuery(query, 2);+ expl = searcher.explain(query, 0);+ // function+ assertEquals(5f, expl.getDetails()[0].getValue(), 0f);+ // boost+ assertEquals("boost", expl.getDetails()[1].getDescription());+ assertEquals(2f, expl.getDetails()[1].getValue(), 0f);+ // norm+ assertEquals("queryNorm", expl.getDetails()[2].getDescription());+ assertEquals(1f, expl.getDetails()[2].getValue(), 0f);++ searcher.setSimilarity(new ClassicSimilarity()); // in order to have a queryNorm != 1+ expl = searcher.explain(query, 0);+ // function+ assertEquals(5f, expl.getDetails()[0].getValue(), 0f);+ // boost+ assertEquals("boost", expl.getDetails()[1].getDescription());+ assertEquals(2f, expl.getDetails()[1].getValue(), 0f);+ // norm+ assertEquals("queryNorm", expl.getDetails()[2].getDescription());+ assertEquals(0.5f, expl.getDetails()[2].getValue(), 0f);+ }}+