top_hit aggregation returns some document sorted by specified order. You can get host.name
from one (or some) representative document of that bucket.
sample on kibana_sample_data_flights.
GET /kibana_sample_data_flights/_search
{
"size":0,
"aggs": {
"by_host_ip": {
"terms": { "field": "DestAirportID", "size": 1},
"aggs": {
"avg_utilization": {
"avg": { "field": "AvgTicketPrice" }
},
"name":{
"top_hits":{
"size":1,
"_source": ["DestCityName"]
}
}
}
}
}
}
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"by_host_ip" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 12368,
"buckets" : [
{
"key" : "ZRH",
"doc_count" : 691,
"name" : {
"hits" : {
"total" : {
"value" : 691,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "kibana_sample_data_flights",
"_type" : "_doc",
"_id" : "Tpr_Zn4Bf0nakUP8OrRZ",
"_score" : 1.0,
"_source" : {
"DestCityName" : "Zurich"
}
}
]
}
},
"avg_utilization" : {
"value" : 575.1067587028537
}
}
]
}
}
}
Another option using terms aggregation:
GET /kibana_sample_data_flights/_search
{
"size":0,
"aggs": {
"by_host_ip": {
"terms": { "field": "DestAirportID", "size": 1},
"aggs": {
"avg_utilization": {
"avg": { "field": "AvgTicketPrice" }
},
"name":{
"terms": {
"field": "DestCityName",
"size": 10
}
}
}
}
}
}
{
"took" : 42,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"by_host_ip" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 12368,
"buckets" : [
{
"key" : "ZRH",
"doc_count" : 691,
"name" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "Zurich",
"doc_count" : 691
}
]
},
"avg_utilization" : {
"value" : 575.1067587028537
}
}
]
}
}
}
Both returns DestCityName: "Zurich" which is identital to the DestAirportID "ZRH".