桶 aggregation/bucket_script 计算

bucket aggregation/bucket_script computation

如何通过bucket_script使用桶字段应用计算?更重要的是,我想了解如何聚合不同的结果。

例如,下面是示例查询和响应。

我正在寻找的是将以下内容汇总到两个字段中:

  1. 所有桶的总和 dist.value 来自例如回应 (1+2=3)
  2. 来自例如响应 (1x10)+(2x20)=50
  3. 的所有桶的总和(dist.value x 键)

查询

{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "field": "value"
                    }
                }
            ]
        }
    },
    "aggs":{
        "sales_summary":{
            "terms":{
                "field":"qty",
                "size":"100"
            },
            "aggs":{
                "dist":{
                    "cardinality":{
                        "field":"somekey.keyword"
                    }
                }
            }
        }
    }
}

查询结果:

{
    "aggregations": {
        "sales_summary": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 10,
                    "doc_count": 100,
                    "dist": {
                        "value": 1
                    }
                },
                {
                    "key": 20,
                    "doc_count": 200,
                    "dist": {
                        "value": 2
                    }
                }
            ]
        }
    }
}

您需要使用 sum bucket aggregation,这是一个管道聚合,用于查找所有桶中基数聚合的响应总和。

搜索查询所有桶的总和 dist.value 来自例如响应 (1+2=3):

POST idxtest1/_search
{
  "size": 0,
  "aggs": {
    "sales_summary": {
      "terms": {
        "field": "qty",
        "size": "100"
      },
      "aggs": {
        "dist": {
          "cardinality": {
            "field": "pageview"
          }
        }
      }
    },
    "sum_buckets": {
      "sum_bucket": {
        "buckets_path": "sales_summary>dist"
      }
    }
  }
}

搜索响应:

"aggregations" : {
    "sales_summary" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 10,
          "doc_count" : 3,
          "dist" : {
            "value" : 2
          }
        },
        {
          "key" : 20,
          "doc_count" : 3,
          "dist" : {
            "value" : 3
          }
        }
      ]
    },
    "sum_buckets" : {
      "value" : 5.0
    }
  }

对于第二个需求,需要先修改bucket aggregation response中value的response,使用bucket script aggregation,然后使用修改后的value对其进行bucket sum聚合

从例如响应 (1x10)+(2x20)=50

中搜索查询所有桶的总和(dist.value x 键)
POST idxtest1/_search
{
  "size": 0,
  "aggs": {
    "sales_summary": {
      "terms": {
        "field": "qty",
        "size": "100"
      },
      "aggs": {
        "dist": {
          "cardinality": {
            "field": "pageview"
          }
        },
        "format-value-agg": {
          "bucket_script": {
            "buckets_path": {
              "newValue": "dist"
            },
            "script": "params.newValue * 10"
          }
        }
      }
    },
    "sum_buckets": {
      "sum_bucket": {
        "buckets_path": "sales_summary>format-value-agg"
      }
    }
  }
}

搜索响应:

"aggregations" : {
    "sales_summary" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 10,
          "doc_count" : 3,
          "dist" : {
            "value" : 2
          },
          "format-value-agg" : {
            "value" : 20.0
          }
        },
        {
          "key" : 20,
          "doc_count" : 3,
          "dist" : {
            "value" : 3
          },
          "format-value-agg" : {
            "value" : 30.0
          }
        }
      ]
    },
    "sum_buckets" : {
      "value" : 50.0
    }
  }