码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • ES通过Collapse实现类似SQL over开窗函数功能


    数据如下:

    ab
    101
    102
    103
    2020
    2010
    2030

    需求:

    按照a列进行分组,然后按照b列进行排序,返回b列中最小的结果对应的数据。类似SQL :

    select a,b from (select a,b,row_number() over (partition by a order by b asc) as rank from tbl ) t where rank =1

    想要得到的结果:

    ab
    101
    2010

    实现:

    在es中可以通过collapse来实现以上功能,关于collapse的使用官方解释如下:

    You can use the collapse parameter to collapse search results based on field values. The collapsing is done by selecting only the top sorted document per collapse key.

    For example, the following search collapses results by user.id and sorts them by http.response.bytes.

    1. GET my-index-000001/_search
    2. {
    3.   "query": {
    4.     "match": {
    5.       "message": "GET /search"
    6.     }
    7.   },
    8.   "collapse": {
    9.     "field": "user.id"    ----- ①     
    10.   },
    11.   "sort": [
    12.     {
    13.       "http.response.bytes": { -----②
    14.         "order": "desc"
    15.       }
    16.     }
    17.   ],
    18.   "from": 0     -------③               
    19. }
    20.     
    21. ①:Collapse the result set using the user.id field
    22. ②:Sort the results by http.response.bytes
    23. ③:Define the offset of the first collapsed result

     es中创建索引并插入测试数据:

    1. DELETE test
    2. PUT /test/_doc/1
    3. {
    4. "a":10,
    5. "b":1
    6. }
    7. PUT /test/_doc/2
    8. {
    9. "a":10,
    10. "b":2
    11. }
    12. PUT /test/_doc/3
    13. {
    14. "a":10,
    15. "b":3
    16. }
    17. PUT /test/_doc/4
    18. {
    19. "a":20,
    20. "b":20
    21. }
    22. PUT /test/_doc/5
    23. {
    24. "a":20,
    25. "b":10
    26. }
    27. PUT /test/_doc/6
    28. {
    29. "a":20,
    30. "b":30
    31. }

    编写查询语句:
     

    1. GET /test/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "collapse": {
    7. "field": "a"
    8. },
    9. "sort": [
    10. {
    11. "b": {
    12. "order": "asc"
    13. }
    14. }
    15. ]
    16. }

    结果如下:

    1. {
    2. "took" : 855,
    3. "timed_out" : false,
    4. "_shards" : {
    5. "total" : 1,
    6. "successful" : 1,
    7. "skipped" : 0,
    8. "failed" : 0
    9. },
    10. "hits" : {
    11. "total" : {
    12. "value" : 6,
    13. "relation" : "eq"
    14. },
    15. "max_score" : null,
    16. "hits" : [
    17. {
    18. "_index" : "test",
    19. "_type" : "_doc",
    20. "_id" : "1",
    21. "_score" : null,
    22. "_source" : {
    23. "a" : 10,
    24. "b" : 1
    25. },
    26. "fields" : {
    27. "a" : [
    28. 10
    29. ]
    30. },
    31. "sort" : [
    32. 1
    33. ]
    34. },
    35. {
    36. "_index" : "test",
    37. "_type" : "_doc",
    38. "_id" : "5",
    39. "_score" : null,
    40. "_source" : {
    41. "a" : 20,
    42. "b" : 10
    43. },
    44. "fields" : {
    45. "a" : [
    46. 20
    47. ]
    48. },
    49. "sort" : [
    50. 10
    51. ]
    52. }
    53. ]
    54. }
    55. }

  • 相关阅读:
    @FeignClient configuration参数配置
    React子组件没有随父组件更新问题的解决
    Text-to-SQL小白入门(六)Awesome-Text2SQL项目介绍
    【linux】关于内存相关指标、内存回收、内存泄漏、内存调优等一些内存优化问题~
    R语言使用timeROC包计算无竞争情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、可视化无竞争情况下的生存资料多时间ROC曲线
    Nacos多种安装方式
    PgSql使用技巧总结 PgSql和MySql的对比
    Eureka注册中心
    关于ElasticSearch日期格式不一致的异常,可以这么解决
    解决Windows Server 2022启动PowerShell出现乱码问题
  • 原文地址:https://blog.csdn.net/qq_32020645/article/details/133271495
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号