Search code examples
elasticsearchkibanakibana-4

how to get the total value of field in kibana 4.x


I need to get the total value of field in Kibana using script field.

e.g) if id field have values like 1, 2, 3, 4, 5

I am looking to sum up all the values of id, I am expecting the output is 15.

I need to achieve the below formula after getting total of each field.

lifetime=a-b-c-(d-e-f)*g

here a,b,c,d,e,f,g all are total of the each field values.

for more info please refer this question which is raised by me.


Solution

  • You can definitely use sum aggregations to get the sum of id, but to further equate your formula, you can take a look at pipeline aggregations to use the sum value for further calculations.

    Take a look at bucket script aggregation, with proper bucket path to sum aggregator you can achieve your solution.

    For sample documents

    {
      "a":100,
      "b":200,
      "c":400,
      "d":600
    }
    

    query

     {
       "size": 0,
       "aggs": {
          "result": {
             "terms": {"script":"'nice to have it here'"},
             "aggs": {
                "suma": {
                    "sum": {
                        "field": "a"
                    }
                },
                "sumb": {
                    "sum": {
                      "field": "b"
                    }
                },
                "sumc": {
                    "sum": {
                      "field": "c"
                    }
                },
                "equation": {
                    "bucket_script": {
                        "buckets_path": {
                            "suma": "suma",
                            "sumb": "sumb",
                            "sumc" : "sumc"
                        },
                        "script": "suma + sumb + 2*sumc"
                    }
                }
            }
          }
       }
    }
    

    Now you can surely add term filter on each sum agg to filter the summation for each sum aggregator.