# Help with aggregation needed

**URL:** <https://discuss.elastic.co/t/help-with-aggregation-needed/152066>\
**Category:** Elasticsearch\
**Created:** [October 11, 2018, 2:01pm UTC](https://discuss.elastic.co/t/help-with-aggregation-needed/152066 "2018-10-11T14:01:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emil.mirzayev](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@emil.mirzayev](https://discuss.elastic.co/u/emil.mirzayev)\
**Post date:** [October 11, 2018, 2:01pm UTC](https://discuss.elastic.co/t/help-with-aggregation-needed/152066/1 "2018-10-11T14:01:43Z")

</div>

Hi there!

I have a dataset of different devices and their consumption values (absolute) which are increasing by the time.  
Every device is connected to a group (there are 3 groups overall). What I want to achieve, is, to find the consumption of each group. It will be the sum of all latest values of the devices connected to that group.  
I tried a query, but I failed to write the correct one:

```auto
POST measurements/_search
{
  "size": 0,
  "aggs": {
    "max_per_group": {
      "terms": {
        "field": "group_name",
        "size": 99999
      },
      "aggs": {
        "max_meter": {
          "terms": {
            "field": "meter_id"
          },
          "aggs": {
            "max_cons": {
              "max": {
                "field": "value"
              }
            }
          }
        },
        "max_per_group" :{
          "sum_bucket": {
            "buckets_path": "max_meter>max_cons"
          }
        }
      }
    }
  }
}

```

It gives me the output:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 96000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "max_per_group": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Erfurt-Zone1",
          "doc_count": 61440,
          "max_meter": {
            "doc_count_error_upper_bound": 981,
            "sum_other_doc_count": 55312,
            "buckets": [
              ........................
          },
          "max_per_group": {
            "value": 489.1849772365934
          }

```

Which is incorrect It should be around 3k, and the aggregation gives me just 10 meters for that group, despite 60~.  
What am I doing wrong here? Can you help me with this query? Thanks!

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [October 22, 2018, 5:05pm UTC](https://discuss.elastic.co/t/help-with-aggregation-needed/152066/2 "2018-10-22T17:05:20Z")

</div>

I think the issue here is that you need to specify a `size` on the "max\_meter" terms agg as well, otherwise it will only show you the top 10 meters by default.

---

<div class="post-metadata">

**Author:** ![emil.mirzayev](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@emil.mirzayev](https://discuss.elastic.co/u/emil.mirzayev)\
**Post date:** [October 23, 2018, 7:14am UTC](https://discuss.elastic.co/t/help-with-aggregation-needed/152066/3 "2018-10-23T07:14:49Z")

</div>

Thanks! I have missed this tiny detail. Resolved

---

<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 20, 2018, 7:14am UTC](https://discuss.elastic.co/t/help-with-aggregation-needed/152066/4 "2018-11-20T07:14:51Z")

</div>

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