Bucket sort in composite aggregation?

五迷三道 提交于 2020-12-13 03:15:43

问题


How can I do Bucket Sort in composite Aggregation?

I need to do Composite Aggregation with Bucket sort.

I have tried Sort with aggregation. I have tried composite aggregation.


回答1:


I think this question, is in continuation to your previous question, so considered the same use case

You need to use Bucket sort aggregation that is a parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation. And please refer to this documentation on composite aggregation to know more about this.

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{
  "mappings":{
    "properties":{
      "user":{
        "type":"keyword"
      },
      "date":{
        "type":"date"
      }
    }
  }
}

Index Data:

{
  "date": "2015-01-01",
  "user": "user1"
}
{
  "date": "2014-01-01",
  "user": "user2"
}
{
  "date": "2015-01-11",
  "user": "user3"
}

Search Query:

The size parameter can be set to define how many composite buckets should be returned. Each composite bucket is considered as a single bucket, so setting a size of 10 will return the first 10 composite buckets created from the values source. The response contains the values for each composite bucket in an array containing the values extracted from each value source. Defaults to 10.

{
  "size": 0,
  "aggs": {
    "my_buckets": {
      "composite": {
       "size": 3,               <-- note this
        "sources": [
          {
            "product": {
              "terms": {
                "field": "user"
              }
            }
          }
        ]
      },
      "aggs": {
        "mySort": {
          "bucket_sort": {
            "sort": [
              {
                "sort_user": {
                  "order": "desc"
                }
              }
            ]
          }
        },
        "sort_user": {
          "min": {
            "field": "date"
          }
        }
      }
    }
  }
}

Search Result:

"aggregations": {
    "my_buckets": {
      "after_key": {
        "product": "user3"
      },
      "buckets": [
        {
          "key": {
            "product": "user3"
          },
          "doc_count": 1,
          "sort_user": {
            "value": 1.4209344E12,
            "value_as_string": "2015-01-11T00:00:00.000Z"
          }
        },
        {
          "key": {
            "product": "user1"
          },
          "doc_count": 1,
          "sort_user": {
            "value": 1.4200704E12,
            "value_as_string": "2015-01-01T00:00:00.000Z"
          }
        },
        {
          "key": {
            "product": "user2"
          },
          "doc_count": 1,
          "sort_user": {
            "value": 1.3885344E12,
            "value_as_string": "2014-01-01T00:00:00.000Z"
          }
        }
      ]
    }


来源:https://stackoverflow.com/questions/64590806/bucket-sort-in-composite-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!