'how to order on doc count for terms aggregation within a composite aggregation?

I was trying the composite aggregation in elastic-search but found it weird that what i can do within a terms aggregation normally, isn't supported for terms within a composite aggregation!

See the query below :

GET _search
{
  "size": 0, 
  "query": {
"match_all": {}
  },"aggs": {
"compo": {
  "composite": {
    "sources": [
      {
        "terms_inside": {
          "terms": {
            "field": "result_type",
            "order": {
               "_count": "asc"  // not supported here!
            }
          }
        }
      }
    ]
  }
},
"just_terms" :{
  "terms": {
    "field": "result_type",
    "order": {
      "_count": "asc"  // supported here
    }
  }
}
}
}

Is the just the way it is, or is there a way to get sorted buckets on doc count with nested terms aggregation. I want to use paging and sorting on the terms aggregation.



Solution 1:[1]

It cannot be done as composite results paginate the aggregation and thus its function is designed to not fetch the count on all fields, only those in the first paginated set.

https://discuss.elastic.co/t/composite-aggregation-order-by/139563/5

Solution 2:[2]

You cannot aggregate on multiple terms and order on doc_count before elastic 7.12. On elasticsearch 7.12, you can use a multi terms aggregation.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Josiah
Solution 2 florianvazelle