Aggregation using something similar to First when data is grouped

Lets say I have data like this. Every unique "key" has a unique "long key" - totally 1 to 1.
Because key is numeric will prefer using it for agrregating (using) using terms-> field.
Field "somedata" is subject to aggregations like sum etc when grouped by the key.
When aggregating for sum or avg of somedata- would like to also compute long key-

Source Data::

key long key somedata
1 XYZ 10
1 XYZ 20
1 XYZ 20
2 PQR 30
2 PQR 35
Anything like first() or most() exists to show results as below. Main concern is how to capture longkey as shown below in a sample aggregation?
Expected Aggregation representation:
key long key sum of somedata
1 XYZ 50
2 PQR 65

R

You could have another top_hits aggregation within the terms agg that returns the two fields which then could be extracted. See https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-aggregations-metrics-top-hits-aggregation.html

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