Bird
Raised Fist0
Elasticsearchquery~20 mins

Search performance tuning 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
🎖️
Search Performance 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 Elasticsearch query?
Given the following Elasticsearch query, what will be the total number of hits returned?
Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "python" } },
        { "range": { "publish_year": { "gte": 2015 } } }
      ]
    }
  },
  "size": 5
}
AReturns 5 documents but ignores the 'range' filter
BReturns all documents matching 'python' in title regardless of publish_year
CReturns 0 documents because 'range' filter is invalid
DReturns the top 5 documents matching 'python' in title published from 2015 onwards
Attempts:
2 left
💡 Hint
Check how the bool query combines must clauses and the size parameter limits results.
🧠 Conceptual
intermediate
2:00remaining
Which setting improves search speed by reducing scoring calculations?
In Elasticsearch, which setting can you use to improve search performance by skipping scoring calculations when relevance is not important?
A"track_total_hits": false
B"track_scores": false
C"terminate_after": 10
D"explain": true
Attempts:
2 left
💡 Hint
Think about early termination when you only need a small number of matching documents, not full relevance ranking.
🔧 Debug
advanced
3:00remaining
Why does this Elasticsearch query cause slow performance?
Consider this query: { "query": { "wildcard": { "username": "*admin*" } } } Why might this query be slow on large datasets?
AWildcard queries with leading * cause full index scans, slowing performance
BThe field 'username' is not indexed, causing errors
CThe wildcard pattern is too short and matches too few documents
DThe query syntax is invalid and causes errors
Attempts:
2 left
💡 Hint
Leading wildcards are known to be expensive in search engines.
📝 Syntax
advanced
2:30remaining
Which option correctly limits search results to documents with a specific field value and sorts by date descending?
Choose the correct Elasticsearch query syntax to filter documents where "status" is "active" and sort results by "created_date" descending.
A
{
  "query": { "term": { "status": "active" } },
  "sort": [{ "created_date": { "order": "desc" } }]
}
B
{
  "query": { "match": { "status": "active" } },
  "sort": { "created_date": "desc" }
}
C
{
  "filter": { "term": { "status": "active" } },
  "sort": [{ "created_date": "desc" }]
}
D
{
  "query": { "term": { "status": "active" } },
  "sort": [{ "created_date": { "order": "asc" } }]
}
Attempts:
2 left
💡 Hint
Check the correct use of term query and sort syntax with order key.
🚀 Application
expert
4:00remaining
How to optimize a search for a large dataset with frequent updates?
You have an Elasticsearch index with millions of documents that update frequently. You want to optimize search performance while keeping data fresh. Which approach is best?
AIncrease the number of shards to 100 to distribute load evenly
BDisable refresh interval completely to speed up indexing and search
CUse index aliases with write and read indices, and refresh the write index frequently while searching the read index
DUse wildcard queries with leading * to match updated documents quickly
Attempts:
2 left
💡 Hint
Think about separating indexing and searching to improve performance and freshness.

Practice

(1/5)
1. Which of the following is a common way to improve search performance in Elasticsearch?
easy
A. Limit the number of results returned using size parameter
B. Increase the number of shards without limit
C. Disable caching completely
D. Use wildcard queries on all fields

Solution

  1. Step 1: Understand result limiting

    Limiting results with size reduces data processed and returned, speeding up queries.
  2. Step 2: Evaluate other options

    Increasing shards without limit can hurt performance, disabling cache reduces speed, and wildcard queries are slow.
  3. Final Answer:

    Limit the number of results returned using size parameter -> Option A
  4. Quick Check:

    Limiting results = faster search [OK]
Hint: Use size to limit results for faster queries [OK]
Common Mistakes:
  • Thinking more shards always improve speed
  • Ignoring caching benefits
  • Using wildcard queries on all fields
2. Which Elasticsearch query syntax correctly limits the returned fields to only title and author?
easy
A. {"return_fields": ["title", "author"], "query": {"match_all": {}}}
B. {"fields": ["title", "author"], "query": {"match_all": {}}}
C. {"select": ["title", "author"], "query": {"match_all": {}}}
D. {"_source": ["title", "author"], "query": {"match_all": {}}}

Solution

  1. Step 1: Identify correct field limiting syntax

    Elasticsearch uses _source to specify which fields to return.
  2. Step 2: Check other options

    fields, select, and return_fields are not valid for limiting returned fields in this context.
  3. Final Answer:

    {"_source": ["title", "author"], "query": {"match_all": {}}} -> Option D
  4. Quick Check:

    Use _source to limit fields [OK]
Hint: Use _source to specify returned fields [OK]
Common Mistakes:
  • Using fields instead of _source
  • Trying SQL-like select syntax
  • Using unsupported keys like return_fields
3. Given this Elasticsearch query, what will be the effect of adding "timeout": "2s"?
{
  "query": {"match": {"content": "fast search"}},
  "timeout": "2s"
}
medium
A. The query will fail if it takes longer than 2 seconds
B. The query will cache results for 2 seconds
C. The query will return partial results after 2 seconds
D. The query will wait 2 seconds before starting

Solution

  1. Step 1: Understand timeout behavior

    Elasticsearch's timeout stops the query after the specified time and returns partial results if available.
  2. Step 2: Evaluate other options

    It does not fail immediately, does not delay start, and does not control caching.
  3. Final Answer:

    The query will return partial results after 2 seconds -> Option C
  4. Quick Check:

    timeout returns partial results [OK]
Hint: Timeout returns partial results if query is slow [OK]
Common Mistakes:
  • Assuming timeout causes query failure
  • Thinking timeout delays query start
  • Confusing timeout with caching duration
4. You have this query to limit results and fields:
{
  "size": 10,
  "query": {
    "_source": ["title", "date"],
    "match_all": {}
  }
}
But the query returns all fields. What is the likely mistake?
medium
A. Using size instead of limit
B. Using _source inside the query body instead of top-level
C. Missing fields parameter to limit fields
D. The match_all query ignores field limits

Solution

  1. Step 1: Check placement of _source

    _source must be at the top level of the query JSON, not inside query.
  2. Step 2: Review other options

    fields is deprecated for this purpose, size is correct, and match_all does not ignore field limits.
  3. Final Answer:

    Using _source inside the query body instead of top-level -> Option B
  4. Quick Check:

    _source must be top-level [OK]
Hint: Place _source at top level, not inside query [OK]
Common Mistakes:
  • Putting _source inside query
  • Confusing size with limit
  • Assuming match_all ignores field filtering
5. You want to optimize a search that returns many documents but only needs the id and summary fields, and must respond within 1 second. Which combination of settings best improves performance?
hard
A. Set size to a low number, use _source to limit fields, and add timeout of 1s
B. Set size high, disable _source, and remove timeout
C. Use wildcard queries on all fields and set timeout to 5s
D. Increase shards count and use fields to limit fields

Solution

  1. Step 1: Limit results and fields

    Setting size low reduces returned documents; _source limits fields to needed ones.
  2. Step 2: Use timeout to keep response fast

    Adding timeout of 1 second ensures query won't hang and keeps system responsive.
  3. Step 3: Evaluate other options

    High size and disabling _source increase load; wildcard queries are slow; increasing shards without need can hurt performance.
  4. Final Answer:

    Set size to a low number, use _source to limit fields, and add timeout of 1s -> Option A
  5. Quick Check:

    Limit size + fields + timeout = best performance [OK]
Hint: Limit size, fields, and add timeout for fast, efficient search [OK]
Common Mistakes:
  • Setting size too high
  • Disabling field filtering
  • Ignoring timeout setting
  • Increasing shards unnecessarily