elasticsearch系列-ES对多个字段聚合,select A,B,COUNT(*) from table group by A,B
来源:互联网 发布:linux find指令 编辑:程序博客网 时间:2024/06/06 10:50
ES对多个字段聚合,select A,B,COUNT(*)from table group by A,B
假设有下表
NAME SEX PROF
李诚 男 副教授
张旭 男 讲师
王萍 女 助教
刘冰 女 助教
要查询select SEX,PROF,COUNT(*) from table group by SEX,PROF
1、正确的答案:
修改elasticsearch.yml配置文件,添加下面两个配置,重启es集群
script.engine.groovy.inline.aggs: on
script.engine.groovy.inline.search: on
{ "size": 0, "query": { "match_all": {} }, "aggs": { "sexprof": { "terms": { "script": { "inline": "doc['SEX.keyword'].value +'-split-'+ doc['PROF.keyword'].value " } } } }}
JavaAPI
Script script = new Script(ScriptType.INLINE, "groovy", "doc['SEX.keyword'].value+'-split-'+doc['PROF.keyword'].value", new HashMap<String, Object>());
TermsAggregationBuilder callTypeTeamAgg =AggregationBuilders.terms("sexprof").script(script);
这样得到的sex,prof两列是一起返回的,中间通过"-split-"分开,拿到结果后自行处理,结果大概像下面的(省略了没用的信息):
{ "aggregations": { "sexprof": { "doc_count_error_upper_bound": 5, "sum_other_doc_count": 379, "buckets": [ { "key": "女-split-助教", "doc_count": 2 }, { "key": "男-split-讲师", "doc_count": 1 }, { "key": "男-split-教授", "doc_count": 1 } ] } }}
2、错误的答案:
{ "query": { "match_all": {} }, "aggs": { "sex": { "terms": { "field": "SEX.keyword" } }, "prof": { "terms": { "field": "PROF.keyword'" } } }}
拿到的结果是大概像这样的(省略了没用的信息),分开统计了,这明显不是我们想要的
{ "aggregations": { "sex": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "女", "doc_count": 2 }, { "key": "男", "doc_count": 2 } ] }, "prof": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "副教授", "doc_count": 1 }, { "key": "讲师", "doc_count": 1 }, { "key": "助教", "doc_count": 1 } ] } }}
阅读全文
1 0
- elasticsearch系列-ES对多个字段聚合,select A,B,COUNT(*) from table group by A,B
- UPDATE {0} SET DisplayOrder=(SELECT COUNT(1) FROM {0} b WHERE b.DisplayOrder<a.DisplayOrder AND {1})
- hdu5528 Count a * b
- 5528Count a b
- db2中可以实现create table A as select * from B吗?
- select concat('a','b') from dual; select 'a'||'b' from dual;
- oracle按照2个字段分区 over(partition by A,B order by C)
- 使用select * from (select * a inner join b on....)注意事项
- hdu 5528 Count a * b
- A+B Problem (Case Count)
- A+B Problem (Case Count)
- A+B problem(case count)
- oracle insert into A select from B 的一些心得
- select * from A1,A2 where A1.a = A2.b
- select * from a,b 的弊端和代替方案
- select * from a,b是否为内连接?
- 在select * from A.B.C.D中A、B、C、D分别代表什么对象?
- CREATE TABLE A LIKE B
- Week3模拟
- 轮播+跳转+xlistview
- Dialog出现黑框
- 机器学习-训练模型的保存与恢复(sklearn)
- 阿里面试后的想法
- elasticsearch系列-ES对多个字段聚合,select A,B,COUNT(*) from table group by A,B
- 字符串
- 服务播放音乐
- 隔行变色,,,,按钮鼠标变小手
- 数字签名是什么?
- LUA 排序算法和性能分析[3]:选择排序算法
- 查看apk签名,查看key签名,adb常用命令
- 产品落地第一步:需求采集和需求分析
- HDOJ 1200 To and Fro(模拟题)