The same aggregation can be performed using Elastica:
123456789101112131415
useElastica\Aggregation\Terms;useElastica\Query;// set up the aggregation$termsAgg=newTerms("genders");$termsAgg->setField("gender");$termsAgg->setSize(10);// add the aggregation to a Query object$query=newQuery();$query->addAggregation($termsAgg);// retrieve the results$index=$elasticaClient->getIndex('someindex');$buckets=$index->search($query)->getAggregation("genders");
useElastica\Aggregation\Terms;useElastica\Aggregation\Stats;useElastica\Query;$termsAgg=newTerms("genders");$termsAgg->setField("gender");$termsAgg->setOrder("height_stats.avg","desc");$statsAgg=newStats("height_stats");$statsAgg->setField("height");$termsAgg->addAggregation($statsAgg);$index=$elasticaClient->getIndex('someindex');$buckets=$index->search($query)->getAggregation("genders");foreach($bucketsas$bucket){$statsAggResult=$bucket["height_stats"];// do something with the result of the stats agg}