Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,34 @@ setup:
- match: { aggregations.composite_agg.buckets.2.key.num_terms: 500 }
- match: { aggregations.composite_agg.buckets.2.doc_count: 11 }

---
"Test filters agg with doc_count":
- skip:
version: " - 7.99.99"
reason: "fixed in 8.0.0 to be backported to 7.11.2"
- do:
search:
body:
profile: true
size: 0
aggs:
f:
filters:
filters:
abc:
match:
str: abc
foo:
match:
str: foo
xyz:
match:
str: xyz

- match: { hits.total.value: 5 }
- length: { aggregations.f.buckets: 3 }
- match: { aggregations.f.buckets.abc.doc_count: 11 }
- match: { aggregations.f.buckets.foo.doc_count: 8 }
- match: { aggregations.f.buckets.xyz.doc_count: 5 }
- match: { profile.shards.0.aggregations.0.type: FiltersAggregator.FilterByFilter }
- gte: { profile.shards.0.aggregations.0.debug.segments_with_doc_count: 1 }
Copy link
Member Author

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.

Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ public void testFilterByFilter() throws InterruptedException, IOException {
assertThat(delegate.get("delegate"), equalTo("FiltersAggregator.FilterByFilter"));
Map<?, ?> delegateDebug = (Map<?, ?>) delegate.get("delegate_debug");
assertThat(delegateDebug, hasEntry("segments_with_deleted_docs", 0));
assertThat(delegateDebug, hasEntry("segments_with_doc_count", 0));
assertThat(delegateDebug, hasEntry("max_cost", (long) RangeAggregator.DOCS_PER_RANGE_TO_USE_FILTERS * 2));
assertThat(delegateDebug, hasEntry("estimated_cost", (long) RangeAggregator.DOCS_PER_RANGE_TO_USE_FILTERS * 2));
assertThat((long) delegateDebug.get("estimate_cost_time"), greaterThanOrEqualTo(0L)); // ~1,276,734 nanos is normal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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++;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to use a different Counter implementation that always returns 1 when the provider will always return one. I trust the jvm to inline the calls on it regardless because counter is effectively final. It'd save a couple of comparisons in this very very hot section. But performance tests seemed fine so I didn't end up doing it.

for (int filterOrd = 0; filterOrd < filters.length; filterOrd++) {
BulkScorer scorer;
if (scorers == null) {
Expand All @@ -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();
Expand All @@ -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 {}
}
}

/**
Expand Down