桶脚本聚合

编辑

一个父管道聚合,它执行一个脚本,该脚本可以在父多桶聚合中对指定的指标执行每个桶的计算。指定的指标必须是数值型,并且脚本必须返回一个数值。

语法

编辑

一个 bucket_script 聚合单独来看是这样的:

{
  "bucket_script": {
    "buckets_path": {
      "my_var1": "the_sum",                     
      "my_var2": "the_value_count"
    },
    "script": "params.my_var1 / params.my_var2"
  }
}

这里,my_var1 是此桶路径的变量名,用于脚本中,the_sum 是用于该变量的指标路径。

表 56. bucket_script 参数

参数名 描述 是否必需 默认值

script

此聚合要运行的脚本。脚本可以是内联的、文件中的或索引的。(有关详细信息,请参阅脚本

是否必需

buckets_path

脚本变量及其关联的桶路径的映射,我们希望将该路径用于变量(有关详细信息,请参阅 buckets_path 语法

是否必需

gap_policy

当数据中发现间隙时要应用的策略(有关详细信息,请参阅处理数据中的间隙

可选

skip

format

用于输出值的 DecimalFormat 模式。如果指定,格式化的值将返回到聚合的 value_as_string 属性中

可选

null

以下代码片段计算每月 T 恤衫销售额与总销售额的比例百分比

resp = client.search(
    index="sales",
    size=0,
    aggs={
        "sales_per_month": {
            "date_histogram": {
                "field": "date",
                "calendar_interval": "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "t-shirts": {
                    "filter": {
                        "term": {
                            "type": "t-shirt"
                        }
                    },
                    "aggs": {
                        "sales": {
                            "sum": {
                                "field": "price"
                            }
                        }
                    }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                            "tShirtSales": "t-shirts>sales",
                            "totalSales": "total_sales"
                        },
                        "script": "params.tShirtSales / params.totalSales * 100"
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'sales',
  body: {
    size: 0,
    aggregations: {
      sales_per_month: {
        date_histogram: {
          field: 'date',
          calendar_interval: 'month'
        },
        aggregations: {
          total_sales: {
            sum: {
              field: 'price'
            }
          },
          "t-shirts": {
            filter: {
              term: {
                type: 't-shirt'
              }
            },
            aggregations: {
              sales: {
                sum: {
                  field: 'price'
                }
              }
            }
          },
          "t-shirt-percentage": {
            bucket_script: {
              buckets_path: {
                "tShirtSales": 't-shirts>sales',
                "totalSales": 'total_sales'
              },
              script: 'params.tShirtSales / params.totalSales * 100'
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  index: "sales",
  size: 0,
  aggs: {
    sales_per_month: {
      date_histogram: {
        field: "date",
        calendar_interval: "month",
      },
      aggs: {
        total_sales: {
          sum: {
            field: "price",
          },
        },
        "t-shirts": {
          filter: {
            term: {
              type: "t-shirt",
            },
          },
          aggs: {
            sales: {
              sum: {
                field: "price",
              },
            },
          },
        },
        "t-shirt-percentage": {
          bucket_script: {
            buckets_path: {
              tShirtSales: "t-shirts>sales",
              totalSales: "total_sales",
            },
            script: "params.tShirtSales / params.totalSales * 100",
          },
        },
      },
    },
  },
});
console.log(response);
POST /sales/_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "month"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "price"
          }
        },
        "t-shirts": {
          "filter": {
            "term": {
              "type": "t-shirt"
            }
          },
          "aggs": {
            "sales": {
              "sum": {
                "field": "price"
              }
            }
          }
        },
        "t-shirt-percentage": {
          "bucket_script": {
            "buckets_path": {
              "tShirtSales": "t-shirts>sales",
              "totalSales": "total_sales"
            },
            "script": "params.tShirtSales / params.totalSales * 100"
          }
        }
      }
    }
  }
}

以下可能是响应

{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "total_sales": {
                   "value": 550.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 200.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 36.36363636363637
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "total_sales": {
                   "value": 60.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 10.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 16.666666666666664
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "total_sales": {
                   "value": 375.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 175.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 46.666666666666664
               }
            }
         ]
      }
   }
}