Can't get size of array/list in watcher condition

here is the result:

"result": {
  "execution_time": "2018-04-13T23:05:32.255Z",
  "execution_duration": 179,
  "input": {
    "type": "search",
    "status": "success",
    "payload": {
      "_shards": {
        "total": 182,
        "failed": 0,
        "successful": 182,
        "skipped": 180
      },
      "hits": {
        "hits": [],
        "total": 1,
        "max_score": 0
      },
      "took": 29,
      "timed_out": false,
      "aggregations": {
        "bucket_per_hour": {
          "buckets": [
            {
              "key_as_string": "2018-04-13T22:10:00.000Z",
              "doc_count": 1,
              "total_count": {
                "value": 1
              },
              "key": 1523657400000
            }
          ]
        }
      }
    },

and when I use this condition on the buckets array, I can access values in the first element
"condition": { "compare": { "ctx.payload.aggregations.bucket_per_hour.buckets.0.doc_count": { "gt": 0 } } },
works

however, what I really want to do is count the number of buckets.
I have tried

"condition": { "compare": { "ctx.payload.aggregations.bucket_per_hour.buckets.length": { "gt": 0 }
and
"condition": { "compare": { "ctx.payload.aggregations.bucket_per_hour.buckets.length()": { "gt": 0 }
and
"condition": { "compare": { "ctx.payload.aggregations.bucket_per_hour.buckets.size": { "gt": 0 }
and
"condition": { "compare": { "ctx.payload.aggregations.bucket_per_hour.buckets..size()": { "gt": 0 }

all of those last four attempts result in comparing "null" to 0
like this:
"compare": {
"resolved_values": {
"ctx.payload.aggregations.bucket_per_hour.buckets.length": null

If only wanted to compare to 0, I suppose I could test
"ctx.payload.aggregations.bucket_per_hour.buckets.0.bucket.doc_count == null

but I actually want to know how many buckets there are

@iamthealex I think a script condition would do what you're looking for:

"condition": {
    "script": {
      "source": "return ctx.payload.aggregations.buckets_per_hour.buckets.size() > 0"
      "lang": "painless"
    }
  }

The .size() function is a Painless function, so needs to exist within a script. The "regular" compare capability can only access the data that exists in the JSON: it can't run functions.

1 Like

The compare condition does not support mustache syntax like the .size extension, so you have to stick with painless here and go with Mike's example.

--Alex

Thanks. I had thought that the default was painless. D'oh!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.