### Description

We have had this longstanding issue that running a top-level `filter` aggregation is often (not always, e.g. not if you need the total hit count too) a performance bug, as Elasticsearch would iterate over docs that match the `query` and would then check each of these docs against the filter of the `filter` aggregation. It's usually a better idea to put the filter directly into the query, where it can be used to drive iteration if appropriate.

We could do this automatically by implementing the `LeafCollector#competitiveIterator` API that was introduced for dynamic pruning, in order to automatically start intersecting the query with the filter of the `filter` aggregator once enough hits have been counted.
server/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/QueryToFilterAdapter.java
* {@link StreamOutput#writeGenericValue(Object)} and
     * {@link XContentBuilder#value(Object)}. And they'll have an integration
     * test.
     */
    void collectDebugInfo(BiConsumer<String, Object> add) {
        add.accept("query", query.toString());
        add.accept("segments_counted_in_constant_time", segmentsCountedInConstantTime);
    }
    private Weight weight() throws IOException {
        if (weight == null) {
            weight = searcher().createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        }
        return weight;
    }
    /**
     * Checks if all passed filters contain MatchNoDocsQuery queries. In this case, filter aggregation produces
     * no docs for the given segment.
     * @param filters list of filters to check
     * @return true if all filters match no docs, otherwise false
     */
    static boolean MatchesNoDocs(List<QueryToFilterAdapter> filters) {
        for (QueryToFilterAdapter filter : filters) {
            if (filter.query() instanceof MatchNoDocsQuery == false) {
                return false;
            }
        }
        return true;
    }
}