下面显示的是我的数据类型的结构。
ArrivalAirport | DepartureAirport | ArrivalDelay | DepartureDelay
A | B | 2 | 5
B | C | 3 | 6
C | A | 4 | 7
A | B | 5 | 8
我想一起表演
我期待输出的结果如下:
Airport | AverageArrivalDelay | AverageDepartureDelay
A | 3.5 | 7
B | 3 | 6.5
C | 4 | 6
我能够分别完成两个聚合:
ArrivalAirport | AverageArrivalDelay
A | 3.5
B | 3
C | 4
做:
"aggs": {
"ArrivalAirport": {
"terms": {
"field": "ArrivalAirport "
},
"aggs": {
"AverageArrivalDelay ": {
"avg": {
"field": "ArrivalDelay"
}
}
}
}
}
并且类似地使AverageDepartureDelay得到这样的聚合。
DepartureAirport | AverageDepartureDelay
A | 7
B | 6.5
C | 6
我正在寻找一种方法,其中我不必在应用程序级别合并两个结果。任何想法?
答案 0 :(得分:2)
我基于以下映射类型:
curl -XPUT localhost:9200/tests -d '
{
"mappings": {
"test1": {
"properties": {
"ArrivalAirport": {
"type": "string"
},
"DepartureAirport": {
"type": "string"
},
"DepartureDelay": {
"type": "integer"
},
"ArrivalDelay": {
"type": "integer"
}
}
}
}
}'
并创建了四个与上述数据表相匹配的文档。
curl -XPOST localhost:9200/tests/_bulk -d '
{"index": {"_type": "test1", "_id": 1}}
{"ArrivalAirport": "A", "DepartureAirport": "B", "ArrivalDelay": 2, "DepartureDelay": 5}
{"index": {"_type": "test1", "_id": 2}}
{"ArrivalAirport": "B", "DepartureAirport": "C", "ArrivalDelay": 3, "DepartureDelay": 6}
{"index": {"_type": "test1", "_id": 3}}
{"ArrivalAirport": "C", "DepartureAirport": "A", "ArrivalDelay": 4, "DepartureDelay":7}
{"index": {"_type": "test1", "_id": 4}}
{"ArrivalAirport": "A", "DepartureAirport": "B", "ArrivalDelay": 5, "DepartureDelay": 8}
'
将返回您期望的结果的聚合查询如下所示:
curl -XPOST localhost:9200/tests/_search -d '
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"Arrivals": {
"filter": {
"terms": {
"ArrivalAirport": [
"a",
"b",
"c"
]
}
},
"aggs": {
"Arrival": {
"terms": {
"field": "ArrivalAirport"
},
"aggs": {
"AverageArrivalDelay": {
"avg": {
"field": "ArrivalDelay"
}
}
}
}
}
},
"Departures": {
"filter": {
"terms": {
"DepartureAirport": [
"a",
"b",
"c"
]
}
},
"aggs": {
"Departure": {
"terms": {
"field": "DepartureAirport"
},
"aggs": {
"AverageDepartureDelay": {
"avg": {
"field": "DepartureDelay"
}
}
}
}
}
}
}
}'