# Date aggregation in ES

**URL:** <https://discuss.elastic.co/t/date-aggregation-in-es/202344>\
**Category:** Elasticsearch\
**Created:** [October 4, 2019, 12:52pm UTC](https://discuss.elastic.co/t/date-aggregation-in-es/202344 "2019-10-04T12:52:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![avinsahlg](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@avinsahlg](https://discuss.elastic.co/u/avinsahlg)\
**Post date:** [October 4, 2019, 12:52pm UTC](https://discuss.elastic.co/t/date-aggregation-in-es/202344/1 "2019-10-04T12:52:12Z")

</div>

I am new to elastic search, would someone kind enough to provide me a jason query to search last two months documents based on below data

"\_source":{"date":"2016-08-01T10:24:29+0000","clientCode":"xxxx","countryCode":"AU"}

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [October 11, 2019, 7:02pm UTC](https://discuss.elastic.co/t/date-aggregation-in-es/202344/2 "2019-10-11T19:02:31Z")

</div>

Hi @avinsahlg,

Please, check the [range query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#ranges-on-dates) using date fields documentation.

It would be something like this:

a. Create the document

```auto
PUT test/_doc/1
{
  "date": "2019-08-12T10:24:29+0000",
  "clientCode": "xxxx",
  "countryCode": "AU"
}

```

b. Search for last 2 months:

```auto
GET test/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "now/d-2M",
        "lte": "now/d"
      }
    }
  }
}

```

---

<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 8, 2019, 7:13pm UTC](https://discuss.elastic.co/t/date-aggregation-in-es/202344/3 "2019-11-08T19:13:18Z")

</div>

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