Elasticsearch Guide [8.14]
/apple-icon-57x57.png
A single-value metrics aggregation that computes the average of numeric values that are extracted from the aggregated documents. These values can be extracted either from specific numeric or histogram fields in the documents.

Assuming the data consists of documents representing exams grades (between 0 and 100) of students we can average their scores with:

response = client.search( index: 'exams', size: 0, body: { aggregations: { avg_grade: { avg: { field: 'grade' } } } } ) puts response

POST /exams/_search?size=0 { "aggs": { "avg_grade": { "avg": { "field": "grade" } } } }

The above aggregation computes the average grade over all documents. The aggregation type is avg and the field setting defines the numeric field of the documents the average will be computed on. The above will return the following:

{ ... "aggregations": { "avg_grade": { "value": 75.0 } } }