Single query to find document with largest value for some field
You can accomplish this by combining "sort" and "size":
`{
"filter" : {
"match_all" : { }
},
"sort": [
{
"run_id": {
"order": "desc"
}
}
],
"size": 1
}`
This will return the record with the highest run_id
It is possible to retrieve entire document holding max value using top hits aggregation
{
"size": 0,
"aggs":{
"doc_with_max_run_id": {
"top_hits": {
"sort": [
{
"latest_run_id": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}
Also by modifying 'size' aggregation parameter it is possible to retrieve n-top documents.