一、ES的demo数据如下:
二、简单聚合,获得平均分
GET /student/student/_search
{
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
查询结果:
java实现:
/** * 聚合查询avg * *GET /student/student/_search * { * "aggs": { * "avg_score": { * "avg": { * "field": "score" * } * } * } * } * */ @ResponseBody @RequestMapping("/searchAvg") public Double searchAvg(){ AvgAggregationBuilder scoreQuery = AggregationBuilders.avg("avg_score"); scoreQuery.field("score"); System.out.println(s