Skip to content

Commit 5f94725

Browse files
committed
minimum override
1 parent 270f63e commit 5f94725

File tree

2 files changed

+14
-40
lines changed

2 files changed

+14
-40
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,25 @@ public TopDocs topDocs(int start, int howMany) {
153153
howMany = Math.min(size - start, howMany);
154154
ScoreDoc[] results = new ScoreDoc[howMany];
155155

156-
// pq's pop() returns the 'least' element in the queue, therefore need
157-
// to discard the first ones, until we reach the requested range.
156+
// Prune the least competitive hits until we reach the requested range.
158157
// Note that this loop will usually not be executed, since the common usage
159158
// should be that the caller asks for the last howMany results. However it's
160159
// needed here for completeness.
161-
for (int i = pq.size() - start - howMany; i > 0; i--) {
162-
pq.pop();
163-
}
160+
pruneLeastCompetitiveHitsTo(start + howMany);
164161

165162
// Get the requested results from pq.
166163
populateResults(results, howMany);
167164

168165
return newTopDocs(results, start);
169166
}
167+
168+
/**
169+
* Prune the least competitive hits until the number of candidates is less than or equal to {@code
170+
* keep}. This is typically called before {@link #populateResults} to ensure we are at right pos.
171+
*/
172+
protected void pruneLeastCompetitiveHitsTo(int keep) {
173+
for (int i = pq.size() - keep; i > 0; i--) {
174+
pq.pop();
175+
}
176+
}
170177
}

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -191,42 +191,9 @@ protected void populateResults(ScoreDoc[] results, int howMany) {
191191
}
192192

193193
@Override
194-
public TopDocs topDocs(int start, int howMany) {
195-
// In case pq was populated with sentinel values, there might be less
196-
// results than pq.size(). Therefore return all results until either
197-
// pq.size() or totalHits.
198-
int size = topDocsSize();
199-
200-
if (howMany < 0) {
201-
throw new IllegalArgumentException(
202-
"Number of hits requested must be greater than 0 but value was " + howMany);
203-
}
204-
205-
if (start < 0) {
206-
throw new IllegalArgumentException(
207-
"Expected value of starting position is between 0 and " + size + ", got " + start);
208-
}
209-
210-
if (start >= size || howMany == 0) {
211-
return newTopDocs(null, start);
212-
}
213-
214-
// We know that start < pqsize, so just fix howMany.
215-
howMany = Math.min(size - start, howMany);
216-
ScoreDoc[] results = new ScoreDoc[howMany];
217-
218-
// pq's pop() returns the 'least' element in the queue, therefore need
219-
// to discard the first ones, until we reach the requested range.
220-
// Note that this loop will usually not be executed, since the common usage
221-
// should be that the caller asks for the last howMany results. However it's
222-
// needed here for completeness.
223-
for (int i = heap.size() - start - howMany; i > 0; i--) {
194+
protected void pruneLeastCompetitiveHitsTo(int keep) {
195+
for (int i = heap.size() - keep; i > 0; i--) {
224196
heap.pop();
225197
}
226-
227-
// Get the requested results from pq.
228-
populateResults(results, howMany);
229-
230-
return newTopDocs(results, start);
231198
}
232199
}

0 commit comments

Comments
 (0)