Apache Solr open-source search software
LUCENE-6844: Add includeSpanScore option to PayloadScoreQuery
Alan Woodward0b305f9279a5
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex b4e06d73441..06e0cd0f5da 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -91,6 +91,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.diff --git a/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.javaindex 3ba35030cc6..a10b56fa0aa 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java@@ -51,15 +51,27 @@ public class PayloadScoreQuery extends SpanQuery {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 SpanQuery {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 calculationExplanation 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 SpanQuery {@Overrideprotected float scoreCurrentDoc() throws IOException {- return getSpanScore() * getPayloadScore();+ if (includeSpanScore)+ return getSpanScore() * getPayloadScore();+ return getPayloadScore();}@Overridediff --git a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.javaindex 213c1428856..b8950d9b607 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadExplanations.java@@ -51,7 +51,7 @@ public class TestPayloadExplanations extends BaseExplanationTestCase {/** 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 extends BaseExplanationTestCase {}}- // TODO: test the payloadnear query too!-/*protected static final String[] docFields = {"w1 w2 w3 w4 w5",@@ -95,7 +93,7 @@ public class TestPayloadExplanations extends BaseExplanationTestCase {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);}}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.javaindex c52b3b542d0..2ccd26c4b7b 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/payloads/TestPayloadScoreQuery.java@@ -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.ClassicSimilarity;@@ -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 extends LuceneTestCase {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 extends LuceneTestCase {}, 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 extends LuceneTestCase {directory = null;}- static class BoostingSimilarity extends ClassicSimilarity {+ static class MultiplyingSimilarity extends ClassicSimilarity {@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;}//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!