ElasticSearch仅按条件筛选第一条记录

0kjbasz6  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(158)

在elasticsearch中,我有一个数据表,其中有字段customer_id和event_date,我想按每个月计数,但不是所有数据,我想过滤掉重复的customer_id,除了第一条记录(具有最小事件日期)之前应用日期历史记录,请帮助我过滤查询。
这是我的样本数据:

"hits": [
            {
                "_index": "log_report",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "created_at": "2021-08-23T02:36:10+00:00",
                    "created_by": -1,
                    "updated_at": "2021-08-23T02:36:10+00:00",
                    "updated_by": -1,
                    "is_deleted": false,
                    "account_id": 509,
                    "customer_id": 1961105,
                    "opened_at": "2021-08-23T02:36:10+00:00",
                    "id": 5
                }
            },
            {
                "_index": "log_report",
                "_id": "11",
                "_score": 1.0,
                "_source": {
                    "created_at": "2021-08-23T02:37:26+00:00",
                    "created_by": -1,
                    "updated_at": "2021-08-23T02:37:26+00:00",
                    "updated_by": -1,
                    "is_deleted": false,
                    "account_id": 509,
                    "customer_id": 1961105,
                    "opened_at": "2021-08-23T02:37:26+00:00",
                    "id": 11
                }
            },
            ...
       ]

我有字段customer_idcreated_at,customer_id值可以在多个记录中具有相同值,但我只想处理每个customer_id第一个记录

9wbgstp7

9wbgstp71#

{
  "sort": [
    {
      "created_at": {
        "order": "asc",
        "unmapped_type": "date"
      }
    }
  ],
  "collapse": {
    "field": "customer_id"
  }
}

试试这个

相关问题