Skip to content

Apache Lucene open-source search software

Merge CombinedFieldQuery's WeightedDisiWrapper into DisiWrapper. (#14747)

Adrien Grandd7a483c7c8ad
diff --git a/lucene/core/src/java/org/apache/lucene/search/CombinedFieldQuery.java b/lucene/core/src/java/org/apache/lucene/search/CombinedFieldQuery.java
index 42659e3b37e..7cb6cb89f50 100644
--- a/lucene/core/src/java/org/apache/lucene/search/CombinedFieldQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/CombinedFieldQuery.java
@@ -378,8 +378,10 @@ public final class CombinedFieldQuery extends Query implements Accountable {
List<DisiWrapper> wrappers = new ArrayList<>(iterators.size());
for (int i = 0; i < iterators.size(); i++) {
float weight = fields.get(i).weight;
- wrappers.add(
- new WeightedDisiWrapper(new TermScorer(iterators.get(i), simWeight, null), weight));
+ Scorer scorer = new TermScorer(iterators.get(i), simWeight, null);
+ DisiWrapper w = new DisiWrapper(scorer, false, weight);
+ assert w.postingsEnum != null; // needed to access term frequencies
+ wrappers.add(w);
}
// Even though it is called approximation, it is accurate since none of
// the sub iterators are two-phase iterators.
@@ -401,21 +403,6 @@ public final class CombinedFieldQuery extends Query implements Accountable {
}
}
- private static class WeightedDisiWrapper extends DisiWrapper {
- final PostingsEnum postingsEnum;
- final float weight;
-
- WeightedDisiWrapper(Scorer scorer, float weight) {
- super(scorer, false);
- this.weight = weight;
- this.postingsEnum = (PostingsEnum) scorer.iterator();
- }
-
- float freq() throws IOException {
- return weight * postingsEnum.freq();
- }
- }
-
private static class CombinedFieldScorer extends Scorer {
private final DisjunctionDISIApproximation iterator;
private final MultiNormsLeafSimScorer simScorer;
@@ -434,9 +421,9 @@ public final class CombinedFieldQuery extends Query implements Accountable {
float freq() throws IOException {
DisiWrapper w = iterator.topList();
- float freq = ((WeightedDisiWrapper) w).freq();
+ float freq = w.postingsEnum.freq() * w.weight;
for (w = w.next; w != null; w = w.next) {
- freq += ((WeightedDisiWrapper) w).freq();
+ freq += w.postingsEnum.freq() * w.weight;
if (freq < 0) { // overflow
return Integer.MAX_VALUE;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java b/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java
index 5673f16b37b..d3a73556560 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java
@@ -17,6 +17,7 @@
package org.apache.lucene.search;
import java.util.Objects;
+import org.apache.lucene.index.PostingsEnum;
/**
* Wrapper used in {@link DisiPriorityQueue}.
@@ -25,6 +26,8 @@ import java.util.Objects;
*/
public class DisiWrapper {
public final DocIdSetIterator iterator;
+ // Same as `iterator` if `iterator` is an instance of PostingsEnum, null otherwise
+ final PostingsEnum postingsEnum;
public final Scorer scorer;
public final Scorable scorable;
public final long cost;
@@ -45,7 +48,14 @@ public class DisiWrapper {
// for MaxScoreBulkScorer
float maxWindowScore;
+ // for CombinedFieldQuery (BM25F)
+ final float weight;
+
public DisiWrapper(Scorer scorer, boolean impacts) {
+ this(scorer, impacts, 1f);
+ }
+
+ DisiWrapper(Scorer scorer, boolean impacts, float weight) {
this.scorer = Objects.requireNonNull(scorer);
this.scorable = ScorerUtil.likelyTermScorer(scorer);
if (impacts) {
@@ -53,6 +63,7 @@ public class DisiWrapper {
} else {
this.iterator = scorer.iterator();
}
+ this.postingsEnum = iterator instanceof PostingsEnum pe ? pe : null;
this.cost = iterator.cost();
this.doc = -1;
this.twoPhaseView = scorer.twoPhaseIterator();
@@ -64,5 +75,7 @@ public class DisiWrapper {
approximation = iterator;
matchCost = 0f;
}
+
+ this.weight = weight;
}
}