# Execute aggs query for each unqiue value?

**URL:** <https://discuss.elastic.co/t/execute-aggs-query-for-each-unqiue-value/268997>\
**Category:** Elasticsearch\
**Created:** [April 1, 2021, 8:38am UTC](https://discuss.elastic.co/t/execute-aggs-query-for-each-unqiue-value/268997 "2021-04-01T08:38:49Z")\
**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:** [April 1, 2021, 8:38am UTC](https://discuss.elastic.co/t/execute-aggs-query-for-each-unqiue-value/268997/1 "2021-04-01T08:38:49Z")

</div>

Hi,

I have the following script which gives me the 10 most used applications. I want to expand this by getting the top 10 applications for each unique value in the `customers` field. Is this possible?

The goal is to apply a set document id such as "customerid-yyyy-mm-dd" and make a lightweight index where all the data is "precalculated" every couple of hours instead of doing a full search every time for users that only need some static data.

```
{
  "aggs": {
    "2": {
      "terms": {
        "field": "application",
        "order": {
          "1": "desc"
        },
        "size": 10
      },
      "aggs": {
        "1": {
          "sum": {
            "script": {
              "source": "doc['in_bytes'].value + doc['out_bytes'].value",
              "lang": "painless"
            }
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        },
        {
        "range": {
          "@timestamp": {
            "gte": "now-24h"
            }
          }
        }
      ]
    }
  }
}
```

---

<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:** [April 29, 2021, 8:39am UTC](https://discuss.elastic.co/t/execute-aggs-query-for-each-unqiue-value/268997/2 "2021-04-29T08:39:09Z")

</div>

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