• 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. }

  • 相关阅读:
    Android中的屏幕刷新机制(动画视频形象说明机制)
    GoLong的学习之路(十)语法之函数
    苹果新专利:用AR眼镜来控制iPhone屏幕输出,保护隐私
    竞赛 : 题目:基于深度学习的水果识别 设计 开题 技术
    聚星文社同款绘唐科技——小说推文一键AI生成工具
    SPPNet
    JVM基础篇
    Thread线程异常的处理方式
    亚马逊运营纯干货:如何引流?亚马逊站外引流最全讲解
    计算机毕业设计Java游泳馆管理平台(系统+程序+mysql数据库+Lw文档)
  • 原文地址:https://blog.csdn.net/qq_32020645/article/details/133271495