Access aggregation key inside script

Hi, I have a filter script inside an Aggregation. Inside this script i would like to access the key from the outer Aggregation. Is this possible?

"aggs": {
        "category": {
          "terms": {
            "field": "category",
            "size": 20
          },
          "aggs": {
            "my_filter": {
              "filter": {
                "script": {
                  "lang":   "painless",
                  "inline": "if(category_key == 'phone'){do something}else{do something else}"
                }
              }
            }
          }
        }
      }

Thanks!

It's not possible for aggregations to access the key for the bucket they are in. To achieve what you want you could split your terms aggregation into two separate aggregations; a filter aggregation for category:phone which contains the filter you want to apply on the phone documents, and also a terms aggregation similar to your current one but with "excludes": "phone" so the phone documents do not appear in this aggregation since you already have that in the separate filter aggregation.

Hope that helps.

Thanks you!

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