Skip to content

Commit

Permalink
Allow LuceneSourceOperator to early terminate (elastic#108820)
Browse files Browse the repository at this point in the history
We can make the LuceneSourceOperator collector to throw a CollectionTerminatedException whenever it has collected enough documents.
  • Loading branch information
iverase authored May 20, 2024
1 parent 4e1b8bf commit 60777cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/108820.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 108820
summary: Allow `LuceneSourceOperator` to early terminate
area: ES|QL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.compute.lucene;

import org.apache.lucene.search.CollectionTerminatedException;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorable;
Expand Down Expand Up @@ -90,6 +91,8 @@ public void collect(int doc) {
--remainingDocs;
docsBuilder.appendInt(doc);
currentPagePos++;
} else {
throw new CollectionTerminatedException();
}
}
};
Expand Down Expand Up @@ -117,14 +120,19 @@ public Page getCheckedOutput() throws IOException {
if (scorer == null) {
return null;
}
scorer.scoreNextRange(
leafCollector,
scorer.leafReaderContext().reader().getLiveDocs(),
// Note: if (maxPageSize - currentPagePos) is a small "remaining" interval, this could lead to slow collection with a
// highly selective filter. Having a large "enough" difference between max- and minPageSize (and thus currentPagePos)
// alleviates this issue.
maxPageSize - currentPagePos
);
try {
scorer.scoreNextRange(
leafCollector,
scorer.leafReaderContext().reader().getLiveDocs(),
// Note: if (maxPageSize - currentPagePos) is a small "remaining" interval, this could lead to slow collection with a
// highly selective filter. Having a large "enough" difference between max- and minPageSize (and thus currentPagePos)
// alleviates this issue.
maxPageSize - currentPagePos
);
} catch (CollectionTerminatedException ex) {
// The leaf collector terminated the execution
scorer.markAsDone();
}
Page page = null;
if (currentPagePos >= minPageSize || remainingDocs <= 0 || scorer.isDone()) {
pagesEmitted++;
Expand Down

0 comments on commit 60777cf

Please sign in to comment.