【发布时间】:2019-12-26 09:19:48
【问题描述】:
我试图从弹性搜索中获取最后 2 个条目的平均值,但被困在了这里。以下是我在弹性搜索中的数据:
{
"took": 115,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 22696195,
"max_score": 1.0,
"hits": [{
"_index": "queue_pings",
"_type": "QueuePings",
"_id": "AWGtuVzycUtYPYuAwOzr",
"_score": 1.0,
"_source": {
"queue": "queue1",
"length": 134,
"timestamp": "2018-02-19 11:01:01"
}
}, {
"_index": "queue_pings",
"_type": "QueuePings",
"_id": "AWGtuV0DcUtYPYuAwOzs",
"_score": 1.0,
"_source": {
"queue": "queue2",
"length": 1202,
"timestamp": "2018-02-19 11:01:01"
}
}, {
"_index": "queue_pings",
"_type": "QueuePings",
"_id": "AWGtuV0dWFpRPa9T9mcf",
"_score": 1.0,
"_source": {
"queue": "queue1",
"length": 120,
"timestamp": "2018-02-19 11:00:01"
}
}, {
"_index": "queue_pings",
"_type": "QueuePings",
"_id": "AWGtuV0wTPjiqgqsDMAM",
"_score": 1.0,
"_source": {
"queue": "queue2",
"length": 1762,
"timestamp": "2018-02-19 11:00:01"
}
}, {
"_index": "queue_pings",
"_type": "QueuePings",
"_id": "AWGtuV09WFpRPa9T9mcg",
"_score": 1.0,
"_source": {
"queue": "queue3",
"length": 220,
"timestamp": "2018-02-19 11:00:01"
}
}
]
}
}
我想获得queue = queue1 的最后 2 个条目的平均值。
这里是等效的MYSQL查询SELECT AVG(length) FROM queue_pings WHERE queue = 'queue1' order by timestamp desc limit 2;
到目前为止我所尝试的:
GET /queue_pings/_search?size=2
{
"aggs": {
"queue_filter" : {
"filter" : { "term" : { "queue" : "queue1" } },
"aggs" : {
"queue_avg" : { "avg" : { "field" : "length" } }
}
}
}
}
编辑:添加映射
{
"queue_pings": {
"mappings": {
"QueuePings": {
"properties": {
"length": {
"type": "long"
},
"queue": {
"type": "keyword"
},
"timestamp": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
}
【问题讨论】:
标签: elasticsearch