Aggregations on most recent document in group using elasticsearch
If you only need to find the most recent persons
try something like this:
"aggs": { "personName": { "terms": { "field": "name", "size": 5, "order": {"timeCreated": "desc"} }, "aggs": { "timeCreated": { "max": {"field": "timestamp"} } } }}
The second operation is just an aggregation, and to get the average of the value
field you could try something like:
curl -XPOST "http://DOMAIN:9200/your/data/_search" -d'{ "size": 0, "aggregations": { "the_name": { "terms": { "field": "name", "order": { "value_avg": "desc" } }, "aggregations": { "value_avg": { "avg": { "field": "value" } } } } }}'
To achieve a solution for your first issue I would recommend you to order the response by date, and then in your project ignore a term when you have another with the same name (meaning filter the data after the response of ES)