Aggregation: Order by bucket count descending

If I use the following aggregation as an example:

    {
      "aggs" : {
        "actors" : {
          "terms" : {
            "field" : "actors"
          },
          "aggs" : {
            "costars" : {
              "terms" : {
                "field" : "actors"
              }
            }
          }
        }
      }
    }

and the results look something like this:

    "aggregations": {
      "actors": {
        "buckets": [
          {
            "key": "Harvey Keitel",
            "doc_count": 1,
            "costars": {
              "buckets": [
                {
                  "key": "Steve Buscemi",
                  "doc_count": 1
                }, {
                  "key": "Uma Thurman",
                  "doc_count": 1
                }, {
                  "key": "Bruce Willis",
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }

Is there any way to sort by the Actors with the most Costar bucket counts?

The ‘cardinality’ aggregation can be used to count the costars and you can order by that value. Be careful if you have many stars though because cardinality aggregations can add up in memory costs when you are computing lots of them for a lot of different parent entities. There are controls that trade space for accuracy.

1 Like

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