'Order by doc_count in composite aggregation (or suitable alternatives)

I have a search like the following

{
    "size": 0,
    "query": { "...": "..." },
    "_source": false,
    "aggregations": {
        "agg1": { "...": "..." },
        "agg2": { "...": "..." }
    }
}

where agg* is composite aggregation of the kind

"agg1" : {
    "composite": {
        "size": 300,
        "sources": [
            {
                "field1": {
                    "terms": {
                        "field": "field1.keyword",
                        "missing_bucket": true,
                    }
                }
            },
            {
                "field2": {
                    "terms": {
                        "field": "field2.keyword",
                        "missing_bucket": true,
                        "order": "asc"
                    }
                }
            }
        ]
    },
    "aggregations": {
        "field3": {
            "filter": { "term": { "field3.keyword": "xyz" } }
        }
    }
}

I want to order by doc_count of the buckets as I don't need all the buckets, but just the top n, like what happens in some Kibana visualizations. From the documentation of composite aggregations it doesn't seem possible to order the results similarly at what happens with terms aggregations. Is there a workaround or alternative queries to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source