# How to add date range to query?

**URL:** <https://discuss.elastic.co/t/how-to-add-date-range-to-query/141861>\
**Category:** Elasticsearch\
**Created:** [July 27, 2018, 2:47am UTC](https://discuss.elastic.co/t/how-to-add-date-range-to-query/141861 "2018-07-27T02:47:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sjaak01](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Sjaak01](https://discuss.elastic.co/u/Sjaak01)\
**Post date:** [July 27, 2018, 2:47am UTC](https://discuss.elastic.co/t/how-to-add-date-range-to-query/141861/1 "2018-07-27T02:47:10Z")

</div>

Hi,

How can I add a range filter to the `usage_offset` part of this query? I want `usage_current` to return the last known value and`usage_offset` the return the last known value for 1 hour or more ago.

I tried adding range filters and range aggegrations but they all cause the query to fail.

```auto
{
  "query": {
    "match": {
      "name": "test"
    }
  },
           "aggs": {
                "usage_current": {
                    "top_hits": {
                        "sort": [
                            {
                                "@timestamp": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                },
                "usage_offset": {
                    "top_hits": {
                        "sort": [
                            {
                                "@timestamp": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                }
            }
}
```

---

<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:** [August 24, 2018, 2:47am UTC](https://discuss.elastic.co/t/how-to-add-date-range-to-query/141861/2 "2018-08-24T02:47:24Z")

</div>

Closed on August 24, 2018, 2:47am UTC

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