Elasticsearch按照某个字段去重查询

索引较多:

index-1_t_order
index-2_t_order
index-32_t_order

根据pay_amount排序,order_no去重,最后分页。

说明:
1、collapse:去重得到去重后的记录,配合"from": 0, "size": 1分页得到结果;(注意:此处查询返回的total-hits不是去重后的结果数量,而是命中的记录数,要获取去重后的记录数要使用cardinality)

2、cardinality:得到去重统计结果

GET /index-*_t_order/_search
{
  "from": 0,
  "size": 1,
  "track_total_hits": true,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "push_time": {
              "gte": "2022-02-08",
              "lte": "2022-02-09",
              "time_zone": "+08:00"
            }
          }
        },
        {
          "terms": {
            "org_code.keyword": [
              "00T0024",
              "00T0025"
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "pay_amount": {
        "order": "desc"
      }
    }
  ],
  "collapse": {
    "field": "order_no.keyword"
  },
  "aggregations": {
    "count": {
      "cardinality": {
        "field": "order_no.keyword"
      }
    }
  }
}

 参考:官方文档1      官方文档2

posted @ 2022-02-17 18:55  下午喝什么茶  阅读(871)  评论(0编辑  收藏  举报