# Get Current Top 5 Over Time with Date Histogram

**URL:** https://discuss.elastic.co/t/get-current-top-5-over-time-with-date-histogram/287473
**Category:** Elasticsearch
**Created:** [October 22, 2021, 10:47pm UTC](https://discuss.elastic.co/t/get-current-top-5-over-time-with-date-histogram/287473 "2021-10-22T22:47:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![dmer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmer/32/96277_2.png) [@dmer](https://discuss.elastic.co/u/dmer)
#### Post date: [October 22, 2021, 10:47pm UTC](https://discuss.elastic.co/t/get-current-top-5-over-time-with-date-histogram/287473/1 "2021-10-22T22:47:57Z")

</div>

Suppose I have an index filled with `Person` records, and I want to get the doc counts for the top 5 `name`s (based on today) over the past week. To clarify, I don't want the top 5 names each day, instead I want to show the counts of today's top 5 names over the past week. Currently, this is my strategy:

```auto
{
  "aggs": {
    "sales_over_time": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "day",
        "extended_bounds": {
          "min": "now-7d/d",
          "max": "now"
        }
      },
      "aggs": {
        "names": {
          "terms": {
            "field": "name",
            "size": 10000
          }
         }
       }
    }
  }
}

```

I use a large (10k) size, and filter results based on which names are in the top 5 in today's histogram bucket. However, this is very slow and doesn't guarantee I get all results.

A better way would be to filter the data to only records that have one of the top 5 names of the current day.

I know this could be done by using two queries by having the first fetch the top 5 names of today and using the results to filter the second query that fetches counts over the last 7 days. However, it would be much better for me to do this in one query.

Is there any way this could be done in a single query?

---

<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 19, 2021, 10:48pm UTC](https://discuss.elastic.co/t/get-current-top-5-over-time-with-date-histogram/287473/2 "2021-11-19T22:48:29Z")

</div>

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