# Watcher condition against buckets

**URL:** <https://discuss.elastic.co/t/watcher-condition-against-buckets/275870>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [June 14, 2021, 4:20pm UTC](https://discuss.elastic.co/t/watcher-condition-against-buckets/275870 "2021-06-14T16:20:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgreen](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@rgreen](https://discuss.elastic.co/u/rgreen)\
**Post date:** [June 14, 2021, 4:20pm UTC](https://discuss.elastic.co/t/watcher-condition-against-buckets/275870/1 "2021-06-14T16:20:03Z")

</div>

I need to be able to write a condition for a watcher that will alert when the results from the Bucket Critical doc\_count is at 50% or higher of bucket Info doc\_count

This is the aggregation results

```auto
  "aggregations" : {
    "level" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "INFO",
          "doc_count" : 662
        },
        {
          "key" : "Critical",
          "doc_count" : 5
        }
      ]
    }
  }

```

---

<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:** [June 21, 2021, 8:27am UTC](https://discuss.elastic.co/t/watcher-condition-against-buckets/275870/2 "2021-06-21T08:27:04Z")

</div>

this can be done with a `script` condition that accesses the fields in the `aggregations.level.buckets` array and then divides the both `doc_count` fields from each other (make sure to figure out the order first to not accidently switch dividend/divisor).

If you share what you tried so far, we can take a look at improving your script. As a start there is the [examples repo](https://github.com/elastic/examples/tree/master/Alerting) where you can take a look at a few scripts.

---

<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:** [July 19, 2021, 8:27am UTC](https://discuss.elastic.co/t/watcher-condition-against-buckets/275870/3 "2021-07-19T08:27:33Z")

</div>

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