How to find difference between aggregate average and individual values

I have a field "y" in the Elasticsearch index and I would like to find sum(y-y.avg) using aggregations for each person in my index. I am able to calculate y.avg for a person using the below query but need help in calculating sum(y-y.avg).

GET credit_score_transform_v1/_search
{
  "query": {
    "match": {
      "person": "joy"
    }
  },
  "size": 0, 
  "aggs": {
      "avg_y": {
        "avg": {
          "field": "y"
        }
      },
      "y-y.bar": {
        "bucket_script": {
          "buckets_path": {
            "y_bar": "avg_y.value"
          },
          "script": {
            "source": "params.y_bar"
          }
        }
      }
  }
}

I am able to access "y.avg" using the bucket path and need help in calculating the sum(y-y.avg).

thanks for the help and support.

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