Skip to content

Commit 12c3041

Browse files
authored
Speed up TermQuery (#14709)
// nightly-benchmarks-results-changed //
1 parent b5e79a3 commit 12c3041

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Optimizations
122122
* GITHUB#14701: Optimize top-n bulk scorers by evaluating scoring windows in a
123123
term-at-a-time fashion instead of doc-at-a-time. (Adrien Grand)
124124

125+
* GITHUB#14709: Speed up TermQuery by Scorer#nextDocsAndScores. (Guo Feng)
126+
125127
Bug Fixes
126128
---------------------
127129
* GITHUB#14654: ValueSource.fromDoubleValuesSource(dvs).getSortField() would throw errors when
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.lucene.search;
18+
19+
import java.io.IOException;
20+
import org.apache.lucene.util.Bits;
21+
22+
/**
23+
* A bulk scorer used when {@link ScoreMode#needsScores()} is true and {@link
24+
* Scorer#nextDocsAndScores} has optimizations to run faster than one-by-one iteration.
25+
*/
26+
class BatchScoreBulkScorer extends BulkScorer {
27+
28+
private final SimpleScorable scorable = new SimpleScorable();
29+
private final DocAndScoreBuffer buffer = new DocAndScoreBuffer();
30+
private final Scorer scorer;
31+
32+
BatchScoreBulkScorer(Scorer scorer) {
33+
this.scorer = scorer;
34+
}
35+
36+
@Override
37+
public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException {
38+
if (collector.competitiveIterator() != null) {
39+
return new Weight.DefaultBulkScorer(scorer).score(collector, acceptDocs, min, max);
40+
}
41+
42+
collector.setScorer(scorable);
43+
scorer.setMinCompetitiveScore(scorable.minCompetitiveScore);
44+
45+
if (scorer.docID() < min) {
46+
scorer.iterator().advance(min);
47+
}
48+
49+
for (scorer.nextDocsAndScores(max, acceptDocs, buffer);
50+
buffer.size > 0;
51+
scorer.nextDocsAndScores(max, acceptDocs, buffer)) {
52+
for (int i = 0, size = buffer.size; i < size; i++) {
53+
float score = scorable.score = buffer.scores[i];
54+
if (score >= scorable.minCompetitiveScore) {
55+
collector.collect(buffer.docs[i]);
56+
}
57+
}
58+
scorer.setMinCompetitiveScore(scorable.minCompetitiveScore);
59+
}
60+
61+
return scorer.docID();
62+
}
63+
64+
@Override
65+
public long cost() {
66+
return scorer.iterator().cost();
67+
}
68+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ private int advanceTarget(int target) throws IOException {
9898
}
9999
}
100100

101+
/** If current doc is not competitive, move to a competitive one. */
102+
void ensureCompetitive() throws IOException {
103+
int doc = docID();
104+
int advanceTarget = advanceTarget(doc);
105+
if (advanceTarget != doc) {
106+
in.advance(advanceTarget);
107+
}
108+
}
109+
101110
@Override
102111
public int advance(int target) throws IOException {
103112
return in.advance(advanceTarget(target));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public BulkScorer bulkScorer() throws IOException {
173173
return ConstantScoreScorerSupplier.fromIterator(iterator, 0f, scoreMode, maxDoc)
174174
.bulkScorer();
175175
}
176-
return super.bulkScorer();
176+
return new BatchScoreBulkScorer(get(Long.MAX_VALUE));
177177
}
178178

179179
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndScoreBuffer buffer)
135135
}
136136

137137
for (; ; ) {
138+
if (impactsDisi != null) {
139+
impactsDisi.ensureCompetitive();
140+
}
141+
138142
postingsEnum.nextPostings(upTo, docAndFreqBuffer);
139143
if (liveDocs != null && docAndFreqBuffer.size != 0) {
140144
// An empty return value indicates that there are no more docs before upTo. We may be

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void testOptimizeTopLevelClauseOrNull() throws IOException {
201201
weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE, 1);
202202
ss = weight.scorerSupplier(ctx);
203203
scorer = ((BooleanScorerSupplier) ss).booleanScorer();
204-
assertThat(scorer, instanceOf(DefaultBulkScorer.class)); // term scorer
204+
assertThat(scorer, instanceOf(BatchScoreBulkScorer.class)); // term scorer
205205

206206
w.close();
207207
reader.close();

0 commit comments

Comments
 (0)