Saved searches and filters help you quickly find the same data again without typing the search every time.
Saved searches and filters 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
POST /_search/template
{
"id": "my_saved_search",
"params": {
"filter_value": "example"
}
}Saved searches are stored as search templates with an ID.
You can pass parameters to customize the saved search when running it.
Examples
my_saved_search that looks for a message matching a value you provide.Elasticsearch
PUT /_scripts/my_saved_search
{
"script": {
"lang": "mustache",
"source": "{\"query\":{\"match\":{\"message\":\"{{filter_value}}\"}}}"
}
}Elasticsearch
POST /_search/template
{
"id": "my_saved_search",
"params": {
"filter_value": "error"
}
}Elasticsearch
GET /my-index/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "status": "active" } },
{ "range": { "age": { "gte": 30 } } }
]
}
}
}Sample Program
This program first saves a search template that looks for documents with a title matching a term you give. Then it runs that saved search with the term "Elasticsearch".
Elasticsearch
PUT /_scripts/saved_search_example
{
"script": {
"lang": "mustache",
"source": "{\"query\":{\"match\":{\"title\":\"{{search_term}}\"}}}"
}
}
POST /_search/template
{
"id": "saved_search_example",
"params": {
"search_term": "Elasticsearch"
}
}Important Notes
Saved searches are stored as scripts or templates in Elasticsearch.
You can update saved searches anytime by changing the script.
Filters help narrow down results without changing the main query.
Summary
Saved searches let you reuse queries easily.
Filters help focus on specific data in your searches.
You run saved searches by calling their ID and passing parameters.
Practice
1. What is the main purpose of a saved search in Elasticsearch?
easy
Solution
Step 1: Understand what saved searches do
Saved searches store queries so you can run them again without rewriting.Step 2: Compare options to this purpose
Only To reuse a query easily without rewriting it every time describes reusing queries easily, which matches saved searches.Final Answer:
To reuse a query easily without rewriting it every time -> Option CQuick Check:
Saved searches = reuse queries [OK]
Hint: Saved searches store queries for reuse, not data or visuals [OK]
Common Mistakes:
- Confusing saved searches with data storage
- Thinking saved searches create charts
- Assuming saved searches delete data
2. Which of the following is the correct JSON structure to apply a filter in a saved search?
easy
Solution
Step 1: Recall filter syntax in Elasticsearch saved searches
Filters are applied inside a filtered query using the "filtered" key.Step 2: Check each option's structure
{"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} correctly uses "query": {"filtered": {"filter": {...}}} which is the right way to apply filters.Final Answer:
{"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} -> Option BQuick Check:
Filter inside filtered query = {"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} [OK]
Hint: Filters go inside a filtered query block in JSON [OK]
Common Mistakes:
- Putting filter outside query block
- Using match instead of term for exact filter
- Missing filtered wrapper for filters
3. Given this saved search JSON snippet, what documents will it return?
{"query": {"filtered": {"query": {"match": {"title": "book"}}, "filter": {"term": {"status": "published"}}}}}medium
Solution
Step 1: Analyze the query and filter parts
The query matches documents where title contains 'book'. The filter restricts to status 'published'.Step 2: Understand filtered query behavior
Filtered query returns documents matching both query and filter conditions (AND logic).Final Answer:
Documents with title containing 'book' and status 'published' -> Option AQuick Check:
Filtered query = query AND filter [OK]
Hint: Filtered queries combine query and filter with AND logic [OK]
Common Mistakes:
- Thinking query and filter use OR logic
- Ignoring the filter part
- Confusing match and term filters
4. You have this saved search JSON:
What is wrong with this JSON?
{"query": {"filtered": {"query": {"match": {"content": "test"}}, "filter": {"term": {"category": "news"}}}}What is wrong with this JSON?
medium
Solution
Step 1: Check JSON structure carefully
The JSON snippet ends without closing all opened braces, causing syntax error.Step 2: Verify other parts are valid
Using 'term' filter and 'match' query inside filtered is correct syntax.Final Answer:
Missing closing braces at the end -> Option DQuick Check:
JSON must be properly closed [OK]
Hint: Count opening and closing braces to spot JSON errors [OK]
Common Mistakes:
- Ignoring missing braces causing syntax errors
- Thinking 'term' filter is wrong here
- Assuming filters can't be inside queries
5. You want to create a saved search that filters documents where 'status' is 'active' and 'priority' is either 'high' or 'medium'. Which JSON filter correctly represents this?
hard
Solution
Step 1: Understand the filter requirements
Status must be 'active' AND priority must be 'high' OR 'medium'.Step 2: Identify correct bool filter usage
Use 'must' for AND conditions and 'terms' for multiple values in one field.Step 3: Check each option
{"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}} uses 'bool' with 'must' array containing 'term' for status and 'terms' for priority, correctly matching requirements.Final Answer:
{"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}} -> Option AQuick Check:
Bool must + terms array = correct filter [OK]
Hint: Use bool must with terms array for AND + multiple values [OK]
Common Mistakes:
- Using 'or' instead of 'must' for AND logic
- Putting multiple filters without bool wrapper
- Using multiple term filters for same field instead of terms
