1

I'm writing an elastic search query the payload looks like

"userData": [ { "subject1":1234, "subject2":678 } ]

I have written a query which works perfectly fine if I want to get the avg of a single field

Query :

{ 
  "aggs": { 
    "student_data": { 
      "date_histogram": { 
        "field":"@timestamp",
        "calendar_interval":"minute"
      },
      "aggs": { 
        "marks_avg": { 
          "avg": { 
            "field":"subject1"
          }
        }
      }
    }
  }
}

What I want is to get the average of subject2 also.

somewhat like :

"avg": { 
    "field":"subject1",
    "field":"subject2"
 }

1 Answer 1

1

Query using multiple aggregations should work:

{ 
  "aggs": { 
    "student_data": { 
      "date_histogram": { 
        "field":"@timestamp",
        "calendar_interval":"minute"
      },
      "aggs": { 
        "subject1_avg": { 
          "avg": { 
            "field":"subject1"
          }
        },
        "subject2_avg": { 
          "avg": { 
            "field":"subject2"
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

what if I needed the subject1 array also in the output

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.