0
点赞
收藏
分享

微信扫一扫

ES通过Collapse实现类似SQL over开窗函数功能

朱悟能_9ad4 2023-09-26 阅读 39

数据如下:

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的使用官方解释如下:

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

DELETE test

PUT /test/_doc/1
{
  "a":10,
  "b":1
}

PUT /test/_doc/2
{
  "a":10,
  "b":2
}

PUT /test/_doc/3
{
  "a":10,
  "b":3
}

PUT /test/_doc/4
{
  "a":20,
  "b":20
}

PUT /test/_doc/5
{
  "a":20,
  "b":10
}

PUT /test/_doc/6
{
  "a":20,
  "b":30
}

编写查询语句:
 

GET  /test/_search
{
  "query": {
    "match_all": {}
  },
  "collapse": {
    "field": "a"
  },
  "sort": [
    {
      "b": {
        "order": "asc"
      }
    }
  ]
}

结果如下:

{
  "took" : 855,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "a" : 10,
          "b" : 1
        },
        "fields" : {
          "a" : [
            10
          ]
        },
        "sort" : [
          1
        ]
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "a" : 20,
          "b" : 10
        },
        "fields" : {
          "a" : [
            20
          ]
        },
        "sort" : [
          10
        ]
      }
    ]
  }
}
举报

相关推荐

0 条评论