# How do I setup watcher to only alert on new messages?

**URL:** <https://discuss.elastic.co/t/how-do-i-setup-watcher-to-only-alert-on-new-messages/27330>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [August 13, 2015, 1:34pm UTC](https://discuss.elastic.co/t/how-do-i-setup-watcher-to-only-alert-on-new-messages/27330 "2015-08-13T13:34:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![skearns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skearns/32/125945_2.png) [@skearns](https://discuss.elastic.co/u/skearns)\
**Post date:** [August 13, 2015, 2:31pm UTC](https://discuss.elastic.co/t/how-do-i-setup-watcher-to-only-alert-on-new-messages/27330/2 "2015-08-13T14:31:13Z")

</div>

Hi Dennis,

Your query is currently setup to search across _all time_, so once you have one record in the system that matches, you will get a match every time the query executes. You can use simple date math in the query portion to filter just to recent documents.

Here's a slightly modified watch I used in the recent [Watcher Webinar](https://www.elastic.co/webinars/watcher-practical-alerting-for-elasticsearch). Note the way the query section is written - I filter the results to just the results that occurred in the last 25 seconds. In this case, I setup a slightly larger time range for my query than I do for my trigger interval, just to make sure I don't miss anything that might have been in-flight on it's way into ES.

```json
PUT _watcher/watch/twitter_watcher_mention
{
  "trigger": {
    "schedule": {
      "interval": "20s"
    }
  },
  "input": {
    "search": {
      "request": {
        "indices": ["logstash-*"],
        "body": {
          "query": {
            "filtered": {
              "query": {
                "match_phrase": {"message": "my_event_to_match"}
              },
              "filter": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "@timestamp": {
                          "gte": "now-25s"
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  },
  "actions": {
    "log" : {
       "logging" : {
         "text" : "There were {{ctx.payload.hits.total}} Tweets at {{ctx.execution_time}}"
       }
     }
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/how-do-i-setup-watcher-to-only-alert-on-new-messages/27330)._
