Skip to content

Commit 37fc3b3

Browse files
authored
Fix too many documents collected when only bool-filter condition is present (#14757)
1 parent f2493e5 commit 37fc3b3

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Bug Fixes
5757
returns a string which can be parsed back into the original node.
5858
(Peter Barna, Adam Schwartz)
5959

60+
* GITHUB#14755: Fix too many documents collected when only bool-filter condition is present. (Ke Wei)
61+
6062
Changes in Runtime Behavior
6163
---------------------
6264
* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ public float getMaxScore(int upTo) throws IOException {
439439
required.addAll(requiredScoring);
440440
required.addAll(requiredNoScoring);
441441
conjunctionScorer = new ConjunctionScorer(required, requiredScoring);
442+
if (this.scoreMode == ScoreMode.TOP_SCORES && requiredScoring.size() == 0) {
443+
conjunctionScorer =
444+
conjunctionScorer.twoPhaseIterator() != null
445+
? new ConstantScoreScorer(
446+
0.0F, this.scoreMode, conjunctionScorer.twoPhaseIterator())
447+
: new ConstantScoreScorer(0.0F, this.scoreMode, conjunctionScorer.iterator());
448+
}
442449
}
443450
return new DefaultBulkScorer(conjunctionScorer);
444451
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.apache.lucene.document.Document;
2525
import org.apache.lucene.document.Field;
2626
import org.apache.lucene.document.Field.Store;
27+
import org.apache.lucene.document.FieldType;
28+
import org.apache.lucene.document.LongPoint;
2729
import org.apache.lucene.document.StringField;
30+
import org.apache.lucene.index.IndexOptions;
2831
import org.apache.lucene.index.IndexReader;
2932
import org.apache.lucene.index.LeafReaderContext;
3033
import org.apache.lucene.index.Term;
@@ -402,4 +405,40 @@ public void testFilterConstantScore() throws Exception {
402405
w.close();
403406
dir.close();
404407
}
408+
409+
public void testCollectNoThresholdWhenOnlyFilter() throws Exception {
410+
Directory dir = newDirectory();
411+
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
412+
FieldType fieldType = new FieldType();
413+
fieldType.setIndexOptions(IndexOptions.DOCS);
414+
for (int i = 0; i < 50; i++) {
415+
Document doc = new Document();
416+
doc.add(new Field("foo", "bar" + (i % 2), fieldType));
417+
doc.add(new LongPoint("field", i % 4));
418+
w.addDocument(doc);
419+
}
420+
421+
IndexReader reader = w.getReader();
422+
IndexSearcher searcher = new IndexSearcher(reader);
423+
searcher.setQueryCache(null);
424+
425+
TermQuery termQuery = new TermQuery(new Term("foo", "bar0"));
426+
427+
BooleanQuery.Builder builder = new BooleanQuery.Builder();
428+
builder.add(termQuery, BooleanClause.Occur.FILTER);
429+
430+
Query indexQuery = LongPoint.newRangeQuery("field", 1, Long.MAX_VALUE);
431+
builder.add(indexQuery, BooleanClause.Occur.FILTER);
432+
433+
int totalHitsThreshold = 7;
434+
TopScoreDocCollectorManager topScoreDocCollectorManager =
435+
new TopScoreDocCollectorManager(3, null, totalHitsThreshold);
436+
TopScoreDocCollector collector = topScoreDocCollectorManager.newCollector();
437+
searcher.search(builder.build(), collector);
438+
assertEquals(totalHitsThreshold + 1, collector.totalHits);
439+
440+
reader.close();
441+
w.close();
442+
dir.close();
443+
}
405444
}

0 commit comments

Comments
 (0)