如何从弹性Json结果中获取键和值?

时间:2019-03-15 09:18:30

标签: elasticsearch kibana elasticsearch-aggregation elasticsearch-painless

Elastic Watcher的action部分看起来像这样-

"actions": {
    "index_payload": {
      "transform": {
        "script": "ctx.payload.aggregations.time_interval.buckets.0"
      },
      "index": {
        "index": "hourly-data",
        "doc_type": "meters_hourly_data"
      }
    }
  }

,输出为-

"payload" : {
              "key_as_string" : "2019-03-11T00:00:00.000-05:00",
              "doc_count" : 1440,
              "http_server" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "1" : {
                      "value" : 0.16463961166666666
                    },
                    "doc_count" : 960,
                    "2" : {
                      "value" : 0.0
                    },
                    "3" : {
                      "value" : 3.2266329166666665
                    },
                    "4" : {
                      "doc_count_error_upper_bound" : 0,
                      "sum_other_doc_count" : 0,

我想获取直接对象键和值key_as_string。到目前为止,ctx.payload.aggregations.time_interval.buckets.0可以达到payload。当我尝试将脚本更改为ctx.payload.aggregations.time_interval.buckets.0.key_as_stringctx.payload.aggregations.time_interval.buckets.0.key_as_string.value时,我收到错误消息。

使用以下脚本-

"script": "['ctx.payload.aggregations.time_interval.buckets.0'].value"

我收到以下错误-

> "script":
> "(int)(['ctx.payload.aggregations.time_interval.buckets.0'].value)",
>     "lang": "painless",
>     "caused_by": {
>       "type": "class_cast_exception",
>       "reason": "Cannot cast from [java.lang.String] to [int]."

0 个答案:

没有答案