# How to Get Bucket Key in Terms Aggregation to use in Sub Aggregations

**URL:** <https://discuss.elastic.co/t/how-to-get-bucket-key-in-terms-aggregation-to-use-in-sub-aggregations/243699>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 4, 2020, 11:41am UTC](https://discuss.elastic.co/t/how-to-get-bucket-key-in-terms-aggregation-to-use-in-sub-aggregations/243699 "2020-08-04T11:41:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjieperez28](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjieperez28/32/50609_2.png) [@benjieperez28](https://discuss.elastic.co/u/benjieperez28)\
**Post date:** [August 4, 2020, 11:41am UTC](https://discuss.elastic.co/t/how-to-get-bucket-key-in-terms-aggregation-to-use-in-sub-aggregations/243699/1 "2020-08-04T11:41:19Z")

</div>

Here I have an aggregation to determine each article count per publication,  
now my question is I have to make a sub aggregation base on per publication (bucket.key), is it possible to get the bucket key after the term aggregation and make a sub aggregation on it.

Raw Query :

```auto
"size": 0,
  "track_total_hits": true, 
  "query": {...}
  "aggs": {
     "per_publications": {
      "terms": {
        "field": "pub_id"
      }
    }
  }

```

Expected Query:

```auto
"size": 0,
  "track_total_hits": true, 
  "query": {...},
 "aggs": {
    "mm": {
      "terms": {
        "field": "pub_id"
      },
      "aggs": {
    "PER_PLATFORM_FREQ_DISTRI": {
          "nested": {
            "path": "latest"
          },
          "aggs": {
            "SVALUE": {
              "terms": {
                "script": {
                  "source": "\r\n double temp = Double.parseDouble((((doc['latest.soc_mm_score'].value * 10) / 146879.5).toString()));\r\n return temp;\r\n ",
                  "lang": "painless"
                },
                "size": 8000
              },
              "aggs": {
                "AVG_KSCORE": {
                  "avg": {
                    "script": {
                      "lang": "painless",
                      "source": "return (doc['latest.soc_kf_score'].value > 0)?((doc['latest.soc_kf_score'].value-params['rnak']['<bucket key value here>']['min'])*10.0)/(params['rnak']['<bucket key value here>']['max']-params['rnak']['<bucket key value here>']['min']):(0)",
                      "params": {
                        "rnak": {
                          "4737": {
                            "max": 1,
                            "min": 0
                          },
                          "4744": {
                            "max": 0.4189189076423645,
                            "min": 0
                          },
                          "4854": {
                            "max": 0.5833333134651184,
                            "min": 0
                          },
                          "35386": {
                            "max": 0.875,
                            "min": 0
                          }
                        }
                      }
                    }
                  }
                },
                "SUM_ENG": {
                  "avg": {
                    "field": "latest.soc_mm_score"
                  }
                }
              }
            }
          }
        }
      }
    }
  }

```

---

<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:** [September 1, 2020, 11:41am UTC](https://discuss.elastic.co/t/how-to-get-bucket-key-in-terms-aggregation-to-use-in-sub-aggregations/243699/2 "2020-09-01T11:41:27Z")

</div>

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