# Average over buckets doc\_count

**URL:** https://discuss.elastic.co/t/average-over-buckets-doc-count/18355
**Category:** Elasticsearch
**Created:** [June 26, 2014, 5:10pm UTC](https://discuss.elastic.co/t/average-over-buckets-doc-count/18355 "2014-06-26T17:10:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Demetrius\_Nunes](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Demetrius\_Nunes](https://discuss.elastic.co/u/Demetrius_Nunes)
#### Post date: [June 26, 2014, 5:10pm UTC](https://discuss.elastic.co/t/average-over-buckets-doc-count/18355/1 "2014-06-26T17:10:38Z")

</div>

Hi there,

Is there a way to calculate the average over the doc\_count result of a  
bucket aggregation?

For instance, I have this aggregation query:

GET channel/Subscription/\_search  
{  
"size": 1,  
"aggs": {  
"SubscriptionsPerUser": {  
"terms": {  
"field": "UserId",  
"min\_doc\_count": 0,  
"size": 1000  
}  
}  
}  
}

I would like to calculate the average doc\_count for each resulting bucket,  
which would give me the average subscriptions/user metric.

Thanks,  
Demetrius

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6d15e905-ec85-462f-838f-a41e77795786%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6d15e905-ec85-462f-838f-a41e77795786%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Demetrius\_Nunes](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Demetrius\_Nunes](https://discuss.elastic.co/u/Demetrius_Nunes)
#### Post date: [June 26, 2014, 5:40pm UTC](https://discuss.elastic.co/t/average-over-buckets-doc-count/18355/2 "2014-06-26T17:40:35Z")

</div>

In other words, I think it would be very useful if we could nest metric  
aggregations, so a very straight syntax for the use case I described would  
be:

GET channel/Subscription/\_search  
{  
"size": 1,  
"aggs": {  
"AvgSubsPerUser": {  
"avg": { "field": "$SubscriptionsPerUser.doc\_count" },  
"aggs": {  
"SubscriptionsPerUser": {  
"terms": {  
"field": "UserId",  
"min\_doc\_count": 0,  
"size": 1000  
}  
}  
}  
}  
}  
}

On Thursday, June 26, 2014 2:10:38 PM UTC-3, Demetrius Nunes wrote:

> Hi there,
> 
> Is there a way to calculate the average over the doc\_count result of a  
> bucket aggregation?
> 
> For instance, I have this aggregation query:
> 
> GET channel/Subscription/\_search  
> {  
> "size": 1,  
> "aggs": {  
> "SubscriptionsPerUser": {  
> "terms": {  
> "field": "UserId",  
> "min\_doc\_count": 0,  
> "size": 1000  
> }  
> }  
> }  
> }
> 
> I would like to calculate the average doc\_count for each resulting bucket,  
> which would give me the average subscriptions/user metric.
> 
> Thanks,  
> Demetrius

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f58c358a-c70f-49c7-a80c-624767e2674b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f58c358a-c70f-49c7-a80c-624767e2674b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:19am UTC](https://discuss.elastic.co/t/average-over-buckets-doc-count/18355/3 "2017-07-06T01:19:27Z")

</div>


