Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch extended_bounds min max

Im using elasticsearch aggregations to return two different aggregate groups, one for the current week and one for the previous week, my current week aggregate looks like this:

"aggregations": {            
    "current": {
        "date_histogram": {
            "field": "date",
            "interval": "1d",
            "min_doc_count": 0,
            "extended_bounds": {
                "min": new Date().setDate(new Date().getDate() - 7),
                "max": new Date().getDate()
            }
        }
    }
}

At this point all is fine, im using min_doc_count in conjunction with extended_bounds to fill in the gaps for empty buckets in case there are any.

On my other aggregate I repeat this process almost the same way but I want my buckets be for the previous period !

I know that extended_bounds is not filtering buckets therefore I though about adding a filter on top of my aggregation like this:

"aggregations": {
    filtered: {
        "filter" : { 
            "bool": {
                "must": [{
                    "range" : { 
                        date: {
                            from: new Date().setDate(new Date().getDate() - 14),
                            to: new Date().setDate(new Date().getDate() - 7)
                        }
                    }
                }]
            }
        },
    },
    "previous": {
        "date_histogram": {
            "field": "date",
            "interval": "1d",
            "min_doc_count": 0,
            "extended_bounds": {
                "min": new Date().setDate(new Date().getDate() - 14),
                "max": new Date().setDate(new Date().getDate() - 7)
            }
        }
    }
}

The second aggregate should go back in time, exactly 14 days up until 7 days ago. I know the min works, but the max does not, the returned buckets go up until today even though Im using a filter to restrict the range, Im using must here but does not seem to do anything. Im quite new to ES, maybe Im missing something obvious here, pardon my lack of knowledge.

like image 662
MiniQuark Avatar asked Mar 19 '23 00:03

MiniQuark


1 Answers

According to the documentation:

Note that (as the name suggest) extended_bounds is not filtering buckets. Meaning, if the extended_bounds.min is higher than the values extracted from the documents, the documents will still dictate what the first bucket will be (and the same goes for the extended_bounds.max and the last bucket). For filtering buckets, one should nest the histogram aggregation under a range filter aggregation with the appropriate from/to settings.

Example:

{
    "query" : {
        "filtered" : { "filter": { "range" : { "price" : { "to" : "500" } } } }
    },
    "aggs" : {
        "prices" : {
            "histogram" : {
                "field" : "price",
                "interval" : 50,
                "min_doc_count" : 0,
                "extended_bounds" : {
                    "min" : 0,
                    "max" : 500
                }
            }
        }
    }
}

Long story short: use a query filter, or nest the histogram aggregation inside of your filter aggregation. In your example you're not nesting the aggregations but using them side by side (so the histogram agg isn't filtered by the filter-agg).

like image 66
Yousef Avatar answered Mar 28 '23 15:03

Yousef