桶在复合聚合中排序? [英] Bucket sort in composite aggregation?

查看:74
本文介绍了桶在复合聚合中排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I do Bucket Sort in composite Aggregation?

我需要使用Bucket排序进行复合聚合.

I need to do Composite Aggregation with Bucket sort.

我尝试使用聚合排序.我已经尝试过复合聚合.

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

推荐答案

我认为这个问题是您以前的

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

索引映射:

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

搜索查询:

尺寸参数可以设置为定义多少个复合桶应该退货.每个复合桶都被视为一个桶,因此将大小设置为10将返回前10个复合从值源创建的存储桶.响应中包含包含值的数组中每个复合存储桶的值从每个值源中提取.默认值为10.

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

这篇关于桶在复合聚合中排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