有没有办法获取行数并按小时,天或月分组。
例如,假设我有消息
_source{
"timestamp":"2013-10-01T12:30:25.421Z",
"amount":200
}
_source{
"timestamp":"2013-10-01T12:35:25.421Z",
"amount":300
}
_source{
"timestamp":"2013-10-02T13:53:25.421Z",
"amount":100
}
_source{
"timestamp":"2013-10-03T15:53:25.421Z",
"amount":400
}
有没有办法单独使用{date,sum}这些行(不一定是这种格式,只是想知道我是否有办法实现这一点)
{
{"2013-10-01T12:00:00.000Z", 500},
{"2013-10-02T13:00:00.000Z", 100},
{"2013-10-03T15:00:00.000Z", 400}
}
谢谢
答案 0 :(得分:5)
尝试使用聚合。
{
"aggs": {
"amount_per_month": {
"date_histogram": {
"field": "timestamp",
"interval": "week"
},
"aggs": {
"total_amount": {
"sum": {
"field": "amount"
}
}
}
}
}
}
此外,如果您想计算索引数量,请用以下内容替换总和内容:
"sum": {
"script": "1"
}
希望它有所帮助。