# How to set watcher condition when aggregations doc\_count greater then a value

**URL:** https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [July 23, 2020, 6:10am UTC](https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283 "2020-07-23T06:10:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hanase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hanase/32/40115_2.png) [@hanase](https://discuss.elastic.co/u/hanase)
#### Post date: [July 23, 2020, 6:10am UTC](https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283/1 "2020-07-23T06:10:59Z")

</div>

Hi,

I search heartbeat-\* indices when status is down and want to send message to telegram

My DSL is ,

```auto
GET heartbeat-7.7.0*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "monitor.status": "down"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gt": "now-5m"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "alert": {
      "terms": {
        "field": "monitor.name"
      }
    }
  }
}

```

And aggregations result is

```auto
"aggregations" : {
    "alert" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "BC_Video_alert(BC)",
          "doc_count" : 990
        },
        {
          "key" : "AB_Video",
          "doc_count" : 5
        },
        {
          "key" : "SP_sport_alert(SABA)",
          "doc_count" : 2
        }
      ]
    }
  }

```

Is it passable to use watcher condition syntax, when aggregations.buckets.doc\_count greater then a value

like syntax below, (but i don't know what correct syntax is)

> ctx.payload.aggregations.buckets.doc\_count \> 20

then do some actions

thank you 🙂

---

<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: [July 23, 2020, 7:49am UTC](https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283/2 "2020-07-23T07:49:46Z")

</div>

you can do that, but a workaround might be to specify `min_doc_count` in the `terms` aggregation and then check for `ctx.payload.aggregations.alert.buckets.size > 0`

---

<div class="post-metadata">

### Author: ![hanase](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hanase/32/40115_2.png) [@hanase](https://discuss.elastic.co/u/hanase)
#### Post date: [July 23, 2020, 9:31am UTC](https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283/3 "2020-07-23T09:31:32Z")

</div>

Thank you Alexander 🥰

I fix the DSL syntax and it works !!

```auto
{
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "heartbeat-*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 5,
          "track_total_hits": true,
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "monitor.status": "down"
                  }
                }
              ],
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gt": "now-5m",
                      "time_zone": "+08:00"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "alert": {
              "terms": {
                "field": "monitor.name",
                "min_doc_count": 5
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "return ctx.payload.aggregations.alert.buckets.size() > 0",
      "lang": "painless"
    }
  },
  "actions": {
    "my_webhook": {
      "webhook": {
        "scheme": "https",
        "host": "api.telegram.org"
~~~~~
```

---

<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: [August 20, 2020, 9:31am UTC](https://discuss.elastic.co/t/how-to-set-watcher-condition-when-aggregations-doc-count-greater-then-a-value/242283/4 "2020-08-20T09:31:38Z")

</div>

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