复合聚合中的桶排序?

Bucket sort in composite aggregation?

如何在复合聚合中进行桶排序?

我需要使用桶排序进行复合聚合。

我试过使用聚合排序。 我试过复合聚合。

我认为这个问题是您之前 的延续,因此被视为相同的用例

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.

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

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

索引数据:

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

搜索查询:

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"
          }
        }
      }
    }
  }
}

搜索结果:

"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"
          }
        }
      ]
    }