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

<div class="post-metadata">

**Author:** ![surjray](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@surjray](https://discuss.elastic.co/u/surjray)\
**Post date:** [August 3, 2020, 6:18pm UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/1 "2020-08-03T18:18:57Z")

</div>

Dear all,  
I am currently attempting to use the doc\_count field (result of aggregation operation: aggregations.aggregation\_name.buckets -\> doc\_count for each bucket) to calculate some percentages, namely pass and fail for test cases run. After running terms aggregation I am getting the necessary buckets, e.g.  
"key" : "PASS",  
"doc\_count" : 100,  
and  
"key" : "FAIL",  
"doc\_count" : 10,  
Is there a way to extract the doc\_count in the above results so that then I can use a bucket\_script to calculate the percent value please ?  
Thanking you.

---

<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)"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![surjray](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@surjray](https://discuss.elastic.co/u/surjray)\
**Post date:** [August 4, 2020, 4:38pm UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/3 "2020-08-04T16:38:22Z")

</div>

Thank you so much @spinscale for your advice. Your code snippet helps me understand how to extract the aggregation counts and use them in a script. The issue I am facing is when I try to use this method for my use-case:  
I am getting the following exception related to the level of aggregation allowed:  
"Invalid pipeline aggregation named [pass\_fail\_relation] of type [bucket\_script]. Only sibling pipeline aggregations are allowed at the top level"

The way I am trying to use your method is the following:

```auto
GET /ls-qat-*/_search
{
  "query": { 
    "bool": {
      "must": [
        {
          "match": { "fields.product.application.keyword": "some_application" }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "now-7d/d",  
              "lte": "now/d"
            }
          }
        }
      ]
    }
  },
  "aggs" : {
      "testcase_status_aggregation" : {
          "terms" : { "field" : "testcase.status.keyword", "size": 3, "show_term_doc_count_error": true }
    },
    
    "pass_fail_relation": {
          "bucket_script": {
            "buckets_path": {
              "pass": "testcase_status_aggregation['PASS']>_count",
              "fail": "testcase_status_aggregation['FAIL']>_count"
            },
            "script": "params.fail / (params.pass + params.fail) * 100"
          }
        }
    
  },
  "size" : 0
}

```

test.status.keyword contains either "PASS" or "FAIL" for a testcase.  
If you can please advise how to alleviate this issue.  
Thanking you.

---

<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 12, 2020, 8:14am UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/4 "2020-08-12T08:14:57Z")

</div>

Please create a fully reproducible example including index creation with mapping, document indexing and query, otherwise this is really hard to reproduce.

---

<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:** [September 9, 2020, 8:15am UTC](https://discuss.elastic.co/t/using-doc-count-to-calculate-percentage-after-aggregation/243611/5 "2020-09-09T08:15:15Z")

</div>

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