3
votes

I want to aggregate documents and sort them by key length.

this query is aggregation query exclude 'ordering'.

{
  "aggs": {
    "per_terms": {
      "terms": {
        "field": "keyword"
      }
    }
  }
}

So I want to order them by the length of terms from 'keyword' field.

How can I do this?

1
did you ever find a solution for this? - 23tux

1 Answers

-1
votes

The simplest approach is to sort by the key alphabetically, beside the fact that its would be sorted by the a-b-c it would also be sorted by the key length:

{
  "aggs": {
    "per_terms": {
      "terms": {
        "field": "keyword",
        "order": {
          "_term": "asc"
        }
      }
    }
  }
}