Sorting on an aggregation in Elasticsearch

To sort on an aggregation in Elasticsearch, you can use the "order" parameter in the aggregation definition.

Here is an example using the "terms" aggregation:

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "my_agg": {
      "terms": {
        "field": "my_field",
        "order": {
          "my_metric": "desc"
        }
      },
      "aggs": {
        "my_metric": {
          "avg": {
            "field": "my_numeric_field"
          }
        }
      }
    }
  }
}

In this example, the "terms" aggregation groups documents by the "my_field" field and calculates the average of the "my_numeric_field" field for each group using the "avg" aggregation. The "order" parameter sorts the groups in descending order based on the average calculated by the "my_metric" aggregation.

You can adjust the sorting order by changing the "desc" parameter to "asc" if you want to sort in ascending order. You can also sort based on other metrics or fields by changing the value of the "my_metric" parameter in the "order" parameter.

댓글

이 블로그의 인기 게시물

What are 5 creative things I could do with my kids' art, 아이와 함께하는 창의적 놀이

how to make inactive kafka topic, 카프카 토픽 비활성화

Screenshot of a web page using Puppeteer with a specific browser width, 웹페이지 스크린샷

리눅스의 부팅과정 (프로세스, 서비스 관리)

To change the ESE index in Fluent Bit

Consume a topic from kafka by Python

ssh-copy-id in ansible

패션 하의, Pants, Skirts, Shorts, Skorts

ftplib.error_perm: 550 Permission denied