# Composite aggregation buckets total count

**URL:** <https://discuss.elastic.co/t/composite-aggregation-buckets-total-count/137156>\
**Category:** Elasticsearch\
**Created:** [June 24, 2018, 5:03pm UTC](https://discuss.elastic.co/t/composite-aggregation-buckets-total-count/137156 "2018-06-24T17:03:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gogua](https://avatars.discourse-cdn.com/v4/letter/g/8491ac/32.png) [@gogua](https://discuss.elastic.co/u/gogua)\
**Post date:** [June 24, 2018, 5:03pm UTC](https://discuss.elastic.co/t/composite-aggregation-buckets-total-count/137156/1 "2018-06-24T17:03:05Z")

</div>

hi,

how to count buckets total, when i have the following ES query

```
"size": 0,
"aggs": {
    "buckets": {
        "composite": {
            "size": 20,
            "sources": [
                {"age": {"terms": {"field": "age"}}},
                {"weight": {"terms": {"field": "weight"}}},
            ]
        },
        "aggs": {
            "Max Wheight": {
                "max": {
                    "field": "weight"
                }
            },
        }
    },
}

```

or such as

```
{
	"query": {
	    "query_string": {
	        "query": "age:>=24"
	    }
	},
	"size": 0,
	"aggs": {
	    "group_by_pni": {
	        "terms": {
	            "field": "pni"
	        },
	        "aggs": {
	            "group_by_code": {
	                "terms": {
	                    "field": "code.keyword"
	                },
	                "aggs": {
	                    "group_by_age": {
	                        "terms": {
	                            "field": "age"
	                        },
	                        "aggs": {
	                            "counts age": {
	                                "value_count": {
	                                    "field": "age"
	                                }
	                            },
	                            "max_age": {
	                                "max": {
	                                    "field": "age"
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        }
	    },
	}
}

```

can you help me? thanks in advance

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [June 26, 2018, 6:57pm UTC](https://discuss.elastic.co/t/composite-aggregation-buckets-total-count/137156/2 "2018-06-26T18:57:16Z")

</div>

I don't believe there's a way to get the total bucket count, because the `composite` agg doesn't know how many buckets there are until it has paged through them all.

I think the best you can do is keep a running counter client-side and increment it with each page that you receive.

That's the trade-off that `composite` pays; memory-friendly, streaming collection of results at the expense of multiple round-trips and not knowing the buckets up-front.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 24, 2018, 6:57pm UTC](https://discuss.elastic.co/t/composite-aggregation-buckets-total-count/137156/3 "2018-07-24T18:57:20Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
