Aggregation: Order by bucket count descending


#1

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?


(Mark Harwood) #2

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.