Skip to content

Apache Lucene open-source search software

Add count() to BayesianScoreQuery.BayesianScoreWeight (#16110)

Prithvi S2fc795f6587a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 73b20decc3a..f2340a43df5 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -324,6 +324,9 @@ New Features
* GITHUB#15794: Add DocValuesSkipper metadata for the maximum number of values
on any document in a field. (Prithvi S)
+* GITHUB#16110: Add count() to BayesianScoreQuery.BayesianScoreWeight, delegating to
+ the inner weight. (Prithvi S)
+
Improvements
---------------------
* GITHUB#15948: Improve BayesianScoreQuery and LogOddsFusionQuery with base rate prior,
diff --git a/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java b/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
index 87ce0350ff9..56593a9918e 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
@@ -242,6 +242,11 @@ public final class BayesianScoreQuery extends Query {
};
}
+ @Override
+ public int count(LeafReaderContext context) throws IOException {
+ return innerWeight.count(context);
+ }
+
@Override
public boolean isCacheable(LeafReaderContext ctx) {
return innerWeight.isCacheable(ctx);
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBayesianScoreQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBayesianScoreQuery.java
index 3e7494ca2ad..67cd313546c 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBayesianScoreQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBayesianScoreQuery.java
@@ -238,6 +238,36 @@ public class TestBayesianScoreQuery extends LuceneTestCase {
assertTrue("should contain alpha", s.contains("alpha"));
}
+ public void testCountDelegatesToInner() throws Exception {
+ Query inner = new TermQuery(new Term("body", "alpha"));
+ BayesianScoreQuery bsq = new BayesianScoreQuery(inner, 0.5f, 5.0f);
+
+ Weight w = searcher.createWeight(searcher.rewrite(bsq), ScoreMode.COMPLETE, 1);
+ LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
+
+ // BayesianScoreQuery only transforms scores; matching docs are unchanged.
+ // count() should delegate to the inner weight.
+ int count = w.count(context);
+ assertEquals(2, count);
+
+ // Verify it matches the inner query's count
+ Weight innerW = searcher.createWeight(inner, ScoreMode.COMPLETE, 1);
+ assertEquals(innerW.count(context), count);
+ }
+
+ public void testCountWithNoScoring() throws Exception {
+ // When scoreMode.needsScores() == false, createWeight returns innerWeight directly
+ Query inner = new TermQuery(new Term("body", "alpha"));
+ BayesianScoreQuery bsq = new BayesianScoreQuery(inner, 0.5f, 5.0f);
+
+ Weight w = searcher.createWeight(searcher.rewrite(bsq), ScoreMode.COMPLETE_NO_SCORES, 1);
+ LeafReaderContext context = searcher.getIndexReader().leaves().get(0);
+
+ // Should delegate to inner weight's count
+ int count = w.count(context);
+ assertEquals(2, count);
+ }
+
public void testDifferentAlphaBeta() throws Exception {
Query inner = new TermQuery(new Term("body", "alpha"));