# How to calculate percentage of a field over all documents present in index

**URL:** <https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544>\
**Category:** Elasticsearch\
**Created:** [May 29, 2023, 3:34am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544 "2023-05-29T03:34:48Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 6, 2023, 4:28am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544/4 "2023-06-06T04:28:23Z")

</div>

You can use [Terms aggregation | Elasticsearch Guide [8.8] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) to create the first lot of buckets.

From there you have two options;

1. calcuate the percentage outside Elasticsearch
2. use something like this [Using doc\_count to calculate percentage after aggregation - #2 by spinscale](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/2)

Elasticsearch doesn't provide this sort of output itself.

---

_[View the full topic](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544)._
