Count buckets returned by sub aggregation

前端 未结 1 1213
走了就别回头了
走了就别回头了 2021-01-27 00:58

I need to count the number of buckets from a result set returned by pipe aggregation. Problem is that my query that is using script selector here:

POST visitor_c         


        
相关标签:
1条回答
  • 2021-01-27 01:13

    You can make use of Stats Bucket Aggregation to get the count of buckets.

    Below is how your query would be.

    Aggregation Query:

    POST visitor_carts/_search
    {
      "size": 0,
      "aggs": {
        "visitors": {
          "terms": {
            "field" : "visitor_id"
          },
          "aggs": {
            "one_purchase": {
              "bucket_selector": {
                "buckets_path": {
                  "nb_purchases": "_count"
                },
                "script": "params.nb_purchases == 3"
              }
            }
          }
        },
        "mybucketcount":{
          "stats_bucket": {
            "buckets_path":"visitors._count"
          }
        }
      }
    }
    

    Aggregation Result:

    {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 8,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "visitors": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "2",
              "doc_count": 3
            },
            {
              "key": "3",
              "doc_count": 3
            }
          ]
        },
        "mybucketcount": {
          "count": 2,              <---- This is the count you are looking for
          "min": 3,
          "max": 3,
          "avg": 3,
          "sum": 6
        }
      }
    }
    

    Let me know if this helps!

    0 讨论(0)
提交回复
热议问题