Search latest events and unique by field

Hello.

I got the following data in my indice:

{"username": "michael", "is_vac": true, "created_date": 1596647898006}
{"username": "kate", "is_vac": true, "created_date": 1596647898007}
{"username": "michael", "is_vac": true, "created_date": 1596647898016}
{"username": "kate", "is_vac": true, "created_date": 1596647898017}

I want to search and get latest documents (by field created_date - it is unixtime with milliseconds) and unique (distinct) by field username.

In this case i want to see the following result:

{"username": "michael", "is_vac": true, "created_date": 1596647898016}
{"username": "kate", "is_vac": true, "created_date": 1596647898017}

Unique usernames (michael and kate) and latest documents:
in michael case: 1596647898016 > 1596647898006
in kate case: 1596647898017 > 1596647898007

Example #2. Dataset:

{"username": "jonh", "is_vac": true, "created_date": 1596647898001}
{"username": "michael", "is_vac": true, "created_date": 1596647898001}
{"username": "kate", "is_vac": true, "created_date": 1596647898002}
{"username": "michael", "is_vac": true, "created_date": 1596647898006}
{"username": "kate", "is_vac": true, "created_date": 1596647898007}
{"username": "michael", "is_vac": true, "created_date": 1596647898016}
{"username": "kate", "is_vac": true, "created_date": 1596647898017}
{"username": "kate", "is_vac": true, "created_date": 1596647898027}

Desireable search result:

{"username": "jonh", "is_vac": true, "created_date": 1596647898001}
{"username": "michael", "is_vac": true, "created_date": 1596647898016}
{"username": "kate", "is_vac": true, "created_date": 1596647898027}

How i can achieve this?

Check out collapsing search results

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.