# Index\_out\_of\_bounds\_exception watcher using aggregration

**URL:** <https://discuss.elastic.co/t/index-out-of-bounds-exception-watcher-using-aggregration/170376>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [February 28, 2019, 3:36pm UTC](https://discuss.elastic.co/t/index-out-of-bounds-exception-watcher-using-aggregration/170376 "2019-02-28T15:36:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Saleem](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Muhammad\_Saleem](https://discuss.elastic.co/u/Muhammad_Saleem)\
**Post date:** [February 28, 2019, 3:36pm UTC](https://discuss.elastic.co/t/index-out-of-bounds-exception-watcher-using-aggregration/170376/1 "2019-02-28T15:36:09Z")

</div>

Here is my code to create a watcher, which is trying to compare two values from the aggregated values from the payload.  
The problem is when there is no data being context by the condition in the watcher, it returns the

```auto

```

"exception": {  
"type": "index\_out\_of\_bounds\_exception",  
"reason": "Index: 1, Size: 0"  
}

```auto

```

Here is the watcher json

```auto

```

{  
"trigger": {  
"schedule": {  
"interval": "12h"  
}  
},  
"input": {  
"search": {  
"request": {  
"search\_type": "query\_then\_fetch",  
"indices": [  
"pbs\_logs\*"  
],  
"types": ,  
"body": {  
"query": {  
"bool": {  
"filter": {  
"range": {  
"wall\_clock\_time": {  
"from": "now-10h",  
"to": "now"  
}  
}  
}  
}  
},  
"aggs": {  
"group\_by\_search": {  
"terms": {  
"field": "data.search-version.keyword",  
"size": 2,  
"order": {  
"\_key": "asc"  
}  
}  
}  
}  
}  
}  
}  
},  
"condition": {  
"compare": {  
"ctx.payload.aggregations.group\_by\_search.buckets.0.doc\_count": {  
"gte": "{{ctx.payload.aggregations.group\_by\_search.buckets.1.doc\_count}}"  
}  
}  
},  
"actions": {}  
}

```auto

```

How can I make the watcher able to check if the response from the condition is empty then do not proceed with the execution

---

<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:** [March 20, 2019, 8:59am UTC](https://discuss.elastic.co/t/index-out-of-bounds-exception-watcher-using-aggregration/170376/2 "2019-03-20T08:59:11Z")

</div>

you should use a `script` based condition and check for the size of the buckets array.

See the [alerting examples repo](https://github.com/elastic/examples/tree/master/Alerting) for some inspiration to copy from.

--Alex

---

<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:** [April 17, 2019, 9:09am UTC](https://discuss.elastic.co/t/index-out-of-bounds-exception-watcher-using-aggregration/170376/3 "2019-04-17T09:09:15Z")

</div>

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