-
},
-
“low”: {
-
“type”: “long”
-
},
-
“date”: {
-
“format”: “strict_date_optional_time||epoch_millis”,
-
“type”: “date”
-
},
-
“close”: {
-
“type”: “long”
-
}
-
},
-
“_all”: {
-
“enabled”: false
-
}
-
}
-
}
索引中的全部数据:
name | age | salary | team | position |
james | 33 | 3000 | cav | sf |
irving | 25 | 2000 | cav | pg |
curry | 29 | 1000 | war | pg |
thompson | 26 | 2000 | war | sg |
green | 26 | 2000 | war | pf |
garnett | 40 | 1000 | tim | pf |
towns | 21 | 500 | tim | c |
lavin | 21 | 300 | tim | sg |
wigins | 20 | 500 | tim | sf |
首先,初始化Builder:
SearchRequestBuilder sbuilder = client.prepareSearch(“player”).setTypes(“player”);
接下来举例说明各种聚合操作的实现方法,因为在es的api中,多字段上的聚合操作需要用到子聚合(subAggregation),初学者可能找不到方法(网上资料比较少,笔者在这个问题上折腾了两天,最后度了源码才彻底搞清楚T_T),后边会特意说明多字段聚合的实现方法。另外,聚合后的排序也会单独说明。
1. group by/count
例如要计算每个球队的球员数,如果使用SQL语句,应表达如下:
select team, count(*) as player_count from player group by team;
ES的java api:
-
TermsBuilder teamAgg= AggregationBuilders.terms( "player_count ").field( “team”);
-
sbuilder.addAggregation(teamAgg);
-
SearchResponse response = sbuilder.execute().actionGet();
2.group by多个field
例如要计算每个球队每个位置的球员数,如果使用SQL语句,应表达如下:
select team, position, count(*) as pos_count from player group by team, position;
ES的java api:
-
TermsBuilder teamAgg= AggregationBuilders.terms( "player_count ").field( “team”);
-
TermsBuilder posAgg= AggregationBuilders.terms( “pos_count”).field( “position”);
-
sbuilder.addAggregation(teamAgg.subAggregation(posAgg));
-
SearchResponse response = sbuilder.execute().actionGet();
3.max/min/sum/avg
例如要计算每个球队年龄最大/最小/总/平均的球员年龄,如果使用SQL语句,应表达如下:
select team, max(age) as max_age from player group by team;
ES的java api:
-
TermsBuilder teamAgg= AggregationBuilders.terms( "player_count ").field( “team”);
-
MaxBuilder ageAgg= AggregationBuilders.max( “max_age”).field( “age”);
-
sbuilder.addAggregation(teamAgg.subAggregation(ageAgg));
-
SearchResponse response = sbuilder.execute().actionGet();
4.对多个field求max/min/sum/avg
例如要计算每个球队球员的平均年龄,同时又要计算总年薪,如果使用SQL语句,应表达如下:
select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;
ES的java api:
-
TermsBuilder teamAgg= AggregationBuilders.terms( “team”);
-
AvgBuilder ageAgg= AggregationBuilders.avg( “avg_age”).field( “age”);
-
SumBuilder salaryAgg= AggregationBuilders.avg( "total_salary ").field( “salary”);
-
sbuilder.addAggregation(teamAgg.subAggregation(ageAgg).subAggregation(salaryAgg));
-
SearchResponse response = sbuilder.execute().actionGet();
5.聚合后对Aggregation结果排序
例如要计算每个球队总年薪,并按照总年薪倒序排列,如果使用SQL语句,应表达如下:
select team, sum(salary) as total_salary from player group by team order by total_salary desc;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
面试题文档来啦,内容很多,485页!
由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。
1111道Java工程师必问面试题
MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:
Elasticsearch 24 题 +Memcached + Redis 40题:
Spring 26 题+ 微服务 27题+ Linux 45题:
Java面试题合集:
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!
Redis 40题:**
[外链图片转存中…(img-a5lFb7i8-1711972912774)]
Spring 26 题+ 微服务 27题+ Linux 45题:
[外链图片转存中…(img-3ykm3NWQ-1711972912774)]
Java面试题合集:
[外链图片转存中…(img-Yd5z7eKp-1711972912774)]文章来源:https://www.toymoban.com/news/detail-846908.html
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!文章来源地址https://www.toymoban.com/news/detail-846908.html
到了这里,关于ElasticSearch java API - 聚合查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!