def dict_with_date_buckets(a_dict):
    """Given an Elasticsearch aggregation, return the 'buckets' part.

    Also sort it in descending order if it's a range facet
    because Elasticsearch always returns those in ascending order.
    """
    if 'buckets' in a_dict:
        buckets = a_dict['buckets']
        buckets.sort(key=lambda x: -x['from'])
        return buckets
    else:
        for k, v in a_dict.items():
            if isinstance(v, dict) and 'buckets' in v:
                return a_dict[k]['buckets']
    raise Exception('Should not happen: aggregations dict from Elasticsearch '
                    'does not have an aggregation with "buckets" array')