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

**URL:** <https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [April 13, 2018, 11:13pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032 "2018-04-13T23:13:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamthealex](https://avatars.discourse-cdn.com/v4/letter/i/e9c0ed/32.png) [@iamthealex](https://discuss.elastic.co/u/iamthealex)\
**Post date:** [April 13, 2018, 11:13pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032/1 "2018-04-13T23:13:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [April 14, 2018, 1:35pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032/2 "2018-04-14T13:35:28Z")

</div>

@iamthealex I think a [script condition](https://www.elastic.co/guide/en/x-pack/current/condition-script.html) would do what you're looking for:

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

```

The `.size()` function is a [Painless](https://www.elastic.co/guide/en/elasticsearch/painless/6.2/index.html) 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.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 14, 2018, 9:43pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032/3 "2018-04-14T21:43:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iamthealex](https://avatars.discourse-cdn.com/v4/letter/i/e9c0ed/32.png) [@iamthealex](https://discuss.elastic.co/u/iamthealex)\
**Post date:** [April 16, 2018, 7:29pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032/4 "2018-04-16T19:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 14, 2018, 7:29pm UTC](https://discuss.elastic.co/t/cant-get-size-of-array-list-in-watcher-condition/128032/5 "2018-05-14T19:29:47Z")

</div>

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