# Access "uncle" aggregation from bucket

**URL:** <https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099>\
**Category:** Elasticsearch\
**Created:** [September 8, 2016, 4:43pm UTC](https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099 "2016-09-08T16:43:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredericogalvao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fredericogalvao/32/33093_2.png) [@fredericogalvao](https://discuss.elastic.co/u/fredericogalvao)\
**Post date:** [September 8, 2016, 4:43pm UTC](https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099/1 "2016-09-08T16:43:29Z")

</div>

Consider I have the following doc `_source`'s

```json
[{"x": "AA", "y": 10},
{"x": "AA", "y": 20},
{"x": "AB", "y": 5},
{"x": "BB", "y": 50},
{"x": "BC", "y": 15},
{"x": "BC", "y": 0},
{"x": "CC", "y": -10}]

```

I want all docs

- filtered by `{"range": {"y": {"gte": 0}}}`
- grouped by a `terms` on `x`
- with the sum of all `y` of that bucket
- with the percentage of said local sum on `y` over the sum of all `y` of this search context (all buckets)

I want to get a list of aggregations with a structure similar to this:

```json
{
"sum_by_x": {
  "buckets": [{
    "key": "AA",
    "localSum": {"value": 30},
    "percentage": {"value": 0.3}
  },{
    "key": "AB",
    "localSum": {"value": 5},
    "percentage": {"value": 0.05}
  },{
    "key": "BB",
    "localSum": {"value": 50},
    "percentage": {"value": 0.5}
  },{
    "key": "BC",
    "localSum": {"value": 15},
    "percentage": {"value": 0.15}
  }]
},
"globalSum": {"value": 100}

```

I've done my share of research on many combinations and structures to try and make that global sum available as a bucket\_path to the terms aggregation, but with no success so far.

My base query looks something like this for now (please ignore the boilerplate structure for the query):

```json
{
  "size": 0,
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "y": {
                  "gte": 0
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "sum_by_x": {
      "terms": {
        "field": "x"
      },
      "aggs": {
        "localSum": {
          "sum": {
            "field": "y"
          }
        }
      }
    },
    "globalSum": {
      "sum": {
        "field": "y"
      }
    }
  }
}

```

Is there any way I can access the globalSum while creating the sum\_by\_x buckets so I can calculate the percentage of each bucket towards the total?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 9, 2016, 7:36am UTC](https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099/2 "2016-09-09T07:36:30Z")

</div>

This is unfortunately not currently possible. It's something we have talked about, would love to add and have even attempted to add but unfortunately it requires some tricky problems in the reduce phase of aggregations to be overcome.

For now the final part of this calculation needs to be done in the client application to obtain figures like the percentages you are after here.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 9, 2016, 8:49am UTC](https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099/3 "2016-09-09T08:49:17Z")

</div>

The significant\_terms aggregation provides some of those "percentage of background" stats but the basis of its accounting is currently _volumes_ of docs, not quantities of values held on those docs.  
There is an open issue for this [1] but we don't have any development on it just yet.

[1] [https://github.com/elastic/elasticsearch/issues/12309](https://github.com/elastic/elasticsearch/issues/12309)

---

<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:** [July 5, 2017, 10:21pm UTC](https://discuss.elastic.co/t/access-uncle-aggregation-from-bucket/60099/4 "2017-07-05T22:21:39Z")

</div>


