Search code examples
elasticsearchelasticsearch-aggregationelasticsearch-dsl

Elasticsearch DSL filter on aggregation and extended stats


If I'm using aggregations.bucket with a metric, how can I filter that to control the lookback period? Similarly can a filter be used in the same way for extended stats? Here's a code snippet that works (along with the kind of filter I'd like to use):

s = Search(using=client)
s.aggs.bucket('some_bucket_by_day', 'date_histogram', field='time_field', interval='day')
        .metric('some_avg', 'avg', field='some_field')

Trying to filter somehow like this:

filter='range', **{'time_field': {'gte': 'now-10d'}}

Also if using extended_stats, could a filter work as well?

s.aggs.bucket('exchange_stats', 'extended_stats', field='some_field')

Thanks!


Solution

  • The filter can be applied at the query level, which will reduce the number of documents on which the aggregations need to be computed. Also extended_stats is a metric aggregation, not a bucket one. So you can do it like this:

    // create search and filter the document by date
    s = Search(using=client)
         .filter('range', time_field={'gte': 'now-10d'})
    
    // add some aggregations
    s.aggs.bucket('some_bucket_by_day', 'date_histogram', field='time_field', interval='day')
         .metric('some_avg', 'avg', field='some_field')
         .metric('exchange_stats', 'extended_stats', field='some_field')