Elasticsearch 中的 terms 聚合,如何得到所有 buckets 的数量?

2021-02-04 17:59:05 +08:00
 JJstyle

原问题: https://stackoverflow.com/questions/66043206/how-to-get-the-buckets-count-from-terms-aggregation-in-elasticsearch

比如:得到所有订单数量大于 10 的用户总数,类似于 SQL 语句:

select count(1) from (
  select count(1) as cnt from orders group by userid having cnt >= 10
);
1828 次点击
所在节点    Elasticsearch
5 条回复
weofuh
2021-02-05 10:53:49 +08:00
{
"aggs": {
"user_order_cnt": {
"terms": {
"field": "userid"
},
"aggs": {
"count_bucket_selector": {
"bucket_selector": {
"buckets_path": {
"count" :"_count"
},
"script": "params.count>10"
}
}
}
}
}
}
JJstyle
2021-02-05 12:54:31 +08:00
@weofuh 你这个是过滤出来 count>10 的 buckets,但是我想要的是这些 buckets 的数量
rykinia
2021-02-05 13:32:49 +08:00
```
{
"aggs": {
"cnt": {
"terms": {
"field": "userid",
"size": 1000,
"min_doc_count": 10
}
}
},
"size": 0
}
```

结果:
```
"buckets" : [
{
"key" : "320581180",
"doc_count" : 20
},
{
"key" : "2207021711",
"doc_count" : 10
},
```

这个 doc_count 不就是数量吗
JJstyle
2021-02-05 13:46:44 +08:00
@rykinia 不是的,我要的是所有 doc_count>10 的 buckets 的数量,而不是 buckets 的集合
tairan2006
2021-02-07 12:57:28 +08:00
老哥,你不知道`_sql/translate`这个工具么

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/751336

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX