LUCENE-6844: Add includeSpanScore option to PayloadScoreQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1709386 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alan Woodward 2015-10-19 10:53:37 +00:00
parent c1ec5f5ec7
commit 0b305f9279
4 changed files with 60 additions and 20 deletions

View File

@ -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.

View File

@ -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 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 SpanQuery {
@Override
protected float scoreCurrentDoc() throws IOException {
return getSpanScore() * getPayloadScore();
if (includeSpanScore)
return getSpanScore() * getPayloadScore();
return getPayloadScore();
}
@Override

View File

@ -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);
}
}

View File

@ -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,7 +251,17 @@ public class TestPayloadScoreQuery extends LuceneTestCase {
directory = null;
}
static class BoostingSimilarity extends ClassicSimilarity {
static class MultiplyingSimilarity extends ClassicSimilarity {
@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];
}
}
static class BoostingSimilarity extends MultiplyingSimilarity {
@Override
public float queryNorm(float sumOfSquaredWeights) {
@ -246,12 +273,6 @@ public class TestPayloadScoreQuery extends LuceneTestCase {
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];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!