共计 659 个字符,预计需要花费 2 分钟才能阅读完成。
有个需要,在 sql 中的计算形式是:
sum(cost) / 100 / sum(view_count) * 1000
统计出均匀千次展示均价,老本按分存储。
在 es 中须要聚合 cost 和 view_count 之后再运算。折腾了半天,记录下:
"aggs" => [
'total_cost_count' => [
'sum' => ['field' => 'cost']
],
'total_view_count' => [
'sum' => ['field' => 'view_count']
],
'(total_cost / total_view)' => [
'bucket_script' => [
'buckets_path' => [
'cost' => 'total_cost_count',
'view_count' => 'total_view_count',
],
'script' => [
'source' => "params.cost / params.unit / params.view_count * params.days",
'params' => [
'unit' => 100,
'days' => 1000,
]
]
]
]
],
调试过程中报错信息:
-
Only sibling pipeline aggregations are allowed at the top level
- 在顶层只容许兄弟管道聚合。也就是聚合运算只能在同一个桶内。
参考文档:https://www.elastic.co/guide/en/elasticsearch/painless/7.13/painless-bucket-script-agg-context.html
正文完
发表至: elasticsearch
2021-05-28