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

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 :

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

Expected Query:

"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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }

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