Skip to content

Apache Lucene open-source search software

LUCENE-6042: fix CustomScoreQuery explain to properly factor boost

Robert Muir35375d57b9b1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1016e1e9ff1..9a7e4e7585f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -204,6 +204,9 @@ Bug Fixes
* LUCENE-6041: Remove sugar methods FieldInfo.isIndexed and
FieldInfo.hasDocValues. (Robert Muir, Mike McCandless)
+* LUCENE-6042: CustomScoreQuery explain was incorrect in some cases,
+ such as when nested inside a boolean query. (Denis Lantsman via Robert Muir)
+
Documentation
* LUCENE-5392: Add/improve analysis package documentation to reflect
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java b/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java
index 37c9e650857..22b2815d269 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestComplexExplanations.java
@@ -27,7 +27,7 @@ import org.apache.lucene.search.spans.*;
* on the assumption that if the explanations work out right for them,
* they should work for anything.
*/
-public class TestComplexExplanations extends TestExplanations {
+public class TestComplexExplanations extends BaseExplanationTestCase {
/**
* Override the Similarity used in our searcher with one that plays
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java
index e863da59c43..afbabc2d242 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanations.java
@@ -22,7 +22,7 @@ import org.apache.lucene.index.Term;
/**
* TestExplanations subclass focusing on basic query types
*/
-public class TestSimpleExplanations extends TestExplanations {
+public class TestSimpleExplanations extends BaseExplanationTestCase {
// we focus on queries that don't rewrite to other queries.
// if we get those covered well, then the ones that rewrite should
diff --git a/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java b/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java
index e98e3347617..91a62de22c9 100644
--- a/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java
+++ b/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java
@@ -20,14 +20,14 @@ package org.apache.lucene.search.payloads;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity;
-import org.apache.lucene.search.TestExplanations;
+import org.apache.lucene.search.BaseExplanationTestCase;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.util.BytesRef;
/**
* TestExplanations subclass focusing on payload queries
*/
-public class TestPayloadExplanations extends TestExplanations {
+public class TestPayloadExplanations extends BaseExplanationTestCase {
private PayloadFunction functions[] = new PayloadFunction[] {
new AveragePayloadFunction(),
new MinPayloadFunction(),
diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/TestSpanExplanations.java b/lucene/core/src/test/org/apache/lucene/search/spans/TestSpanExplanations.java
index e752f1d7f73..aab5a9ec1a9 100644
--- a/lucene/core/src/test/org/apache/lucene/search/spans/TestSpanExplanations.java
+++ b/lucene/core/src/test/org/apache/lucene/search/spans/TestSpanExplanations.java
@@ -23,7 +23,7 @@ import org.apache.lucene.search.*;
/**
* TestExplanations subclass focusing on span queries
*/
-public class TestSpanExplanations extends TestExplanations {
+public class TestSpanExplanations extends BaseExplanationTestCase {
/* simple SpanTermQueries */
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java
index 6e6727a3a45..e48e8f24a56 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java
@@ -263,11 +263,11 @@ public class CustomScoreQuery extends Query {
valSrcExpls[i] = valSrcWeights[i].explain(info, doc);
}
Explanation customExp = CustomScoreQuery.this.getCustomScoreProvider(info).customExplain(doc,subQueryExpl,valSrcExpls);
- float sc = getBoost() * customExp.getValue();
+ float sc = queryWeight * customExp.getValue();
Explanation res = new ComplexExplanation(
true, sc, CustomScoreQuery.this.toString() + ", product of:");
res.addDetail(customExp);
- res.addDetail(new Explanation(getBoost(), "queryBoost")); // actually using the q boost as q weight (== weight value)
+ res.addDetail(new Explanation(queryWeight, "queryWeight"));
return res;
}
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java b/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java
new file mode 100644
index 00000000000..537f9f21aad
--- /dev/null
+++ b/lucene/queries/src/test/org/apache/lucene/queries/TestCustomScoreExplanations.java
@@ -0,0 +1,53 @@
+package org.apache.lucene.queries;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queries.function.FunctionQuery;
+import org.apache.lucene.queries.function.valuesource.ConstValueSource;
+import org.apache.lucene.search.BaseExplanationTestCase;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+
+public class TestCustomScoreExplanations extends BaseExplanationTestCase {
+ public void testOneTerm() throws Exception {
+ Query q = new TermQuery(new Term(FIELD, "w1"));
+ CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
+ qtest(csq, new int[] { 0,1,2,3 });
+ }
+
+ public void testBoost() throws Exception {
+ Query q = new TermQuery(new Term(FIELD, "w1"));
+ CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
+ csq.setBoost(4);
+ qtest(csq, new int[] { 0,1,2,3 });
+ }
+
+ public void testTopLevelBoost() throws Exception {
+ Query q = new TermQuery(new Term(FIELD, "w1"));
+ CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
+ BooleanQuery bq = new BooleanQuery();
+ bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
+ bq.add(csq, BooleanClause.Occur.MUST);
+ bq.setBoost(6);
+ qtest(bq, new int[] { 0,1,2,3 });
+ }
+}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestExplanations.java b/lucene/test-framework/src/java/org/apache/lucene/search/BaseExplanationTestCase.java
similarity index 97%
rename from lucene/core/src/test/org/apache/lucene/search/TestExplanations.java
rename to lucene/test-framework/src/java/org/apache/lucene/search/BaseExplanationTestCase.java
index faf124067b6..525d846bfd9 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestExplanations.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/search/BaseExplanationTestCase.java
@@ -48,7 +48,7 @@ import org.junit.BeforeClass;
*
* @see "Subclasses for actual tests"
*/
-public class TestExplanations extends LuceneTestCase {
+public abstract class BaseExplanationTestCase extends LuceneTestCase {
protected static IndexSearcher searcher;
protected static IndexReader reader;
protected static Directory directory;
@@ -211,12 +211,4 @@ public class TestExplanations extends LuceneTestCase {
bq.add(new TermQuery(new Term(FIELD,"w1")), BooleanClause.Occur.SHOULD);
return bq;
}
-
- /**
- * Placeholder: JUnit freaks if you don't have one test ... making
- * class abstract doesn't help
- */
- public void testNoop() {
- /* NOOP */
- }
}