0

I am exploring the ease of querying and aggregating the data using elasticsearch. But i am not able to pivot and aggregate the data in a single query as below:

Considering the data:

enter image description here

Is there a way to query the below result that pivots and aggregates the value as below:

enter image description here

Required Result:

{  
   {  
      "A":a1,
      "B":b1,
      "Value":3
   },
   {  
      "A":a1,
      "B":b2,
      "Value":3
   },
   {  
      "A":a2,
      "B":b2,
      "Value":4
   },
   {  
      "A":a1,
      "B":b3,
      "Value":11
   }
}

1 Answer 1

1

Yes, you can nest two terms aggregations for A and B, like this, and you'll get exactly the results you expect:

{
  "size": 0,
  "aggs": {
    "A": {
      "terms": {
        "field": "A"
      },
      "aggs": {
        "B": {
          "terms": {
            "field": "B"
          },
          "aggs": {
            "value_sum": {
              "sum": {
                "field": "Value1"
              }
            }
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Val. It worked exactly. Just had to add a label to the Sum.

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.