# How to get counts / stats for different periods of time?

**URL:** <https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799>\
**Category:** Elasticsearch\
**Created:** [May 24, 2021, 8:58am UTC](https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799 "2021-05-24T08:58:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashwinbhaskar/32/88750_2.png) [@ashwinbhaskar](https://discuss.elastic.co/u/ashwinbhaskar)\
**Post date:** [May 24, 2021, 8:58am UTC](https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799/1 "2021-05-24T08:58:00Z")

</div>

I am trying to get the count of documents that were created for the last 10 weeks grouped by week. To get the count for the last week, I would do something like this

```auto
GET my_index/_count
{
  "query": {
    "range": {
      "document.createdOn": {
        "gte": "now-7d"
      }
    }
  }
}

```

But If I wanted the count for every week of the last 10 weeks, should I fire 10 queries with different `gte` and `lte` values? Or is there any way to do this in a single query?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 24, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799/2 "2021-05-24T09:31:47Z")

</div>

Welcome to our community! 😃

Why not use an aggregation - [Date range aggregation | Elasticsearch Guide [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html)

---

<div class="post-metadata">

**Author:** ![ashwinbhaskar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashwinbhaskar/32/88750_2.png) [@ashwinbhaskar](https://discuss.elastic.co/u/ashwinbhaskar)\
**Post date:** [May 24, 2021, 10:46am UTC](https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799/3 "2021-05-24T10:46:22Z")

</div>

Thank you:) and thanks for the suggestion. I did not know about Date Range Aggregation. I gave it a shot and this comes close to what I was looking for. Though, it returns all the documents along with the aggregations. Is there a way to make it not documents and make it return only the aggregations?

```auto
GET denormalized_spots/_search
{
  "aggs": {
    "range": {
      "date_range": {
        "field": "myObj.createdOn",
        "ranges": [
          {
            "from": "now-10d/d",
            "to": "now"
          },
          {
            "from": "now-20d/d",
            "to": "now-10d/d"
          }
        ]
      }
    }
  }
}

```

**Edit**

query parameter `size=0` did the trick

---

<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:** [June 21, 2021, 10:47am UTC](https://discuss.elastic.co/t/how-to-get-counts-stats-for-different-periods-of-time/273799/4 "2021-06-21T10:47:09Z")

</div>

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