Support getMaxScore of DisjunctionSumScorer for non top level scoring clause (#13066)

This commit is contained in:
Shintaro Murakami 2024-03-20 18:37:48 +09:00 committed by GitHub
parent 7a08eeab47
commit c78533c53c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 3 deletions

View File

@ -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
---------------------

View File

@ -18,10 +18,13 @@ package org.apache.lucene.search;
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>.
*
@ -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
@ -43,10 +47,25 @@ final class DisjunctionSumScorer extends DisjunctionScorer {
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());
}
}

View File

@ -536,5 +536,16 @@ public class TestBoolean2ScorerSupplier extends LuceneTestCase {
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);
}
}