SRIRAM RAMACHANDRAN
SRIRAM RAMACHANDRAN

Reputation: 337

How to sum up multiple columns in elastic search

How to sum the 4 columns in elastic search. Example:

 A B C 
 1 1 1 
 1 2 2

Sum of A = 2, Sum of B = 3, Sum of C = 3 and Sum of total(A,B,C) = 8.

How to get the sum? I have done an aggregation query but it is throwing me Unexpected token START_ARRAY

 {
  "aggs": {
    "total_count": {
      "sum": {
        "base_count": {
          "sum": {
            "field": "cse_licenseactivated_base_count"
          }
        },
        "malware_count": {
          "sum": {
            "field": "cse_licenseactivated_malware_count"
          }
        },
        "threat_count": {
          "sum": {
            "field": "cse_licenseactivated_threat_count"
          }
        },
        "urlfiltering_count": {
          "sum": {
            "field": "cse_licenseactivated_urlfiltering_count"
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 2083

Answers (1)

Val
Val

Reputation: 217554

You need to use a script like this:

{
  "aggs": {
    "total_count": {
      "sum": {
         "source": "doc.cse_licenseactivated_base_count.value + doc.cse_licenseactivated_malware_count.value + doc.cse_licenseactivated_threat_count.value + doc.cse_licenseactivated_urlfiltering_count.value"
      }
    }
  }
} 

Or you can also sum up all those fields at indexing time and store the sum in another fields called cse_licenseactivated_total_count, which is a better way to do it

Upvotes: 1

Related Questions