# How can I construct a filter that matches a bucket from a date\_histogram aggregation?

**URL:** <https://discuss.elastic.co/t/how-can-i-construct-a-filter-that-matches-a-bucket-from-a-date-histogram-aggregation/103272>\
**Category:** Elasticsearch\
**Created:** [October 9, 2017, 11:41pm UTC](https://discuss.elastic.co/t/how-can-i-construct-a-filter-that-matches-a-bucket-from-a-date-histogram-aggregation/103272 "2017-10-09T23:41:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonwillison](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonwillison/32/22902_2.png) [@simonwillison](https://discuss.elastic.co/u/simonwillison)\
**Post date:** [October 9, 2017, 11:41pm UTC](https://discuss.elastic.co/t/how-can-i-construct-a-filter-that-matches-a-bucket-from-a-date-histogram-aggregation/103272/1 "2017-10-09T23:41:56Z")

</div>

Say for example I have a date\_histogram aggregation that looks like this:

```
{
        "date_histogram": {
            "field": "request_datetime",
            "interval": "hour"
        }
}

```

I get back buckets that look like this:

```
[
    {
        "key_as_string": "2017-09-25T07:00:00.000Z",
        "key": 1506322800000,
        "doc_count": 20
    },
    {
        "key_as_string": "2017-09-25T08:00:00.000Z",
        "key": 1506326400000,
        "doc_count": 27
    },
    {
        "key_as_string": "2017-09-25T09:00:00.000Z",
        "key": 1506330000000,
        "doc_count": 22
    },
    {
        "key_as_string": "2017-09-25T10:00:00.000Z",
        "key": 1506333600000,
        "doc_count": 42
    },
]

```

I'm providing a UI where users can select one of those histogram buckets to further filter the results. When they select a bucket, what is the correct way of constructing a filter that will apply to those results?

For year I went with this... but I don't know if this is guaranteed to return the exact same count of results as the corresponding date\_histogram bucket:

```
{
    "range": {
        "request_datetime": {
            "gte": "2017-10-09T00:00:00Z",
            "lte": "2017-10-09T23:59:59Z"
        }
    }
}

```

What should this look like for day/hour/etc?

---

<div class="post-metadata">

**Author:** ![simonwillison](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonwillison/32/22902_2.png) [@simonwillison](https://discuss.elastic.co/u/simonwillison)\
**Post date:** [October 10, 2017, 2:10am UTC](https://discuss.elastic.co/t/how-can-i-construct-a-filter-that-matches-a-bucket-from-a-date-histogram-aggregation/103272/2 "2017-10-10T02:10:22Z")

</div>

Got a great answer to this from Honza: [https://github.com/elastic/elasticsearch-dsl-py/blob/master/elasticsearch\_dsl/faceted\_search.py#L152-L158](https://github.com/elastic/elasticsearch-dsl-py/blob/master/elasticsearch_dsl/faceted_search.py#L152-L158)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 7, 2017, 2:11am UTC](https://discuss.elastic.co/t/how-can-i-construct-a-filter-that-matches-a-bucket-from-a-date-histogram-aggregation/103272/3 "2017-11-07T02:11:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
