# Access global document count from inside bucket script

**URL:** <https://discuss.elastic.co/t/access-global-document-count-from-inside-bucket-script/324040>\
**Category:** Elasticsearch\
**Tags:** kql-kibana-query-language, eql-elastic-query-language\
**Created:** [January 26, 2023, 3:48pm UTC](https://discuss.elastic.co/t/access-global-document-count-from-inside-bucket-script/324040 "2023-01-26T15:48:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpax51](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bpax51/32/116217_2.png) [@bpax51](https://discuss.elastic.co/u/bpax51)\
**Post date:** [January 26, 2023, 3:48pm UTC](https://discuss.elastic.co/t/access-global-document-count-from-inside-bucket-script/324040/1 "2023-01-26T15:48:42Z")

</div>

Hello guys,  
I'm new to Elasticsearch and already having fun with aggregations but I'm facing an issue that I couldn't figure out.  
I have the following query, that will search my index within a date range, give me a list of customers and the error ratio (number\_of\_failed\_requests/tot\_number\_of\_requests).

```auto
GET my_index/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "doc.Event_datetime": {
              "gte": "2023-01-25T21:41:00.000Z"
            }
          }
        }
      ]
    }
  },
  "track_total_hits": true,
  "aggs": {
    "number_of_documents": {
      "value_count": {
        "field": "doc.Event-Timestamp"
      }
    },
    "per_customer": {
      "terms": {
        "field": "doc.customer_name.keyword",
        "size": 1000
      },
      "aggs": {
        "total_requests": {
          "value_count": {
            "field": "doc.Event-Timestamp"
          }
        },
        "error": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "doc.status.keyword": {
                      "value": "Request accepted"
                    }
                  }
                }
              ]
            }
          }
        },
        "error_ratio": {
          "bucket_script": {
            "buckets_path": {
              "total": "requests_count",
              "total_errors": "error._count"
            },
            script": "params.total_errors/(params.total)*100"
          }
        }
      }
    }
  }
}

```

As you can see here my ratio is calculated based on tot\_number\_of\_requests that each customer has generated. what I actually would like to do is have my ratio calculated based on total off all requested that were generated within the period.

ratio = total\_number\_of\_errors\_generated\_by\_customer/number\_of\_documents

My issue is that I'm enable to access the value of number\_of\_documents within my bucket script, anyway I can do that ?

---

<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:** [February 23, 2023, 3:49pm UTC](https://discuss.elastic.co/t/access-global-document-count-from-inside-bucket-script/324040/2 "2023-02-23T15:49:32Z")

</div>

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