Apache Lucene open-source search software
LUCENE-8432: TopFieldComparator stops calling the comparator when only counting hits.
Adrien Grandba83c5a26a9e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a9f93b9523f..5120f2808f3 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -274,6 +274,10 @@ Improvements* LUCENE-8460: Better argument validation in StoredField. (Namgyu Kim)+* LUCENE-8432: TopFieldComparator stops comparing documents if the index is+ sorted, even if hits still need to be visited to compute the hit count.+ (Nikolay Khitrin)+Other:* LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11'sdiff --git a/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java b/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.javaindex 8f0e05974bb..90c45559a63 100644--- a/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java+++ b/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java@@ -107,21 +107,25 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {return new MultiComparatorLeafCollector(comparators, reverseMul) {+ boolean collectedAllCompetitiveHits = false;+@Overridepublic void collect(int doc) throws IOException {++totalHits;if (queueFull) {- if (reverseMul * comparator.compareBottom(doc) <= 0) {+ if (collectedAllCompetitiveHits || reverseMul * comparator.compareBottom(doc) <= 0) {// since docs are visited in doc Id order, if compare is 0, it means// this document is largest than anything else in the queue, and// therefore not competitive.- if (canEarlyTerminate && totalHits >= totalHitsThreshold) {- totalHitsRelation = Relation.GREATER_THAN_OR_EQUAL_TO;- throw new CollectionTerminatedException();- } else {- // just move to the next doc- return;+ if (canEarlyTerminate) {+ if (totalHits >= totalHitsThreshold) {+ totalHitsRelation = Relation.GREATER_THAN_OR_EQUAL_TO;+ throw new CollectionTerminatedException();+ } else {+ collectedAllCompetitiveHits = true;+ }}+ return;}// This hit is competitive - replace bottom element in queue & adjustTop@@ -183,6 +187,8 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {canEarlyTerminate(sort, indexSort);return new MultiComparatorLeafCollector(queue.getComparators(context), queue.getReverseMul()) {+ boolean collectedAllCompetitiveHits = false;+@Overridepublic void collect(int doc) throws IOException {//System.out.println(" collect doc=" + doc);@@ -192,16 +198,19 @@ public abstract class TopFieldCollector extends TopDocsCollector<Entry> {if (queueFull) {// Fastmatch: return if this hit is no better than// the worst hit currently in the queue:- final int cmp = reverseMul * comparator.compareBottom(doc);- if (cmp <= 0) {- // not competitive since documents are visited in doc id order- if (canEarlyTerminate && totalHits >= totalHitsThreshold) {- totalHitsRelation = Relation.GREATER_THAN_OR_EQUAL_TO;- throw new CollectionTerminatedException();- } else {- // just move to the next doc- return;+ if (collectedAllCompetitiveHits || reverseMul * comparator.compareBottom(doc) <= 0) {+ // since docs are visited in doc Id order, if compare is 0, it means+ // this document is largest than anything else in the queue, and+ // therefore not competitive.+ if (canEarlyTerminate) {+ if (totalHits >= totalHitsThreshold) {+ totalHitsRelation = Relation.GREATER_THAN_OR_EQUAL_TO;+ throw new CollectionTerminatedException();+ } else {+ collectedAllCompetitiveHits = true;+ }}+ return;}}