Apache Lucene open-source search software
LUCENE-6590: Fix BooleanQuery to not propagate query boosts twice.
Adrien Grande21c1994b709
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.javaindex 813a8804302..44dc2dbeaf3 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java@@ -43,7 +43,6 @@ final class BooleanWeight extends Weight {final boolean disableCoord;final boolean needsScores;final float coords[];- float boost = 1f;BooleanWeight(BooleanQuery query, IndexSearcher searcher, boolean needsScores, boolean disableCoord) throws IOException {super(query);@@ -106,8 +105,6 @@ final class BooleanWeight extends Weight {i += 1;}- sum *= boost * boost; // boost each sub-weight-return sum ;}@@ -129,7 +126,6 @@ final class BooleanWeight extends Weight {@Overridepublic void normalize(float norm, float boost) {- this.boost = boost;for (Weight w : weights) {// normalize all clauses, (even if non-scoring in case of side affects)w.normalize(norm, boost);diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.javaindex eeba6c311ab..5ee288d6d98 100644--- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java@@ -118,7 +118,6 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>/** The Weights for our subqueries, in 1-1 correspondence with disjuncts */protected final ArrayList<Weight> weights = new ArrayList<>(); // The Weight's for our subqueries, in 1-1 correspondence with disjunctsprivate final boolean needsScores;- private float boost;/** Construct the Weight for this Query searched by searcher. Recursively construct subquery weights. */public DisjunctionMaxWeight(IndexSearcher searcher, boolean needsScores) throws IOException {@@ -127,7 +126,6 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>weights.add(searcher.createWeight(disjunctQuery, needsScores));}this.needsScores = needsScores;- this.boost = 1f;}@Override@@ -147,13 +145,12 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>max = Math.max(max, sub);}- return (((sum - max) * tieBreakerMultiplier * tieBreakerMultiplier) + max) * boost * boost;+ return (((sum - max) * tieBreakerMultiplier * tieBreakerMultiplier) + max);}/** Apply the computed normalization factor to our subqueries */@Overridepublic void normalize(float norm, float boost) {- this.boost = boost;for (Weight wt : weights) {wt.normalize(norm, boost);}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleSearchEquivalence.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleSearchEquivalence.javaindex ddf83371d64..1321900a3ed 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleSearchEquivalence.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleSearchEquivalence.java@@ -214,4 +214,20 @@ public class TestSimpleSearchEquivalence extends SearchEquivalenceTestBase {assertSameScores(q1, q2);}++ public void testBooleanBoostPropagation() throws Exception {+ float boost1 = random().nextFloat();+ Query tq = new BoostQuery(new TermQuery(randomTerm()), boost1);++ float boost2 = random().nextFloat();+ // Applying boost2 over the term or boolean query should have the same effect+ Query q1 = new BoostQuery(tq, boost2);+ Query q2 = new BooleanQuery.Builder()+ .add(tq, Occur.MUST)+ .add(tq, Occur.FILTER)+ .build();+ q2 = new BoostQuery(q2, boost2);++ assertSameScores(q1, q2);+ }}