LUCENE-7209: Fixed explanations of FunctionScoreQuery.

This commit is contained in:
Adrien Grand 2016-04-20 14:48:53 +02:00
parent af92294b0c
commit 858082c4ca
3 changed files with 59 additions and 8 deletions

View File

@ -88,6 +88,8 @@ Bug Fixes
match 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 you

View File

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

View File

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