JJstyle

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

  •  
  •   JJstyle · Feb 4, 2021 · 5688 views
    This topic created in 1950 days ago, the information mentioned may be changed or developed.

    原问题: 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
    );
    
    Supplement 1  ·  Feb 5, 2021
    可能我的表达有点问题,您可以看我的 SQL 示例。
    5 replies    2021-02-07 12:57:28 +08:00
    weofuh
        1
    weofuh  
       Feb 5, 2021
    {
    "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
       Feb 5, 2021
    @weofuh 你这个是过滤出来 count>10 的 buckets,但是我想要的是这些 buckets 的数量
    rykinia
        3
    rykinia  
       Feb 5, 2021
    ```
    {
    "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
       Feb 5, 2021
    @rykinia 不是的,我要的是所有 doc_count>10 的 buckets 的数量,而不是 buckets 的集合
    tairan2006
        5
    tairan2006  
       Feb 7, 2021
    老哥,你不知道`_sql/translate`这个工具么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   952 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 21:04 · PVG 05:04 · LAX 14:04 · JFK 17:04
    ♥ Do have faith in what you're doing.