Skip to content

Commit

Permalink
Support getMaxScore of DisjunctionSumScorer for non top level scoring…
Browse files Browse the repository at this point in the history
… clause (#13066)
  • Loading branch information
mrkm4ntr authored Mar 20, 2024
1 parent 7a08eea commit c78533c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Improvements

* GITHUB#13156: Hunspell: don't proceed with other suggestions if we found good REP ones (Peter Gromov)

* GITHUB#13066: Support getMaxScore of DisjunctionSumScorer for non top level scoring clause (Shintaro Murakami)

Optimizations
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import java.io.IOException;
import java.util.List;
import org.apache.lucene.util.MathUtil;

/** A Scorer for OR like queries, counterpart of <code>ConjunctionScorer</code>. */
final class DisjunctionSumScorer extends DisjunctionScorer {

private final List<Scorer> scorers;

/**
* Construct a <code>DisjunctionScorer</code>.
*
Expand All @@ -31,6 +34,7 @@ final class DisjunctionSumScorer extends DisjunctionScorer {
DisjunctionSumScorer(Weight weight, List<Scorer> subScorers, ScoreMode scoreMode)
throws IOException {
super(weight, subScorers, scoreMode);
this.scorers = subScorers;
}

@Override
Expand All @@ -43,10 +47,25 @@ protected float score(DisiWrapper topList) throws IOException {
return (float) score;
}

@Override
public int advanceShallow(int target) throws IOException {
int min = DocIdSetIterator.NO_MORE_DOCS;
for (Scorer scorer : scorers) {
if (scorer.docID() <= target) {
min = Math.min(min, scorer.advanceShallow(target));
}
}
return min;
}

@Override
public float getMaxScore(int upTo) throws IOException {
// It's ok to return a bad upper bound here since we use WANDScorer when
// we actually care about block scores.
return Float.MAX_VALUE;
double maxScore = 0;
for (Scorer scorer : scorers) {
if (scorer.docID() <= upTo) {
maxScore += scorer.getMaxScore(upTo);
}
}
return (float) MathUtil.sumUpperBound(maxScore, scorers.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -536,5 +536,16 @@ public void testMaxScoreNonTopLevelScoringClause() throws Exception {
Scorer scorer =
new Boolean2ScorerSupplier(new FakeWeight(), subs, ScoreMode.TOP_SCORES, 0).get(10);
assertEquals(2.0, scorer.getMaxScore(DocIdSetIterator.NO_MORE_DOCS), 0.0);

subs = new EnumMap<>(Occur.class);
for (Occur occur : Occur.values()) {
subs.put(occur, new ArrayList<>());
}

subs.get(Occur.SHOULD).add(clause1);
subs.get(Occur.SHOULD).add(clause2);

scorer = new Boolean2ScorerSupplier(new FakeWeight(), subs, ScoreMode.TOP_SCORES, 0).get(10);
assertEquals(2.0, scorer.getMaxScore(DocIdSetIterator.NO_MORE_DOCS), 0.0);
}
}

0 comments on commit c78533c

Please sign in to comment.