# ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合

2019/04/10 10:10

• Metric:

• Bucketing:

• Matrix:

• Pipeline:

## 度量(Metric)聚合

### avg 聚合

DSL语句示例:

POST /student/_search?size=0
{
"aggs" : {
}
}


DSL语句示例:

POST /student/_search
{
"size": 0,
"aggs" : {
"weighted_avg": {
"value": {
},
"weight": {
"field": "weight"
}
}
}
}
}


### max/min 聚合

DSL语句示例:

POST /student/_search?size=0
{
"aggs" : {
}
}

POST /student/_search?size=0
{
"aggs" : {
}
}


### sum聚合

DSL语句示例:

POST /student/_search?size=0
{

"aggs" : {
}
}


### top 聚合

• from -与您要提取的第一个结果的偏移量。
• size-每个存储区返回的最匹配匹配项的最大数量。默认情况下，返回前三个匹配项。 sort-热门匹配项的排序方式。默认情况下，命中按主要查询的分数排序。

DSL语句示例:

POST /student/_search?size=0
{
"aggs": {
"top_tags": {
"terms": {
"size": 2
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"order": "desc"
}
}
],
"_source": {
},
"size" : 1
}
}
}
}
}
}


### JAVA代码示例


/**
* @Author pancm
* @Description 平均聚合查询测试用例
* @Date  2019/4/1
* @Param []
* @return void
**/
private static  void avgSearch() throws IOException {

//直接求平均数
logger.info("求班级的平均分数:");
agg(aggregation,buk);

}

private static  void maxSearch() throws  IOException{
logger.info("求班级的最高分数:");
agg(aggregation,buk);
}

private static  void sumSearch() throws  IOException{
logger.info("求班级的总分数:");
agg(aggregation,buk);
}

private static SearchResponse search(AggregationBuilder aggregation) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("student");
searchRequest.types("_doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//不需要解释
searchSourceBuilder.explain(false);
//不需要原始数据
searchSourceBuilder.fetchSource(false);
//不需要版本号
searchSourceBuilder.version(false);
searchSourceBuilder.aggregation(aggregation);
logger.info("查询的语句:"+searchSourceBuilder.toString());
searchRequest.source(searchSourceBuilder);
// 同步查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return  searchResponse;
}

protected  static  void agg(AggregationBuilder aggregation, String buk) throws  IOException{
SearchResponse searchResponse = search(aggregation);
if(RestStatus.OK.equals(searchResponse.status())) {
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();

if(buk.contains("avg")){
//取子聚合
Avg ba = aggregations.get(buk);
logger.info(buk+":" + ba.getValue());
logger.info("------------------------------------");
}else if(buk.contains("max")){
//取子聚合
Max ba = aggregations.get(buk);
logger.info(buk+":" + ba.getValue());
logger.info("------------------------------------");

}else if(buk.contains("min")){
//取子聚合
Min ba = aggregations.get(buk);
logger.info(buk+":" + ba.getValue());
logger.info("------------------------------------");
}else if(buk.contains("sum")){
//取子聚合
Sum ba = aggregations.get(buk);
logger.info(buk+":" + ba.getValue());
logger.info("------------------------------------");
}else if(buk.contains("top")){
//取子聚合TopHits
TopHits ba = aggregations.get(buk);
logger.info(buk+":" + ba.getHits().totalHits);
logger.info("------------------------------------");
}

}
}



## 其它

### 音乐推荐

<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1366594775&auto=0&height=66"></iframe>

0
0 收藏

0 评论
0 收藏
0