Skip to content

Commit d7a483c

Browse files
authored
Merge CombinedFieldQuery's WeightedDisiWrapper into DisiWrapper. (#14747)
This aims at simplifying things a bit to make it easier to implement `CombinedFieldScorer#nextDocsAndScores` later.
1 parent 3b6408a commit d7a483c

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lucene/core/src/java/org/apache/lucene/search/CombinedFieldQuery.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@ public Scorer get(long leadCost) throws IOException {
378378
List<DisiWrapper> wrappers = new ArrayList<>(iterators.size());
379379
for (int i = 0; i < iterators.size(); i++) {
380380
float weight = fields.get(i).weight;
381-
wrappers.add(
382-
new WeightedDisiWrapper(new TermScorer(iterators.get(i), simWeight, null), weight));
381+
Scorer scorer = new TermScorer(iterators.get(i), simWeight, null);
382+
DisiWrapper w = new DisiWrapper(scorer, false, weight);
383+
assert w.postingsEnum != null; // needed to access term frequencies
384+
wrappers.add(w);
383385
}
384386
// Even though it is called approximation, it is accurate since none of
385387
// the sub iterators are two-phase iterators.
@@ -401,21 +403,6 @@ public boolean isCacheable(LeafReaderContext ctx) {
401403
}
402404
}
403405

404-
private static class WeightedDisiWrapper extends DisiWrapper {
405-
final PostingsEnum postingsEnum;
406-
final float weight;
407-
408-
WeightedDisiWrapper(Scorer scorer, float weight) {
409-
super(scorer, false);
410-
this.weight = weight;
411-
this.postingsEnum = (PostingsEnum) scorer.iterator();
412-
}
413-
414-
float freq() throws IOException {
415-
return weight * postingsEnum.freq();
416-
}
417-
}
418-
419406
private static class CombinedFieldScorer extends Scorer {
420407
private final DisjunctionDISIApproximation iterator;
421408
private final MultiNormsLeafSimScorer simScorer;
@@ -434,9 +421,9 @@ public int docID() {
434421

435422
float freq() throws IOException {
436423
DisiWrapper w = iterator.topList();
437-
float freq = ((WeightedDisiWrapper) w).freq();
424+
float freq = w.postingsEnum.freq() * w.weight;
438425
for (w = w.next; w != null; w = w.next) {
439-
freq += ((WeightedDisiWrapper) w).freq();
426+
freq += w.postingsEnum.freq() * w.weight;
440427
if (freq < 0) { // overflow
441428
return Integer.MAX_VALUE;
442429
}

lucene/core/src/java/org/apache/lucene/search/DisiWrapper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.lucene.search;
1818

1919
import java.util.Objects;
20+
import org.apache.lucene.index.PostingsEnum;
2021

2122
/**
2223
* Wrapper used in {@link DisiPriorityQueue}.
@@ -25,6 +26,8 @@
2526
*/
2627
public class DisiWrapper {
2728
public final DocIdSetIterator iterator;
29+
// Same as `iterator` if `iterator` is an instance of PostingsEnum, null otherwise
30+
final PostingsEnum postingsEnum;
2831
public final Scorer scorer;
2932
public final Scorable scorable;
3033
public final long cost;
@@ -45,14 +48,22 @@ public class DisiWrapper {
4548
// for MaxScoreBulkScorer
4649
float maxWindowScore;
4750

51+
// for CombinedFieldQuery (BM25F)
52+
final float weight;
53+
4854
public DisiWrapper(Scorer scorer, boolean impacts) {
55+
this(scorer, impacts, 1f);
56+
}
57+
58+
DisiWrapper(Scorer scorer, boolean impacts, float weight) {
4959
this.scorer = Objects.requireNonNull(scorer);
5060
this.scorable = ScorerUtil.likelyTermScorer(scorer);
5161
if (impacts) {
5262
this.iterator = ScorerUtil.likelyImpactsEnum(scorer.iterator());
5363
} else {
5464
this.iterator = scorer.iterator();
5565
}
66+
this.postingsEnum = iterator instanceof PostingsEnum pe ? pe : null;
5667
this.cost = iterator.cost();
5768
this.doc = -1;
5869
this.twoPhaseView = scorer.twoPhaseIterator();
@@ -64,5 +75,7 @@ public DisiWrapper(Scorer scorer, boolean impacts) {
6475
approximation = iterator;
6576
matchCost = 0f;
6677
}
78+
79+
this.weight = weight;
6780
}
6881
}

0 commit comments

Comments
 (0)