Skip to content

Commit

Permalink
Misc cleanups to TopScoreDocCollector (#13935)
Browse files Browse the repository at this point in the history
We can be a little less verbose for one big conditional and
optimize a few obvious spots in the logic.
  • Loading branch information
original-brownbear authored Jan 6, 2025
1 parent 204c39f commit d92d045
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
24 changes: 11 additions & 13 deletions lucene/core/src/java/org/apache/lucene/search/HitQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,22 @@ public final class HitQueue extends PriorityQueue<ScoreDoc> {
public HitQueue(int size, boolean prePopulate) {
super(
size,
() -> {
if (prePopulate) {
// Always set the doc Id to MAX_VALUE so that it won't be favored by
// lessThan. This generally should not happen since if score is not NEG_INF,
// TopScoreDocCollector will always add the object to the queue.
return new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY);
} else {
return null;
}
});
prePopulate
? () -> {
// Always set the doc Id to MAX_VALUE so that it won't be favored by
// lessThan. This generally should not happen since if score is not NEG_INF,
// TopScoreDocCollector will always add the object to the queue.
return new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY);
}
: () -> null);
}

@Override
protected final boolean lessThan(ScoreDoc hitA, ScoreDoc hitB) {
if (hitA.score == hitB.score) {
int cmp = Float.compare(hitA.score, hitB.score);
if (cmp == 0) {
return hitA.doc > hitB.doc;
} else {
return hitA.score < hitB.score;
}
return cmp < 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected int topDocsSize() {
// In case pq was populated with sentinel values, there might be less
// results than pq.size(). Therefore return all results until either
// pq.size() or totalHits.
return totalHits < pq.size() ? totalHits : pq.size();
return Math.min(totalHits, pq.size());
}

/** Returns the top docs that were collected by this collector. */
Expand Down

0 comments on commit d92d045

Please sign in to comment.