-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Description
Elasticsearch version 8.18.1 on Windows Server 2019
I am looking for assistance with an ES|QL query to populate a new field with the result of the Top Hit:
Here is an example:
ROW host = "win10-abc", ip = ["192.168.5.45", "192.168.5.4", "192.168.5.45", "192.168.5.77", "192.168.5.45","192.168.5.45","192.168.18.24"]
| MV_EXPAND ip
| STATS count = COUNT(*) BY ip, host
| SORT count DESC
Which results in:
count ip host
4 "192.168.5.45" "win10-abc"
1 "192.168.5.77" "win10-abc"
1 "192.168.18.24" "win10-abc"
1 "192.168.5.4" "win10-abc"
I would like to show just the top result, but here is the kicker, I want this for other rows as well, such as another host, win-11def and return it's highest IP value so that I can have a query that is below the 10,000 ES|QL limit.
I can do this easily in Lens with a KQL filters and aggregations but I want to use the VALUEs on other field to aggregate field values in another field (such as all host names) which can't be done with lens today.
Trying something like this:
ROW host = "win10-abc", ip = ["192.168.5.45", "192.168.5.4", "192.168.5.45", "192.168.5.77", "192.168.5.45","192.168.5.45","192.168.5.18.24"]
| MV_EXPAND ip
| STATS count = COUNT(*) BY ip, host
| STATS top_ip = TOP(ip, 1, "desc"), top_count = MAX(count) BY host
Yields in this result which isn't what I expected:
"top_ip" "top_count" host
"192.168.5.77" 4 "win10-abc"
I would expect to see 4 "192.168.5.45" "win10-abc" as my top result.
In short, I would like to aggregate on some values but only select the row with the highest count.
What I am really looking for is a Top result to return based on an aggregation that ES|QL does. I can do this with KQL and Lens today but I wanted the power of ES|QL behind it.
Top commands work well in Lens with KQL, but I want to do this in ESQL.
