You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2016/04/20 14:56:40 UTC

[2/2] lucene-solr:branch_6_0: LUCENE-7209: Fixed explanations of FunctionScoreQuery.

LUCENE-7209: Fixed explanations of FunctionScoreQuery.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/cc43eaff
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/cc43eaff
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/cc43eaff

Branch: refs/heads/branch_6_0
Commit: cc43eaff8773a0212d1a14573990e78f5a2d4cfb
Parents: a2195b1
Author: Adrien Grand <jp...@gmail.com>
Authored: Wed Apr 20 14:48:53 2016 +0200
Committer: Adrien Grand <jp...@gmail.com>
Committed: Wed Apr 20 14:54:36 2016 +0200

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  2 +
 .../lucene/queries/function/FunctionQuery.java  | 19 ++++----
 .../queries/TestCustomScoreExplanations.java    | 46 ++++++++++++++++++++
 3 files changed, 59 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cc43eaff/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f066f5d..639c0e1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -10,6 +10,8 @@ Bug Fixes
 * LUCENE-7187: Block join queries' Weight#extractTerms(...) implementations
   should delegate to the wrapped weight. (Martijn van Groningen)
 
+* LUCENE-7209: Fixed explanations of FunctionScoreQuery. (Adrien Grand)
+
 ======================= Lucene 6.0.0 =======================
 
 System Requirements

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cc43eaff/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java
----------------------------------------------------------------------
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java
index 6f9ecd8..85eac26 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 {
 
     @Override
     public float getValueForNormalization() throws IOException {
-      return queryNorm * queryNorm;
+      return queryWeight * queryWeight;
     }
 
     @Override
     public void normalize(float norm, float boost) {
-      this.queryNorm = norm * boost;
+      this.queryNorm = norm;
+      this.boost = boost;
+      this.queryWeight = norm * boost;
     }
 
     @Override
     public Scorer scorer(LeafReaderContext context) throws IOException {
-      return new AllScorer(context, this, queryNorm);
+      return new AllScorer(context, this, queryWeight);
     }
 
     @Override
     public 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"));
     }
 
   }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/cc43eaff/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java
----------------------------------------------------------------------
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java b/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java
index fc64998..9ab90a4 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);
+  }
 }
+