-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix filter by filter execution with doc_count #68930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,4 +43,13 @@ public int getDocCount(int doc) throws IOException { | |
| public void setLeafReaderContext(LeafReaderContext ctx) throws IOException { | ||
| docCountPostings = ctx.reader().postings(new Term(DocCountFieldMapper.NAME, DocCountFieldMapper.NAME)); | ||
| } | ||
|
|
||
| public boolean alwaysOne() { | ||
| return docCountPostings == null; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "doc counts are " + (alwaysOne() ? "always one" : "based on " + docCountPostings); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used this while debugging. We certainly don't need it but if folks don't object I'd like to keep it. I'm sure I'll bump into this class later on and this helps. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,12 @@ | |
| import org.apache.lucene.search.CollectionTerminatedException; | ||
| import org.apache.lucene.search.IndexOrDocValuesQuery; | ||
| import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery; | ||
| import org.apache.lucene.search.LeafCollector; | ||
| import org.apache.lucene.search.MatchAllDocsQuery; | ||
| import org.apache.lucene.search.PointRangeQuery; | ||
| import org.apache.lucene.search.Query; | ||
| import org.apache.lucene.search.Scorable; | ||
| import org.apache.lucene.search.ScoreMode; | ||
| import org.apache.lucene.search.TotalHitCountCollector; | ||
| import org.apache.lucene.search.Weight; | ||
| import org.apache.lucene.util.Bits; | ||
| import org.elasticsearch.common.ParseField; | ||
|
|
@@ -38,6 +39,7 @@ | |
| import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
| import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; | ||
| import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; | ||
| import org.elasticsearch.search.aggregations.bucket.DocCountProvider; | ||
| import org.elasticsearch.search.aggregations.support.AggregationContext; | ||
|
|
||
| import java.io.IOException; | ||
|
|
@@ -275,6 +277,11 @@ public static class FilterByFilter extends FiltersAggregator { | |
| */ | ||
| private BulkScorer[][] scorers; | ||
| private int segmentsWithDeletedDocs; | ||
| /** | ||
| * Count of segments with documents have consult the {@code doc_count} | ||
| * field. | ||
| */ | ||
| private int segmentsWithDocCount; | ||
|
|
||
| private FilterByFilter( | ||
| String name, | ||
|
|
@@ -354,6 +361,10 @@ protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucket | |
| weights = buildWeights(topLevelQuery(), filters); | ||
| } | ||
| Bits live = ctx.reader().getLiveDocs(); | ||
| Counter counter = new Counter(docCountProvider); | ||
| if (false == docCountProvider.alwaysOne()) { | ||
| segmentsWithDocCount++; | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible to use a different |
||
| for (int filterOrd = 0; filterOrd < filters.length; filterOrd++) { | ||
| BulkScorer scorer; | ||
| if (scorers == null) { | ||
|
|
@@ -367,9 +378,8 @@ protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucket | |
| // the filter doesn't match any docs | ||
| continue; | ||
| } | ||
| TotalHitCountCollector collector = new TotalHitCountCollector(); | ||
| scorer.score(collector, live); | ||
| incrementBucketDocCount(filterOrd, collector.getTotalHits()); | ||
| scorer.score(counter, live); | ||
| incrementBucketDocCount(filterOrd, counter.readAndReset(ctx)); | ||
| } | ||
| // Throwing this exception is how we communicate to the collection mechanism that we don't need the segment. | ||
| throw new CollectionTerminatedException(); | ||
|
|
@@ -379,13 +389,42 @@ protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucket | |
| public void collectDebugInfo(BiConsumer<String, Object> add) { | ||
| super.collectDebugInfo(add); | ||
| add.accept("segments_with_deleted_docs", segmentsWithDeletedDocs); | ||
| add.accept("segments_with_doc_count", segmentsWithDocCount); | ||
| if (estimatedCost != -1) { | ||
| // -1 means we didn't estimate it. | ||
| add.accept("estimated_cost", estimatedCost); | ||
| add.accept("max_cost", maxCost); | ||
| add.accept("estimate_cost_time", estimateCostTime); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Counts collected documents, delegating to {@link DocCountProvider} for | ||
| * how many documents each search hit is "worth". | ||
| */ | ||
| private static class Counter implements LeafCollector { | ||
| private final DocCountProvider docCount; | ||
| private long count; | ||
|
|
||
| Counter(DocCountProvider docCount) { | ||
| this.docCount = docCount; | ||
| } | ||
|
|
||
| public long readAndReset(LeafReaderContext ctx) throws IOException { | ||
| long result = count; | ||
| count = 0; | ||
| docCount.setLeafReaderContext(ctx); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void collect(int doc) throws IOException { | ||
| count += docCount.getDocCount(doc); | ||
| } | ||
|
|
||
| @Override | ||
| public void setScorer(Scorable scorer) throws IOException {} | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figure it'll be nice to see this in the profile so if it is 0 we can know we didn't need to do anything fancy to get the doc count.