From cdb651976e8213d6e7e140c5e8325af09f5c3948 Mon Sep 17 00:00:00 2001
From: Adrien Grand
A comparator must define these functions:
- * + * an int ranging from 0 to numHits-1. Segment transitions are + * handled by creating a dedicated per-segment + * {@link LeafFieldComparator} which also needs to interact + * with the {@link FieldValueHitQueue} but can optimize based + * on the segment to collect. + * + *The following functions need to be implemented
*For a search that hits many results, this method - * will be the hotspot (invoked by far the most - * frequently).
- * - * @param doc that was hit - * @return any {@code N < 0} if the doc's value is sorted after - * the bottom entry (not competitive), any {@code N > 0} if the - * doc's value is sorted before the bottom entry and {@code 0} if - * they are equal. - */ - public abstract int compareBottom(int doc) throws IOException; - - /** - * Compare the top value with this doc. This will - * only invoked after setTopValue has been called. This - * should return the same result as {@link - * #compare(int,int)}} as if topValue were slot1 and the new - * document were slot 2. This is only called for searches that - * use searchAfter (deep paging). - * - * @param doc that was hit - * @return any {@code N < 0} if the doc's value is sorted after - * the bottom entry (not competitive), any {@code N > 0} if the - * doc's value is sorted before the bottom entry and {@code 0} if - * they are equal. - */ - public abstract int compareTop(int doc) throws IOException; - - /** - * This method is called when a new hit is competitive. - * You should copy any state associated with this document - * that will be required for future comparisons, into the - * specified slot. - * - * @param slot which slot to copy the hit to - * @param doc docID relative to current reader - */ - public abstract void copy(int slot, int doc) throws IOException; - - /** - * Set a new {@link org.apache.lucene.index.LeafReaderContext}. All subsequent docIDs are relative to - * the current reader (you must add docBase if you need to - * map it to a top-level docID). - * - * @param context current reader context - * @return the comparator to use for this segment; most - * comparators can just return "this" to reuse the same - * comparator across segments - * @throws IOException if there is a low-level IO error - */ - public abstract FieldComparatorA leaf comparator must define these functions:
+ * + *For a search that hits many results, this method + * will be the hotspot (invoked by far the most + * frequently).
+ * + * @param doc that was hit + * @return any {@code N < 0} if the doc's value is sorted after + * the bottom entry (not competitive), any {@code N > 0} if the + * doc's value is sorted before the bottom entry and {@code 0} if + * they are equal. + */ + int compareBottom(int doc) throws IOException; + + /** + * Compare the top value with this doc. This will + * only invoked after setTopValue has been called. This + * should return the same result as {@link + * FieldComparator#compare(int,int)}} as if topValue were slot1 and the new + * document were slot 2. This is only called for searches that + * use searchAfter (deep paging). + * + * @param doc that was hit + * @return any {@code N < 0} if the doc's value is sorted after + * the bottom entry (not competitive), any {@code N > 0} if the + * doc's value is sorted before the bottom entry and {@code 0} if + * they are equal. + */ + int compareTop(int doc) throws IOException; + + /** + * This method is called when a new hit is competitive. + * You should copy any state associated with this document + * that will be required for future comparisons, into the + * specified slot. + * + * @param slot which slot to copy the hit to + * @param doc docID relative to current reader + */ + void copy(int slot, int doc) throws IOException; + + /** Sets the Scorer to use in case a document's score is + * needed. + * + * @param scorer Scorer instance that you should use to + * obtain the current hit's score, if necessary. */ + void setScorer(Scorer scorer); + +} diff --git a/lucene/core/src/java/org/apache/lucene/search/SimpleFieldComparator.java b/lucene/core/src/java/org/apache/lucene/search/SimpleFieldComparator.java new file mode 100644 index 00000000000..eb304c19657 --- /dev/null +++ b/lucene/core/src/java/org/apache/lucene/search/SimpleFieldComparator.java @@ -0,0 +1,42 @@ +package org.apache.lucene.search; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; + +import org.apache.lucene.index.LeafReaderContext; + +/** + * Base {@link FieldComparator} implementation that is used for all contexts. + * + * @lucene.experimental + */ +public abstract class SimpleFieldComparatorcontext
. */
+ protected abstract void doSetNextReader(LeafReaderContext context) throws IOException;
+
+ @Override
+ public final LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
+ doSetNextReader(context);
+ return this;
+ }
+
+ @Override
+ public void setScorer(Scorer scorer) {}
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/SortRescorer.java b/lucene/core/src/java/org/apache/lucene/search/SortRescorer.java
index 8d5144023f8..9f166cf3449 100644
--- a/lucene/core/src/java/org/apache/lucene/search/SortRescorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/SortRescorer.java
@@ -61,6 +61,7 @@ public class SortRescorer extends Rescorer {
int endDoc = 0;
int docBase = 0;
+ LeafCollector leafCollector = null;
FakeScorer fakeScorer = new FakeScorer();
while (hitUpto < hits.length) {
@@ -75,15 +76,15 @@ public class SortRescorer extends Rescorer {
if (readerContext != null) {
// We advanced to another segment:
- collector.getLeafCollector(readerContext);
- collector.setScorer(fakeScorer);
+ leafCollector = collector.getLeafCollector(readerContext);
+ leafCollector.setScorer(fakeScorer);
docBase = readerContext.docBase;
}
fakeScorer.score = hit.score;
fakeScorer.doc = docID - docBase;
- collector.collect(fakeScorer.doc);
+ leafCollector.collect(fakeScorer.doc);
hitUpto++;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java b/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
index cbef3b3484e..d9da8aef762 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TopDocsCollector.java
@@ -31,7 +31,7 @@ import org.apache.lucene.util.PriorityQueue;
* however, you might want to consider overriding all methods, in order to avoid
* a NullPointerException.
*/
-public abstract class TopDocsCollectorNOTE: The instances returned by this method
* pre-allocate a full array of length
* NOTE: The instances returned by this method
* pre-allocate a full array of length
* NOTE: The instances returned by this method
* pre-allocate a full array of length
@@ -237,11 +228,11 @@ public abstract class TopScoreDocCollector extends TopDocsCollector NOTE: The instances returned by this method
* pre-allocate a full array of length
@@ -249,27 +240,20 @@ public abstract class TopScoreDocCollector extends TopDocsCollectornumHits
.
- *
+ *
* @param sort
* the sort criteria (SortFields).
* @param numHits
@@ -1067,7 +884,7 @@ public abstract class TopFieldCollector extends TopDocsCollectortrackDocScores
to true as well.
* @param docsScoredInOrder
* specifies whether documents are scored in doc Id order or not by
- * the given {@link Scorer} in {@link #setScorer(Scorer)}.
+ * the given {@link Scorer} in {@link LeafCollector#setScorer(Scorer)}.
* @return a {@link TopFieldCollector} instance which will sort the results by
* the sort criteria.
* @throws IOException if there is a low-level I/O error
@@ -1086,7 +903,7 @@ public abstract class TopFieldCollector extends TopDocsCollectornumHits
.
- *
+ *
* @param sort
* the sort criteria (SortFields).
* @param numHits
@@ -1112,7 +929,7 @@ public abstract class TopFieldCollector extends TopDocsCollectortrackDocScores
to true as well.
* @param docsScoredInOrder
* specifies whether documents are scored in doc Id order or not by
- * the given {@link Scorer} in {@link #setScorer(Scorer)}.
+ * the given {@link Scorer} in {@link LeafCollector#setScorer(Scorer)}.
* @return a {@link TopFieldCollector} instance which will sort the results by
* the sort criteria.
* @throws IOException if there is a low-level I/O error
@@ -1125,7 +942,7 @@ public abstract class TopFieldCollector extends TopDocsCollector