Bird
Raised Fist0
Elasticsearchquery~20 mins

Saved searches and filters in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Saved Searches Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this saved search query?

Given this Elasticsearch saved search query, what will be the total number of hits returned?

Elasticsearch
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "status": "active" } },
        { "range": { "age": { "gte": 30 } } }
      ]
    }
  }
}
AReturns all documents with status 'active' and age greater or equal to 30
BReturns all documents with status 'active' or age greater or equal to 30
CReturns documents with status 'active' but ignores age filter
DReturns documents with age greater or equal to 30 but ignores status filter
Attempts:
2 left
💡 Hint

Remember that filters inside a bool query with filter act as AND conditions.

🧠 Conceptual
intermediate
1:30remaining
Which filter type is best for exact matches in saved searches?

When creating saved searches in Elasticsearch, which filter type should you use to match exact values efficiently?

Amatch filter
Bterm filter
Crange filter
Dexists filter
Attempts:
2 left
💡 Hint

Exact matches require filters that do not analyze the field.

🔧 Debug
advanced
2:30remaining
Why does this saved search filter not work as expected?

Look at this saved search filter. It should return documents where category is either 'books' or 'electronics'. Why does it return no results?

Elasticsearch
{
  "query": {
    "bool": {
      "filter": {
        "term": { "category": ["books", "electronics"] }
      }
    }
  }
}
AThe filter should be inside a must clause, not filter
BThe category field is missing in the index mapping
CThe term filter does not accept an array of values; it expects a single value
DThe query syntax is correct; the problem is with the data
Attempts:
2 left
💡 Hint

Check the expected input type for the term filter.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this saved search filter

Which option contains the correct syntax for a saved search filter that matches documents with status 'pending' and priority greater than 5?

A{ "bool": { "filter": [ { "term": { "status": "pending" } }, { "range": { "priority": { "gt": "5" } } } ] } }
B{ "bool": { "filter": [ { "term": { "status": "pending" } }, { "range": { "priority": { "gte": 5 } } } ] } }
C{ "bool": { "filter": [ { "term": { "status": "pending" } }, { "range": { "priority": { "greater_than": 5 } } } ] } }
D{ "bool": { "filter": [ { "term": { "status": "pending" } }, { "range": { "priority": { "gt": 5 } } } ] } }
Attempts:
2 left
💡 Hint

Check the correct operator name and value types in range queries.

🚀 Application
expert
3:00remaining
How many filters are applied in this saved search?

Consider this saved search query. How many filters are effectively applied to the documents?

Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "term": { "status": "active" } },
        { "range": { "age": { "gte": 25 } } }
      ],
      "filter": [
        { "term": { "verified": true } },
        { "range": { "score": { "gt": 50 } } }
      ]
    }
  }
}
A4 filters
B3 filters
C2 filters
D5 filters
Attempts:
2 left
💡 Hint

Count all conditions inside must and filter arrays.

Practice

(1/5)
1. What is the main purpose of a saved search in Elasticsearch?
easy
A. To create visual charts from data
B. To store raw data permanently
C. To reuse a query easily without rewriting it every time
D. To delete old data automatically

Solution

  1. Step 1: Understand what saved searches do

    Saved searches store queries so you can run them again without rewriting.
  2. 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.
  3. Final Answer:

    To reuse a query easily without rewriting it every time -> Option C
  4. Quick 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
A. {"query": {"match_all": {}}, "filter": {"term": {"status": "active"}}}
B. {"query": {"filtered": {"filter": {"term": {"status": "active"}}}}}
C. {"query": {"term": {"status": "active"}}}
D. {"filter": {"match": {"status": "active"}}}

Solution

  1. Step 1: Recall filter syntax in Elasticsearch saved searches

    Filters are applied inside a filtered query using the "filtered" key.
  2. 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.
  3. Final Answer:

    {"query": {"filtered": {"filter": {"term": {"status": "active"}}}}} -> Option B
  4. Quick 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
A. Documents with title containing 'book' and status 'published'
B. Documents with title containing 'book' or status 'published'
C. Documents with status 'published' only
D. Documents with title containing 'book' only

Solution

  1. Step 1: Analyze the query and filter parts

    The query matches documents where title contains 'book'. The filter restricts to status 'published'.
  2. Step 2: Understand filtered query behavior

    Filtered query returns documents matching both query and filter conditions (AND logic).
  3. Final Answer:

    Documents with title containing 'book' and status 'published' -> Option A
  4. Quick 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:
{"query": {"filtered": {"query": {"match": {"content": "test"}}, "filter": {"term": {"category": "news"}}}}

What is wrong with this JSON?
medium
A. The 'match' query is invalid inside filtered
B. Using 'term' filter instead of 'match'
C. Query should not have a filter
D. Missing closing braces at the end

Solution

  1. Step 1: Check JSON structure carefully

    The JSON snippet ends without closing all opened braces, causing syntax error.
  2. Step 2: Verify other parts are valid

    Using 'term' filter and 'match' query inside filtered is correct syntax.
  3. Final Answer:

    Missing closing braces at the end -> Option D
  4. Quick 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
A. {"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}}
B. {"query": {"filtered": {"filter": {"term": {"status": "active"}, "terms": {"priority": ["high", "medium"]}}}}}
C. {"query": {"filtered": {"filter": {"or": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}}]}}}}
D. {"query": {"filtered": {"filter": {"must": [{"term": {"status": "active"}}, {"term": {"priority": "high"}}, {"term": {"priority": "medium"}}]}}}}

Solution

  1. Step 1: Understand the filter requirements

    Status must be 'active' AND priority must be 'high' OR 'medium'.
  2. Step 2: Identify correct bool filter usage

    Use 'must' for AND conditions and 'terms' for multiple values in one field.
  3. 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.
  4. Final Answer:

    {"query": {"filtered": {"filter": {"bool": {"must": [{"term": {"status": "active"}}, {"terms": {"priority": ["high", "medium"]}]}}}}}} -> Option A
  5. Quick 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