我在Elasticsearch中有一些数据,其中对于相同的时间值,会有不同的速度值(有秒和毫秒字段)。我想按秒分组并获取该秒的平均速度。简而言之,寻找像这样的es查询:
select average(speed) as speed, sec from index group by sec order by sec asc
答案 0 :(得分:2)
您可以简单地在seconds
字段上使用terms
aggregation,然后在speed
字段上使用avg
metric sub-aggregation,如下所示:
{
"size": 0,
"query": {
},
"aggs": {
"seconds": {
"terms": {
"field": "seconds"
},
"aggs": {
"speed": {
"avg": {
"field": "speed"
}
}
}
}
}
}