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
You can make use of Stats Bucket Aggregation to get the count of buckets.
Below is how your query would be.
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"
}
}
}
}
{
"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!