Avg Aggregation

More info about avg aggregation is in the official elasticsearch docs

A single-value metrics aggregation that computes the average of numeric values that are extracted from the aggregated documents.

Simple example

{
    "aggregations": {
        "avg_grade": {
            "avg": {
                "field": "grade"
            }
        }
    }
}

And now the query via DSL:

$avgAggregation = new AvgAggregation('avg_grade');
$avgAggregation->setField('grade');

$search = new Search();
$search->addAggregation($avgAggregation);

$queryArray = $search->toArray();