You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2015/10/19 13:10:51 UTC

svn commit: r1709391 - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/queries/ lucene/queries/src/java/org/apache/lucene/queries/payloads/ lucene/queries/src/test/org/apache/lucene/queries/payloads/

Author: romseygeek
Date: Mon Oct 19 11:10:51 2015
New Revision: 1709391

URL: http://svn.apache.org/viewvc?rev=1709391&view=rev
Log:
LUCENE-6844: Add includeSpanScore option to PayloadScoreQuery

Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/lucene/queries/   (props changed)
    lucene/dev/branches/branch_5x/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java
    lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java
    lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java

Modified: lucene/dev/branches/branch_5x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/CHANGES.txt?rev=1709391&r1=1709390&r2=1709391&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/lucene/CHANGES.txt Mon Oct 19 11:10:51 2015
@@ -29,6 +29,9 @@ New Features
 * LUCENE-6838: Added IndexSearcher#getQueryCache and #getQueryCachingPolicy.
   (Adrien Grand)
 
+* LUCENE-6844: PayloadScoreQuery can include or exclude underlying span scores
+  from its score calculations (Bill Bell, Alan Woodward)
+
 API Changes
 
 * LUCENE-6590: Query.setBoost(), Query.getBoost() and Query.clone() are gone.

Modified: lucene/dev/branches/branch_5x/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java?rev=1709391&r1=1709390&r2=1709391&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java Mon Oct 19 11:10:51 2015
@@ -51,15 +51,27 @@ public class PayloadScoreQuery extends S
 
   private final SpanQuery wrappedQuery;
   private final PayloadFunction function;
+  private final boolean includeSpanScore;
 
   /**
    * Creates a new PayloadScoreQuery
    * @param wrappedQuery the query to wrap
    * @param function a PayloadFunction to use to modify the scores
+   * @param includeSpanScore include both span score and payload score in the scoring algorithm
    */
-  public PayloadScoreQuery(SpanQuery wrappedQuery, PayloadFunction function) {
+  public PayloadScoreQuery(SpanQuery wrappedQuery, PayloadFunction function, boolean includeSpanScore) {
     this.wrappedQuery = wrappedQuery;
     this.function = function;
+    this.includeSpanScore = includeSpanScore;
+  }
+
+  /**
+   * Creates a new PayloadScoreQuery that includes the underlying span scores
+   * @param wrappedQuery the query to wrap
+   * @param function a PayloadFunction to use to modify the scores
+   */
+  public PayloadScoreQuery(SpanQuery wrappedQuery, PayloadFunction function) {
+    this(wrappedQuery, function, true);
   }
 
   @Override
@@ -149,12 +161,16 @@ public class PayloadScoreQuery extends S
       if (scorer == null || scorer.advance(doc) != doc)
         return Explanation.noMatch("No match");
 
-      SpanWeight innerWeight = ((PayloadSpanWeight)scorer.getWeight()).innerWeight;
-      Explanation innerExpl = innerWeight.explain(context, doc);
       scorer.freq();  // force freq calculation
       Explanation payloadExpl = scorer.getPayloadExplanation();
 
-      return Explanation.match(scorer.scoreCurrentDoc(), "PayloadSpanQuery, product of:", innerExpl, payloadExpl);
+      if (includeSpanScore) {
+        SpanWeight innerWeight = ((PayloadSpanWeight) scorer.getWeight()).innerWeight;
+        Explanation innerExpl = innerWeight.explain(context, doc);
+        return Explanation.match(scorer.scoreCurrentDoc(), "PayloadSpanQuery, product of:", innerExpl, payloadExpl);
+      }
+
+      return scorer.getPayloadExplanation();
     }
   }
 
@@ -203,7 +219,9 @@ public class PayloadScoreQuery extends S
 
     @Override
     protected float scoreCurrentDoc() throws IOException {
-      return getSpanScore() * getPayloadScore();
+      if (includeSpanScore)
+        return getSpanScore() * getPayloadScore();
+      return getPayloadScore();
     }
 
     @Override

Modified: lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java?rev=1709391&r1=1709390&r2=1709391&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java (original)
+++ lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java Mon Oct 19 11:10:51 2015
@@ -51,7 +51,7 @@ public class TestPayloadExplanations ext
 
   /** macro for payloadscorequery */
   private SpanQuery pt(String s, PayloadFunction fn) {
-    return new PayloadScoreQuery(new SpanTermQuery(new Term(FIELD,s)), fn);
+    return new PayloadScoreQuery(new SpanTermQuery(new Term(FIELD,s)), fn, random().nextBoolean());
   }
   
   /* simple PayloadTermQueries */
@@ -82,8 +82,6 @@ public class TestPayloadExplanations ext
     }
   }
 
