5

有没有办法获取行数并按小时、天或月对它们进行分组。

例如,假设我有消息

_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}
}

谢谢

4

1 回答 1

9

尝试聚合。

{     
  "aggs": {
    "amount_per_month": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "week"
      },
      "aggs": {
        "total_amount": {
          "sum": {
            "field": "amount"
          }
        }
      }
    }
  }
}

此外,如果您想计算索引的数量,请将 sum 内容替换为:

"sum": {
  "script": "1"
}

希望能帮助到你。

于 2015-09-14T07:14:35.810 回答