Alerting helps you know when something important happens in your data. Notifications tell you right away so you can act fast.
Alerting and notifications in Elasticsearch
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Elasticsearch
PUT _watcher/watch/<watch_id>
{
"trigger": {
"schedule": { "interval": "10s" }
},
"input": {
"search": {
"request": {
"indices": ["<index_name>"],
"body": {
"query": { "match": { "<field>": "<value>" } }
}
}
}
},
"condition": {
"compare": { "ctx.payload.hits.total.value": { "gt": 0 } }
},
"actions": {
"notify_admin": {
"email": {
"to": "admin@example.com",
"subject": "Alert: Condition met",
"body": "Found matching documents in Elasticsearch."
}
}
}
}The trigger defines when the alert runs.
The input defines what data to check.
Examples
Elasticsearch
PUT _watcher/watch/error_alert
{
"trigger": { "schedule": { "interval": "1m" } },
"input": {
"search": {
"request": {
"indices": ["logs"],
"body": {
"query": { "match": { "level": "error" } }
}
}
}
},
"condition": { "compare": { "ctx.payload.hits.total.value": { "gt": 5 } } },
"actions": {
"send_email": {
"email": {
"to": "devteam@example.com",
"subject": "High error rate detected",
"body": "More than 5 errors found in the last minute."
}
}
}
}Elasticsearch
PUT _watcher/watch/disk_space_alert
{
"trigger": { "schedule": { "interval": "5m" } },
"input": {
"search": {
"request": {
"indices": ["metrics"],
"body": {
"query": { "range": { "disk_free": { "lt": 1024 } } }
}
}
}
},
"condition": { "compare": { "ctx.payload.hits.total.value": { "gt": 0 } } },
"actions": {
"send_slack": {
"webhook": {
"method": "POST",
"url": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
"body": "{ \"text\": \"Disk space is low!\" }"
}
}
}
}Sample Program
This watcher runs every 10 seconds. It searches the 'my_index' for documents where 'status' is 'error'. If it finds any, it sends an email to the admin.
Elasticsearch
PUT _watcher/watch/sample_alert
{
"trigger": {
"schedule": { "interval": "10s" }
},
"input": {
"search": {
"request": {
"indices": ["my_index"],
"body": {
"query": { "match": { "status": "error" } }
}
}
}
},
"condition": {
"compare": { "ctx.payload.hits.total.value": { "gt": 0 } }
},
"actions": {
"notify": {
"email": {
"to": "admin@example.com",
"subject": "Error found in my_index",
"body": "There are error documents in my_index."
}
}
}
}Important Notes
Make sure your Elasticsearch cluster has Watcher enabled.
Actions can be emails, webhooks, Slack messages, or other integrations.
Test your alerts to confirm they work as expected.
Summary
Alerting helps you watch your data and get notified automatically.
Use triggers, inputs, conditions, and actions to build alerts.
Alerts can send emails or messages to keep you informed.
Practice
1. What is the main purpose of alerting in Elasticsearch?
easy
Solution
Step 1: Understand alerting concept
Alerting watches your data and triggers notifications when specific conditions happen.Step 2: Identify main purpose
The main goal is to notify users automatically about important data changes or events.Final Answer:
To automatically notify you when certain data conditions are met -> Option AQuick Check:
Alerting = automatic notifications [OK]
Hint: Alerting means automatic notifications on data changes [OK]
Common Mistakes:
- Confusing alerting with data storage
- Thinking alerting is for data visualization
- Mixing alerting with backup processes
2. Which of the following is the correct syntax to define a trigger in an Elasticsearch alerting watch?
easy
Solution
Step 1: Recall trigger syntax in watch
Triggers use a schedule object with an interval field inside curly braces.Step 2: Match correct JSON structure
"trigger": { "schedule": { "interval": "10m" } } correctly nests schedule and interval inside trigger with proper braces and quotes.Final Answer:
"trigger": { "schedule": { "interval": "10m" } } -> Option AQuick Check:
Trigger uses schedule with interval [OK]
Hint: Trigger syntax always nests schedule and interval inside braces [OK]
Common Mistakes:
- Missing braces around schedule
- Using wrong keys like 'time' instead of 'schedule'
- Incorrect JSON structure without nested objects
3. Given this watch input snippet, what type of input is being used?
{
"input": {
"search": {
"request": {
"indices": ["logs"],
"body": {
"query": { "match_all": {} }
}
}
}
}
}medium
Solution
Step 1: Identify input type from JSON keys
The input uses the key "search" with a request containing indices and a query.Step 2: Match input type to Elasticsearch alerting inputs
This matches the Search input type, which runs a search query on indices.Final Answer:
Search input -> Option CQuick Check:
Input with "search" key = Search input [OK]
Hint: Look for 'search' key to identify Search input type [OK]
Common Mistakes:
- Confusing search input with HTTP or webhook inputs
- Ignoring the 'search' key and guessing script input
- Not recognizing the query structure inside input
4. You wrote this action in your watch but it fails to send an email:
"actions": {
"send_email": {
"email": {
"to": "user@example.com",
"subject": "Alert!",
"body": "Condition met"
}
}
}What is the likely error?medium
Solution
Step 1: Check required fields for email action
Email action requires a 'from' field to specify sender address.Step 2: Identify missing 'from' field
The given action lacks the 'from' field, causing failure to send email.Final Answer:
Missing 'from' field in email action -> Option DQuick Check:
Email action needs 'from' field [OK]
Hint: Email actions always need a 'from' address [OK]
Common Mistakes:
- Assuming 'to' format is wrong when it is correct
- Forgetting to add 'from' sender email
- Thinking trigger absence causes email failure
5. You want to create an alert that sends a Slack message only if the number of errors in logs exceeds 100 in the last 5 minutes. Which condition correctly implements this in the watch?
hard
Solution
Step 1: Understand payload structure for hits total
In Elasticsearch 7+, total hits count is accessed as ctx.payload.hits.total.value.Step 2: Choose correct condition syntax
The compare condition with 'gt' operator on ctx.payload.hits.total.value correctly checks if errors exceed 100.Final Answer:
"condition": { "compare": { "ctx.payload.hits.total.value": { "gt": 100 } } } -> Option BQuick Check:
Use compare with ctx.payload.hits.total.value > 100 [OK]
Hint: Use compare on ctx.payload.hits.total.value for counts [OK]
Common Mistakes:
- Using ctx.payload.hits.total instead of .value
- Using script with wrong field name
- Using 'gte' instead of 'gt' when strictly greater needed
