Thanks @s1monw
Note: after executing the request in Sense, I've find-replaced confidential values of key and index fields before posting here.
request
{
"aggs": {
"by_myfield": {
"terms": {
"field": "myfield.raw",
"size": 10000
},
"aggs": {
"latest_record": {
"top_hits": {
"sort": [
{
"@timestamp": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"currentStateText"
]
},
"size": 1
}
}
}
}
},
"size": 0,
"script_fields": {
"my_field_name" : {
"script": {
"lang": "painless",
"inline": "int online_cnt = 0; return online_cnt++;"
}
}
}
}
response
{
"took": 618,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 50,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"by_apid": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "key77",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379151_f0be6bb641cf695b10d81383c50af87b",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379151]
}]
}
}
},
{
"key": "key60",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379144_2b56b947c95231f260bf10d57b2bf4e1",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379144]
}]
}
}
},
{
"key": "keyef",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379150_b1bede849555309b9d38d3f04d46c058",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379150]
}]
}
}
},
{
"key": "key1d",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379151_c93456016cb07c47d873f2ab701cb09d",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379151]
}]
}
}
},
{
"key": "key87",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379151_147c4858295ff2f67da9cb779b851183",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379151]
}]
}
}
},
{
"key": "keya5",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379149_4244a20a7020c99daa954c45f9289ccc",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379149]
}]
}
}
},
{
"key": "keyc9",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379152_0f2c1124f79e5fbbdd4865f30a7a6f39",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379152]
}]
}
}
},
{
"key": "key95",
"doc_count": 6,
"latest_record": {
"hits": {
"total": 6,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1516140379149_75833d3fbbf13666a506b753b3644b43",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1516140379149]
}]
}
}
},
{
"key": "key28",
"doc_count": 2,
"latest_record": {
"hits": {
"total": 2,
"max_score": null,
"hits": [{
"_index": ".my-index-2018.01_v1",
"_type": "log",
"_id": "1515712313126_34036a389510cb4970af0f9167be3c2d",
"_score": null,
"_source": {
"currentStateText": "Online"
},
"sort": [1515712313126]
}]
}
}
}]
}
}
}