Skip to content

Commit

Permalink
Support getMaxScore of ConjunctionScorer for non top level scoring cl…
Browse files Browse the repository at this point in the history
…ause (#13043)
  • Loading branch information
mrkm4ntr committed Jan 29, 2024
1 parent 21e5455 commit 1414136
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Expand Up @@ -202,6 +202,8 @@ Improvements

* GITHUB#12999: Use Automaton for SurroundQuery prefix/pattern matching (Michael Gibney)

* GITHUB#13043: Support getMaxScore of ConjunctionScorer for non top level scoring clause. (Shintaro Murakami)

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

Expand Down
Expand Up @@ -65,22 +65,23 @@ public float score() throws IOException {

@Override
public float getMaxScore(int upTo) throws IOException {
// This scorer is only used for TOP_SCORES when there is at most one scoring clause
switch (scorers.length) {
case 0:
return 0;
case 1:
return scorers[0].getMaxScore(upTo);
default:
return Float.POSITIVE_INFINITY;
double maxScore = 0;
for (Scorer s : scorers) {
if (s.docID() <= upTo) {
maxScore += s.getMaxScore(upTo);
}
}
return (float) maxScore;
}

@Override
public int advanceShallow(int target) throws IOException {
if (scorers.length == 1) {
return scorers[0].advanceShallow(target);
}
for (Scorer scorer : scorers) {
scorer.advanceShallow(target);
}
return super.advanceShallow(target);
}

Expand Down
Expand Up @@ -521,4 +521,20 @@ public void testSingleShouldScoringClause() throws Exception {
assertTrue(clause1.topLevelScoringClause);
assertFalse(clause2.topLevelScoringClause);
}

public void testMaxScoreNonTopLevelScoringClause() throws Exception {
Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
for (Occur occur : Occur.values()) {
subs.put(occur, new ArrayList<>());
}

FakeScorerSupplier clause1 = new FakeScorerSupplier(10, 10);
subs.get(Occur.MUST).add(clause1);
FakeScorerSupplier clause2 = new FakeScorerSupplier(10, 10);
subs.get(Occur.MUST).add(clause2);

Scorer 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 1414136

Please sign in to comment.