title: Count buckets returned by sub aggregation
body: <p>I need to count the number of buckets from a result set returned by <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-aggregations-pipeline.html" rel="nofollow noreferrer">pipe aggregation</a>. Problem is that my query that is using <a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-aggregations-pipeline-bucket-script-aggregation.html" rel="nofollow noreferrer">script selector</a> here:</p>
  
  <pre><code>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"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>returns something like that:</p>
  
  <pre><code>{
    "took" : 0,
    "timed_out" : false,
    "_shards" : {
      "total" : 1,
      "successful" : 1,
      "skipped" : 0,
      "failed" : 0
    },
    "hits" : {
      "total" : 5,
      "max_score" : 0.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
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>Under the <code>buckets</code> key I can see a list of visitors that meet my condition (every visitor identified by <code>visitor_id</code> must have exactly three documents in the <code>visitor_carts</code> index) but that is not very helpful because it should handle instead hundreds of thousands of visitors. I am using PHP to handle the results, theoretically it could count the result set but with a large volume of visitors it feels like not the best idea. Is there a way to just output the count of valid buckets next to <code>doc_count_error_upper_bound</code> and <code>sum_other_doc_count</code>? It is a little odd that there is no <code>bucket_count</code> included in the aggregation stats as it seems to be quite useful.</p>
  
  <p>Or maybe this can be done in a different way? This question is a follow-up for this one: <a href="https://stackoverflow.com/questions/56112844/get-user-count-that-made-a-specific-number-of-purchases-in-elasticsearch/56113760?noredirect=1#comment98893453_56113760">Get user count that made a specific number of purchases</a></p>
  
  <p>Here is my <code>visitor_carts</code> mapping:</p>
  
  <pre><code>{
    "mapping": {
      "_doc": {
        "dynamic": "false",
        "properties": {
          "created_dt": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
          },
          "order_id": {
            "type": "keyword"
          },
          "visitor_id": {
            "type": "keyword"
          }
        }
      }
    }
  }
  </code></pre>