Skip to content

Commit 2fc795f

Browse files
authored
Add count() to BayesianScoreQuery.BayesianScoreWeight (#16110)
* Add count() to BayesianScoreQuery.BayesianScoreWeight * moved to 10.5 version
1 parent d9aa7cb commit 2fc795f

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

lucene/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ New Features
324324
* GITHUB#15794: Add DocValuesSkipper metadata for the maximum number of values
325325
on any document in a field. (Prithvi S)
326326

327+
* GITHUB#16110: Add count() to BayesianScoreQuery.BayesianScoreWeight, delegating to
328+
the inner weight. (Prithvi S)
329+
327330
Improvements
328331
---------------------
329332
* GITHUB#15948: Improve BayesianScoreQuery and LogOddsFusionQuery with base rate prior,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ public void setTopLevelScoringClause() {
242242
};
243243
}
244244

245+
@Override
246+
public int count(LeafReaderContext context) throws IOException {
247+
return innerWeight.count(context);
248+
}
249+
245250
@Override
246251
public boolean isCacheable(LeafReaderContext ctx) {
247252
return innerWeight.isCacheable(ctx);

lucene/core/src/test/org/apache/lucene/search/TestBayesianScoreQuery.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,36 @@ public void testToString() {
238238
assertTrue("should contain alpha", s.contains("alpha"));
239239
}
240240

241+
public void testCountDelegatesToInner() throws Exception {
242+
Query inner = new TermQuery(new Term("body", "alpha"));
243+
BayesianScoreQuery bsq = new BayesianScoreQuery(inner, 0.5f, 5.0f);
244+
245+
Weight w = searcher.createWeight(searcher.rewrite(bsq), ScoreMode.COMPLETE, 1);
246+
LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
247+
248+
// BayesianScoreQuery only transforms scores; matching docs are unchanged.
249+
// count() should delegate to the inner weight.
250+
int count = w.count(context);
251+
assertEquals(2, count);
252+
253+
// Verify it matches the inner query's count
254+
Weight innerW = searcher.createWeight(inner, ScoreMode.COMPLETE, 1);
255+
assertEquals(innerW.count(context), count);
256+
}
257+
258+
public void testCountWithNoScoring() throws Exception {
259+
// When scoreMode.needsScores() == false, createWeight returns innerWeight directly
260+
Query inner = new TermQuery(new Term("body", "alpha"));
261+
BayesianScoreQuery bsq = new BayesianScoreQuery(inner, 0.5f, 5.0f);
262+
263+
Weight w = searcher.createWeight(searcher.rewrite(bsq), ScoreMode.COMPLETE_NO_SCORES, 1);
264+
LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
265+
266+
// Should delegate to inner weight's count
267+
int count = w.count(context);
268+
assertEquals(2, count);
269+
}
270+
241271
public void testDifferentAlphaBeta() throws Exception {
242272
Query inner = new TermQuery(new Term("body", "alpha"));
243273

0 commit comments

Comments
 (0)