Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ private int advanceTarget(int target) throws IOException {
}
}

/** If current doc is not competitive, move to a competitive one. */
void ensureCompetitive() throws IOException {
int doc = docID();
int advanceTarget = advanceTarget(doc);
if (advanceTarget != doc) {
in.advance(advanceTarget);
}
}

@Override
public int advance(int target) throws IOException {
return in.advance(advanceTarget(target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public BulkScorer bulkScorer() throws IOException {
return ConstantScoreScorerSupplier.fromIterator(iterator, 0f, scoreMode, maxDoc)
.bulkScorer();
}
return super.bulkScorer();
return new DefaultBulkScorer(get(Long.MAX_VALUE), scoreMode);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions lucene/core/src/java/org/apache/lucene/search/TermScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public void nextDocsAndScores(int upTo, Bits liveDocs, DocAndScoreBuffer buffer)
docAndFreqBuffer = new DocAndFreqBuffer();
}

if (impactsDisi != null) {
impactsDisi.ensureCompetitive();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should rather put it at the beginning of the below for loop. For instance, imagine that the first block of docs returned only has deleted docs, then it will fetch a new block. It would be good to check if this block is competitive before fetching this new block as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice catch!


for (; ; ) {
postingsEnum.nextPostings(upTo, docAndFreqBuffer);
if (liveDocs != null && docAndFreqBuffer.size != 0) {
Expand Down
47 changes: 46 additions & 1 deletion lucene/core/src/java/org/apache/lucene/search/Weight.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,25 @@ protected static class DefaultBulkScorer extends BulkScorer {
private final Scorer scorer;
private final DocIdSetIterator iterator;
private final TwoPhaseIterator twoPhase;
private final ScoreMode scoreMode;
private DocAndScoreBuffer buffer;
private SimpleScorable scorable;

/** Sole constructor. */
public DefaultBulkScorer(Scorer scorer) {
this(scorer, null);
}

/** Sole constructor. */
public DefaultBulkScorer(Scorer scorer, ScoreMode scoreMode) {
this.scorer = Objects.requireNonNull(scorer);
this.twoPhase = scorer.twoPhaseIterator();
if (twoPhase == null) {
this.iterator = scorer.iterator();
} else {
this.iterator = twoPhase.approximation();
}
this.scoreMode = scoreMode;
}

@Override
Expand All @@ -251,9 +260,14 @@ public long cost() {
@Override
public int score(LeafCollector collector, Bits acceptDocs, int min, int max)
throws IOException {
collector.setScorer(scorer);
DocIdSetIterator competitiveIterator = collector.competitiveIterator();

if (scoreMode != null && scoreMode.needsScores() && competitiveIterator == null) {
return batchScore(collector, acceptDocs, min, max);
}

collector.setScorer(scorer);

if (competitiveIterator != null) {
if (competitiveIterator.docID() > min) {
min = competitiveIterator.docID();
Expand Down Expand Up @@ -290,6 +304,37 @@ public int score(LeafCollector collector, Bits acceptDocs, int min, int max)
return iterator.docID();
}

private int batchScore(LeafCollector collector, Bits acceptDocs, int min, int max)
throws IOException {
if (buffer == null) {
buffer = new DocAndScoreBuffer();
}
if (scorable == null) {
scorable = new SimpleScorable();
}

collector.setScorer(scorable);
scorer.setMinCompetitiveScore(scorable.minCompetitiveScore);

if (scorer.docID() < min) {
scorer.iterator().advance(min);
}

for (scorer.nextDocsAndScores(max, acceptDocs, buffer);
buffer.size > 0;
scorer.nextDocsAndScores(max, acceptDocs, buffer)) {
for (int i = 0, size = buffer.size; i < size; i++) {
float score = scorable.score = buffer.scores[i];
if (score >= scorable.minCompetitiveScore) {
collector.collect(buffer.docs[i]);
}
}
scorer.setMinCompetitiveScore(scorable.minCompetitiveScore);
}

return scorer.docID();
}

private static void scoreIterator(
LeafCollector collector, Bits acceptDocs, DocIdSetIterator iterator, int max)
throws IOException {
Expand Down