Access global document count from inside bucket script

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

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 ?

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