V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JJstyle
V2EX  ›  Elasticsearch

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

  •  
  •   JJstyle · 2021-02-04 17:59:05 +08:00 · 1792 次点击
    这是一个创建于 1148 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原问题: 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
    );
    
    第 1 条附言  ·  2021-02-05 13:48:32 +08:00
    可能我的表达有点问题,您可以看我的 SQL 示例。
    5 条回复    2021-02-07 12:57:28 +08:00
    weofuh
        1
    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
        2
    JJstyle  
    OP
       2021-02-05 12:54:31 +08:00
    @weofuh 你这个是过滤出来 count>10 的 buckets,但是我想要的是这些 buckets 的数量
    rykinia
        3
    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
        4
    JJstyle  
    OP
       2021-02-05 13:46:44 +08:00
    @rykinia 不是的,我要的是所有 doc_count>10 的 buckets 的数量,而不是 buckets 的集合
    tairan2006
        5
    tairan2006  
       2021-02-07 12:57:28 +08:00
    老哥,你不知道`_sql/translate`这个工具么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2902 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:18 · PVG 23:18 · LAX 08:18 · JFK 11:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.