# 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:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Charkha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_charkha/32/117841_2.png) [@Amit\_Charkha](https://discuss.elastic.co/u/Amit_Charkha)\
**Post date:** [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/1 "2023-05-29T03:34:48Z")

</div>

how to calculate percentage of a field log\_count over all documents present in index.

---

<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:** [May 29, 2023, 7:49am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544/2 "2023-05-29T07:49:38Z")

</div>

Welcome to our community! 😃

Do you mean a percentage of documents that have it over those that don't?

---

<div class="post-metadata">

**Author:** ![Amit\_Charkha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_charkha/32/117841_2.png) [@Amit\_Charkha](https://discuss.elastic.co/u/Amit_Charkha)\
**Post date:** [May 29, 2023, 9:18am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544/3 "2023-05-29T09:18:38Z")

</div>

Let me explain this.  
For example  
we have one field called log\_type. and in that field we have 12 different values like demo,demo2....demo12.  
and suppose each log\_type have 1000 documents.  
Now, I want to calculate document count percentage for log\_type=demo1.  
so the value will be like demo have 1000 documents and all log\_types including demo1 have 12000 .

so the calculation will be like (demo1/all\_log\_types)\*100= 8.3333

---

<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.

---

<div class="post-metadata">

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

</div>

@Amit_Charkha

A Lens Pie Chart or Table will do this for you pretty easy

 ![Screenshot 2023-06-05 at 9.45.26 PM](https://us1.discourse-cdn.com/elastic/original/3X/f/3/f332bbed7a0ddd52d6eec97b8c869a5c5c345062.jpeg)

 ![Screenshot 2023-06-05 at 9.51.05 PM](https://us1.discourse-cdn.com/elastic/original/3X/b/4/b449e1f9cb8371c46f5e91ee852d0505a0a2e0f7.jpeg)

---

<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:54am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544/6 "2023-06-06T04:54:48Z")

</div>

I think the issue with those is Kibana calculates the percentages post Elasticsearch query, whereas they want the response from Elasticsearch to show the breakdown.

---

<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 4, 2023, 4:55am UTC](https://discuss.elastic.co/t/how-to-calculate-percentage-of-a-field-over-all-documents-present-in-index/334544/7 "2023-07-04T04:55:30Z")

</div>

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