Thanh Khánh
Thanh Khánh

Reputation: 641

Elasticsearch filter first record only on condition

In elasticsearch, I have a table of data that have field customer_id and event_date, I want to count by each month but not all data, I want to filter out duplicated customer_id except first record (have min event date) before apply date historgrams, please help me to filter query.

This is my sample data:

"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
                }
            },
            ...
       ]

I have field customer_id and created_at, customer_id value can have same value in multiple records, but I just want work with first record foreach customer_id

Upvotes: 0

Views: 308

Answers (1)

Aki
Aki

Reputation: 23

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

try this

Upvotes: 1

Related Questions