Count and group by the results of top_hits aggregation


I have logs with the fields:
. IP
. Timestamp
. SessionId

Each log shows the time (timestamp) the URI was visited. The SessionId is a field I created by concatenating IP with Timestamp (without the minutes or seconds). That is, all the logs belonging to the same day and hour are grouped into the same SessionId.

For e.g.

IP Timestamp URI SessionId December25th2017T08:30:00 December25th2017T08:45:00 December25th2017T09:30:00 December25th2017T09:58:00 December30th2017T15:20:00 December30th2017T15:30:00 December30th2017T15:58:00 December30th2017T09.35:00 December30th2017T09.59:00

Now, I need to know the top URI that were the last ones to be visited in the sessions.
That is, for the example above:
-> "" was the last URI visited in the following sessions:

-> "" was the last URI visited in the following session:

Therefore, I need a visualization/query that indicates something like:
URI Count 3 1

So far, I've managed to get the last visited URL's for each session by doing a terms aggregation on session_id and then a top_hits aggregation on timestamp.
The problem is that I can't group them by URL (to compute the count for each URL) because top_hits aggregation doesn't allow any subaggregations...

How can I solve this?

Thank you

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