# Date range query for the first two hours

**URL:** <https://discuss.elastic.co/t/date-range-query-for-the-first-two-hours/190113>\
**Category:** Elasticsearch\
**Created:** [July 11, 2019, 9:33pm UTC](https://discuss.elastic.co/t/date-range-query-for-the-first-two-hours/190113 "2019-07-11T21:33:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehdi-gital](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mehdi-gital](https://discuss.elastic.co/u/mehdi-gital)\
**Post date:** [July 11, 2019, 9:33pm UTC](https://discuss.elastic.co/t/date-range-query-for-the-first-two-hours/190113/1 "2019-07-11T21:33:16Z")

</div>

I have a time series data where each document kinda looks like this:

{"ID": ABC1,  
"timestamp": "2015-01-01T12:12:12",  
"key": myKey,  
"value": 433}

I'm trying to write an aggregation to get the count of keys that appear under each ID in the first two hours using a range query.

What's the syntax for picking up the minimum timestamp?

I know something like this picks up the min but I'm not sure where to put it. Do I need a script?

"time\_min": {  
"min": {  
"field": "timestamp"  
}  
}

Once I get that I could use something like:

```
"range": {
  "timestamp": {
    "gte": "time_min",
    "lte": "time_min+2h/h"
  }
}

```

Thank you!

---

<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 8, 2019, 9:42pm UTC](https://discuss.elastic.co/t/date-range-query-for-the-first-two-hours/190113/2 "2019-08-08T21:42:06Z")

</div>

Closed on August 8, 2019, 9:42pm UTC

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