How to apply mathematical operation on elastic search aggregations bucket data

2.1k views Asked by At

I am using elastic search 5.5.1. My requirement is to apply following formula : (doc_count*100/10) on below set of data :

{
  "took": 30,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_botId": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1,
          "doc_count": 9
        },
        {
          "key": 3,
          "doc_count": 4
        }
      ]
    }
  }
}

I should be able to find solution for each bucket items. I don't want to use Java SDK for this problem as requirement is to use only elastic search REST API.

1

There are 1 answers

0
Val On BEST ANSWER

You can use the bucket_script aggregation in order to run a script for each bucket:

  "aggs": {
    "group_by_botId": {
      "terms": {
        "field": "botId"
      },
      "aggs": {
        "count": {
          "bucket_script": {
            "buckets_path": {
              "doc_count" : "_count"
            },
            "script": "params.doc_count * 100 / 10"
          }
        }
      }
    }
  }