Skip to content

Speed up terms agg when alone #69377

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

Merged
merged 11 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -177,4 +177,4 @@ setup:
- 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 }
- gte: { profile.shards.0.aggregations.0.debug.segments_with_doc_count_field: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;

@ESIntegTestCase.SuiteScopeTestCase
Expand Down Expand Up @@ -633,10 +634,15 @@ 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("segments_with_doc_count_field", 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
List<?> filtersDebug = (List<?>) delegateDebug.get("filters");
assertThat(filtersDebug, hasSize(1));
Map<?, ?> queryDebug = (Map<?, ?>) filtersDebug.get(0);
assertThat(queryDebug, hasEntry("type", "query"));
assertThat(queryDebug, hasEntry("query", "ConstantScore(DocValuesFieldExistsQuery [field=date])"));
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.search.aggregations.bucket.filter;

import org.apache.lucene.search.Query;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
Expand All @@ -17,13 +16,13 @@
import org.elasticsearch.search.aggregations.support.AggregationContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FiltersAggregatorFactory extends AggregatorFactory {

private final String[] keys;
private final Query[] filters;
private final List<QueryToFilterAdapter<?>> filters;
private final boolean keyed;
private final boolean otherBucket;
private final String otherBucketKey;
Expand All @@ -35,20 +34,17 @@ public FiltersAggregatorFactory(String name, List<KeyedFilter> filters, boolean
this.keyed = keyed;
this.otherBucket = otherBucket;
this.otherBucketKey = otherBucketKey;
keys = new String[filters.size()];
this.filters = new Query[filters.size()];
for (int i = 0; i < filters.size(); ++i) {
KeyedFilter keyedFilter = filters.get(i);
this.keys[i] = keyedFilter.key();
this.filters[i] = context.buildQuery(keyedFilter.filter());
this.filters = new ArrayList<>(filters.size());
for (KeyedFilter f : filters) {
this.filters.add(QueryToFilterAdapter.build(context.searcher(), f.key(), context.buildQuery(f.filter())));
}
}

@Override
public Aggregator createInternal(Aggregator parent,
CardinalityUpperBound cardinality,
Map<String, Object> metadata) throws IOException {
return FiltersAggregator.build(name, factories, keys, filters, keyed,
return FiltersAggregator.build(name, factories, filters, keyed,
otherBucket ? otherBucketKey : null, context, parent, cardinality, metadata);
}
}
Loading