# Using doc\_count to calculate percentage after aggregation

**URL:** <https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611>\
**Category:** Elasticsearch\
**Created:** [August 3, 2020, 6:18pm UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611 "2020-08-03T18:18:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 4, 2020, 9:45am UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/2 "2020-08-04T09:45:59Z")

</div>

try this:

```auto
DELETE test

PUT test/_bulk
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "PASS" }
{ "index" : {}}
{ "result": "FAIL" }

GET test/_search
{
  "size": 0,
  "aggs": {
    "filters_agg": {
      "filters": {
        "filters": {
          "first": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "by_result": {
          "terms": {
            "field": "result.keyword",
            "size": 10
          }
        },
        "pass_fail_relation": {
          "bucket_script": {
            "buckets_path": {
              "pass": "by_result['PASS']>_count",
              "fail": "by_result['FAIL']>_count"
            },
            "script": "params.fail/(params.pass + params.fail)"
          }
        }
      }
    }
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611)._
