Aggregation of two fields in single query

Hello,

I have aמ index with documents that contain several fields, among these are:

grade
person_id

I wish to create an aggregation that count the number of documents found for each person_id and for each person sum or average the grade of the documents associated with it.

Any suggestions how to conduct this aggregation?

Thanks a lot!

Hey,

so you need an aggregation within an aggregation. It seems to me, that you first want to group by person_id, which means, you need a terms aggregation on that field. Within that aggregation you need an avg or sum aggregation on the grade field - and that should be it.

Would that work as a start or am I missing something in the requirements?

--Alex

Thanks @spinscale yes, this is what I have in mind right now:

  "aggs": {
    "group_by_res": {
      "terms": {
        "field": "person_id"
      },
      "aggs": {
        "average_grade": {
          "sum": {
            "field": "grade"
          }
        }
      }
    }
  } 

What do you think?

looks good to me. However this only gives you the top10 results, you may want to look at the composite aggregation for to retrieve all the buckets.

can't I just change the size of the agg to increase the number of results?

yes, but there is a limit to the maximum number of buckets, where as the composite agg allows for pagination.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.