Pipeline Aggregation for pivot on aggregation

Been searching around quite a bit to find a solution to my problem. Hopefully this is possible with ElasticSearch. I have this query:

GET _search
{
  "query": {
    "bool": {
      "must": [
        {"exists" : { "field" : "lvl.keyword" }}
      ]
    }
  },
  "size": 0, 
  "aggs": {
    "player": {
      "terms": {
        "field": "id.keyword",
        "size": 10000000
      },
      "aggs": {
        "min-level": {
          "min": {
            "field": "lvl.keyword"
          }
        }
      }
    }
  }
}

Which gives a result like:

  "aggregations": {
    "player": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "123",
          "doc_count": 24,
          "min-level": {
            "value": 2
          }
        },
        {
          "key": "456",
          "doc_count": 14,
          "min-level": {
            "value": 1
          }
        },
        {
          "key": "678",
          "doc_count": 14,
          "min-level": {
            "value": 1
          }
        },
        {
          "key": "987",
          "doc_count": 12,
          "min-level": {
            "value": 4
          }
        },
        {
          "key": "476",
          "doc_count": 10,
          "min-level": {
            "value": 5
          }
        }
      ]
    }
  }

What I need is a pivot on that result so that I can get an output of how many players have the same "min-level".

"min-level", "# of players"
1, 2
2, 1
4, 1
5, 1

Is that possible with a pipeline aggregation or what are my options to acquire that data from the initial aggregation?

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