码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • elasticsearch系列-ES对多个字段聚合,select A,B,COUNT() from table group by A,B


    **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 "
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    JavaAPI

    Script script = new Script(ScriptType.INLINE, “groovy”, “doc[’ SEX .keyword’].value+‘-split-’+doc[’ PROF .keyword’].value”, new HashMap());

    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
                    }
                ]
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2、错误的答案:

    {
        "query": {
            "match_all": {}
        },
        "aggs": {
            "sex": {
                "terms": {
                    "field": "SEX.keyword"
                }
            },
            "prof": {
                "terms": {
                    "field": "PROF.keyword'"
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    拿到的结果是大概像这样的(省略了没用的信息),分开统计了,这明显不是我们想要的

    {
        "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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    30 数据分析(上)(业务略写可跳)|jupyter|matplotlib
    Ubuntu20.04搭建gem5并运行helloworld
    笔记本电脑windows10有线连接开无线热点方法已经成功
    element tree懒加载默认展开指定节点
    跨模态神经搜索实践VCED 基于Streamlit实现前端页面设计和逻辑
    Go语言进阶,interface接口,socket套接字
    LeetCode高频题55加类似新题45:跳跃游戏 II:最少需要跳多少步才能抵达终点
    怎么用Java的代码制作简单的王者荣耀
    为什么说网上的md5加密解密站都是通过彩虹表解密的?
    【稳定性】秘密武器--功能开关技术
  • 原文地址:https://blog.csdn.net/m0_67402096/article/details/126362662
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号