# Watcher advice

**URL:** <https://discuss.elastic.co/t/watcher-advice/94627>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [July 26, 2017, 11:26am UTC](https://discuss.elastic.co/t/watcher-advice/94627 "2017-07-26T11:26:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [July 26, 2017, 11:26am UTC](https://discuss.elastic.co/t/watcher-advice/94627/1 "2017-07-26T11:26:51Z")

</div>

Hi All,

I created below watcher and although I do get results I am wondering if the condition is working as I would expect it to be.

I get the top 5 hdr\_subjects trough a aggregate. Currently I think that if one of the 5 uniq hdr\_subjects is above 10 it will return true and the script is done and the action is executed. Is this correct?

```
{
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "clog-*"
        ],
        "types": [
          "logs"
        ],
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "must": [
                {
                  "query_string": {
                    "query": "program:GW AND EnvID:ukmail-mxin AND _exists_:hdr_subject",
                    "analyze_wildcard": true
                  }
                },
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-5m",
                      "lte": "now"
                    }
                  }
                }
              ],
              "must_not": []
            }
          },
          "aggs": {
            "subjects": {
              "terms": {
                "field": "hdr_subject.keyword",
                "size": 5,
                "order": {
                  "_count": "desc"
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "inline": "if (ctx.payload.hits.total < 1) return false; for(int i=0; i < ctx.payload.aggregations.subjects.buckets.size(); i++) if (ctx.payload.aggregations.subjects.buckets[i].doc_count > 10) return true",
      "lang": "painless"
    }
  },
  "actions": {
    "standard_account": {
      "throttle_period_in_millis": 900000,
      "email": {
        "profile": "standard",
        "attachments": {
          "attached_data": {
            "data": {
              "format": "json"
            }
          },
          "dashboard.pdf": {
            "reporting": {
              "url": "http://localhost:5601/api/reporting/generate/dashboard/5bcaf390-71eb-11e7-ae4e-e7e0ad2f3161"
            }
          }
        },
        "priority": "high",
        "to": [
          "xxxx@xxx.com"
        ],
        "subject": "Possible spam run detected. Encountered {{ctx.payload.hits.total}} subjects in 5 minutes",
        "body": {
          "text": "Too many error in the system, see attached data"
        }
      }
    }
  }
}
```

---

<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 28, 2017, 7:41am UTC](https://discuss.elastic.co/t/watcher-advice/94627/2 "2017-07-28T07:41:15Z")

</div>

Hey,

the `terms` aggregation supports a [min\_doc\_count](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-aggregations-bucket-terms-aggregation.html#_minimum_document_count_3) parameter to only return aggregations that have a certain numbre of elements. This way you only need to check for size of buckets greather than 0 and thats it, which should simplify your condition.

--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:** [August 25, 2017, 7:41am UTC](https://discuss.elastic.co/t/watcher-advice/94627/3 "2017-08-25T07:41:19Z")

</div>

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