0

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"
          }
        }
      }
    }
  }
}

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.