-  // TODO: test the payloadnear query too!
-
   /*
     protected static final String[] docFields = {
     "w1 w2 w3 w4 w5",
@@ -95,7 +93,7 @@ public class TestPayloadExplanations ext
 
   public void testAllFunctions(SpanQuery query, int[] expected) throws Exception {
     for (PayloadFunction fn : functions) {
-      qtest(new PayloadScoreQuery(query, fn), expected);
+      qtest(new PayloadScoreQuery(query, fn, random().nextBoolean()), expected);
     }
   }
 

Modified: lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java?rev=1709391&r1=1709390&r2=1709391&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java Mon Oct 19 11:10:51 2015
@@ -35,6 +35,7 @@ import org.apache.lucene.index.Term;
 import org.apache.lucene.search.CollectionStatistics;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.QueryUtils;
 import org.apache.lucene.search.TermStatistics;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.similarities.DefaultSimilarity;
@@ -54,10 +55,14 @@ import org.junit.Test;
 public class TestPayloadScoreQuery extends LuceneTestCase {
 
   private static void checkQuery(SpanQuery query, PayloadFunction function, int[] expectedDocs, float[] expectedScores) throws IOException {
+    checkQuery(query, function, true, expectedDocs, expectedScores);
+  }
+
+  private static void checkQuery(SpanQuery query, PayloadFunction function, boolean includeSpanScore, int[] expectedDocs, float[] expectedScores) throws IOException {
 
     assertTrue("Expected docs and scores arrays must be the same length!", expectedDocs.length == expectedScores.length);
 
-    PayloadScoreQuery psq = new PayloadScoreQuery(query, function);
+    PayloadScoreQuery psq = new PayloadScoreQuery(query, function, includeSpanScore);
     TopDocs hits = searcher.search(psq, expectedDocs.length);
 
     for (int i = 0; i < hits.scoreDocs.length; i++) {
@@ -70,6 +75,8 @@ public class TestPayloadScoreQuery exten
 
     if (hits.scoreDocs.length > expectedDocs.length)
       fail("Unexpected hit in document " + hits.scoreDocs[expectedDocs.length]);
+
+    QueryUtils.check(random(), psq, searcher);
   }
 
   @Test
@@ -132,9 +139,19 @@ public class TestPayloadScoreQuery exten
         }, 0, true)
     }, 1, true);
 
-    checkQuery(q, new MaxPayloadFunction(), new int[]{ 122, 222 }, new float[]{ 4.0f, 4.0f });
-    checkQuery(q, new MinPayloadFunction(), new int[]{ 222, 122 }, new float[]{ 4.0f, 2.0f });
-    checkQuery(q, new AveragePayloadFunction(), new int[] { 222, 122 }, new float[]{ 4.0f, 3.666666f });
+    // check includeSpanScore makes a difference here
+    searcher.setSimilarity(new MultiplyingSimilarity());
+    try {
+      checkQuery(q, new MaxPayloadFunction(), new int[]{ 122, 222 }, new float[]{ 41.802513122558594f, 34.13160705566406f });
+      checkQuery(q, new MinPayloadFunction(), new int[]{ 222, 122 }, new float[]{ 34.13160705566406f, 20.901256561279297f });
+      checkQuery(q, new AveragePayloadFunction(), new int[] { 122, 222 }, new float[]{ 38.3189697265625f, 34.13160705566406f });
+      checkQuery(q, new MaxPayloadFunction(), false, new int[]{122, 222}, new float[]{4.0f, 4.0f});
+      checkQuery(q, new MinPayloadFunction(), false, new int[]{222, 122}, new float[]{4.0f, 2.0f});
+      checkQuery(q, new AveragePayloadFunction(), false, new int[]{222, 122}, new float[]{4.0f, 3.666666f});
+    }
+    finally {
+      searcher.setSimilarity(similarity);
+    }
 
   }
 
@@ -234,22 +251,26 @@ public class TestPayloadScoreQuery exten
     directory = null;
   }
 
-  static class BoostingSimilarity extends DefaultSimilarity {
+  static class MultiplyingSimilarity extends DefaultSimilarity {
 
     @Override
-    public float queryNorm(float sumOfSquaredWeights) {
-      return 1.0f;
+    public float scorePayload(int docId, int start, int end, BytesRef payload) {
+      //we know it is size 4 here, so ignore the offset/length
+      return payload.bytes[payload.offset];
     }
 
+  }
+
+  static class BoostingSimilarity extends MultiplyingSimilarity {
+
     @Override
-    public float coord(int overlap, int maxOverlap) {
+    public float queryNorm(float sumOfSquaredWeights) {
       return 1.0f;
     }
 
     @Override
-    public float scorePayload(int docId, int start, int end, BytesRef payload) {
-      //we know it is size 4 here, so ignore the offset/length
-      return payload.bytes[payload.offset];
+    public float coord(int overlap, int maxOverlap) {
+      return 1.0f;
     }
 
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!