Bird
Raised Fist0
Elasticsearchquery~20 mins

Wildcard and prefix queries in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Wildcard and Prefix 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 wildcard query?
Given the following Elasticsearch wildcard query, what documents will it match if the index contains documents with the field name having values: "apple", "application", "apply", "banana"?
Elasticsearch
{
  "query": {
    "wildcard": {
      "name": {
        "value": "app*"
      }
    }
  }
}
A["apple", "banana"]
B["apple", "application", "apply"]
C["banana"]
D["application", "banana"]
Attempts:
2 left
💡 Hint
The wildcard * matches zero or more characters after 'app'.
❓ Predict Output
intermediate
2:00remaining
What documents does this prefix query match?
Consider this Elasticsearch prefix query on the field title. Which documents will it match if the titles are: "Introduction to Elasticsearch", "Introductory Guide", "Installation Manual", "Integration Tips"?
Elasticsearch
{
  "query": {
    "prefix": {
      "title": "Intro"
    }
  }
}
A["Installation Manual", "Integration Tips"]
B["Introductory Guide", "Installation Manual"]
C["Introduction to Elasticsearch", "Introductory Guide"]
D["Integration Tips"]
Attempts:
2 left
💡 Hint
Prefix query matches documents where the field starts with the given prefix.
🔧 Debug
advanced
2:00remaining
Why does this wildcard query cause an error?
This wildcard query is intended to match any category starting with 'tech'. Why does it cause a syntax error?
Elasticsearch
{
  "query": {
    "wildcard": {
      "category": "tech*"
    }
  }
}
ABecause the wildcard value must be inside a 'value' field, not directly as a string.
BBecause the wildcard query does not support the '*' character.
CBecause the field name 'category' is invalid in wildcard queries.
DBecause the query must use 'prefix' instead of 'wildcard' for this pattern.
Attempts:
2 left
💡 Hint
Check the expected structure of a wildcard query in Elasticsearch.
❓ Predict Output
advanced
2:00remaining
What is the result count of this prefix query?
If an Elasticsearch index has documents with the field username values: "john123", "johnny", "johanna", "jane", "jack", what is the number of documents matched by this prefix query?
Elasticsearch
{
  "query": {
    "prefix": {
      "username": "john"
    }
  }
}
A4
B1
C3
D2
Attempts:
2 left
💡 Hint
Count usernames starting exactly with 'john'.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains the difference between wildcard and prefix queries?
Select the option that best describes the key difference between wildcard and prefix queries in Elasticsearch.
AWildcard queries allow multiple wildcard characters anywhere in the term, while prefix queries only match terms starting with the given prefix without wildcards.
BPrefix queries support multiple wildcards in the middle of the term, but wildcard queries only support a single wildcard at the end.
CWildcard queries are faster than prefix queries because they use inverted indices more efficiently.
DPrefix queries require the use of regular expressions, while wildcard queries do not.
Attempts:
2 left
💡 Hint
Think about where wildcards can appear in each query type.

Practice

(1/5)
1. What does a wildcard query with * symbol do in Elasticsearch?
easy
A. Matches terms ending with a specific suffix
B. Matches only the exact search term
C. Matches terms starting with a specific prefix
D. Matches any sequence of characters in the search term

Solution

  1. Step 1: Understand the wildcard symbol *

    The * symbol in wildcard queries matches zero or more characters in any position of the term.
  2. Step 2: Compare with other options

    Exact match requires no wildcard, prefix queries use a different query type, and suffix matching is not directly done by * alone.
  3. Final Answer:

    Matches any sequence of characters in the search term -> Option D
  4. Quick Check:

    wildcard * = match any chars [OK]
Hint: Remember * matches any characters, ? matches one character [OK]
Common Mistakes:
  • Confusing wildcard * with prefix query
  • Thinking * matches only one character
  • Assuming wildcard matches exact terms only
2. Which of the following is the correct syntax for a prefix query in Elasticsearch?
easy
A. { "prefix": { "field_name": "pre" } }
B. { "wildcard": { "field_name": "pre*" } }
C. { "match": { "field_name": "pre*" } }
D. { "term": { "field_name": "pre*" } }

Solution

  1. Step 1: Identify prefix query syntax

    Prefix queries use the prefix keyword with the field and the prefix string.
  2. Step 2: Check other options

    Wildcard queries use wildcard, match and term queries do not support prefix syntax with asterisk.
  3. Final Answer:

    { "prefix": { "field_name": "pre" } } -> Option A
  4. Quick Check:

    prefix query uses "prefix" keyword [OK]
Hint: Prefix queries use "prefix" keyword, not wildcard or match [OK]
Common Mistakes:
  • Using wildcard syntax for prefix query
  • Using match query with asterisk
  • Confusing term query with prefix query
3. Given the following wildcard query, which documents will match?
{ "query": { "wildcard": { "name": "jo?n*" } } }

Assuming documents have names: "john", "joan", "jonas", "joanathan", "jo".
medium
A. ["joan", "jonas", "joanathan"]
B. ["john", "joan", "jo"]
C. ["john", "joan", "joanathan"]
D. ["john", "joan", "jonas", "joanathan", "jo"]

Solution

  1. Step 1: Analyze the wildcard pattern "jo?n*"

    The pattern means: 'jo' followed by any single character (due to '?'), then 'n', then any characters (due to '*').
  2. Step 2: Check each name against the pattern

    "john" matches ('h' replaces '?'), "joan" matches ('a' replaces '?'), "joanathan" matches ('a' replaces '?'). "jonas" does not match ('n' replaces '?' but next 'a' ≠ 'n'). "jo" is too short to match.
  3. Final Answer:

    ["john", "joan", "joanathan"] -> Option C
  4. Quick Check:

    wildcard ? = one char, * = any chars [OK]
Hint: ? matches one char, * matches zero or more chars [OK]
Common Mistakes:
  • Assuming ? matches zero characters
  • Including too short strings like "jo"
  • Confusing wildcard with prefix query
4. You wrote this prefix query but it returns no results:
{ "query": { "prefix": { "title": "prog*" } } }

What is the error?
medium
A. Field name "title" is invalid
B. Prefix query should not include the asterisk (*)
C. Prefix queries require wildcard keyword
D. Prefix query must use match instead of prefix

Solution

  1. Step 1: Understand prefix query syntax

    Prefix queries expect a string prefix without wildcards like '*'. The asterisk is not needed and causes no matches.
  2. Step 2: Check other options

    Field name is assumed valid, wildcard keyword is different query type, and match query is unrelated to prefix syntax.
  3. Final Answer:

    Prefix query should not include the asterisk (*) -> Option B
  4. Quick Check:

    Prefix query uses plain prefix string, no * [OK]
Hint: Remove * from prefix query string to fix [OK]
Common Mistakes:
  • Including * in prefix query string
  • Confusing prefix with wildcard query
  • Using match query instead of prefix
5. You want to find all documents where the "username" field starts with "alex" but also want to allow any characters after "alex" including none. Which query is best and why?
{ "query": { "wildcard": { "username": "alex*" } } }
or
{ "query": { "prefix": { "username": "alex" } } }
hard
A. Prefix query is better because it is faster and matches all starting with "alex"
B. Wildcard query is better because it matches "alex" exactly only
C. Wildcard query is better because it matches any characters including none after "alex"
D. Prefix query is better because it requires the * symbol

Solution

  1. Step 1: Compare prefix and wildcard queries for this case

    Both queries match terms starting with "alex" and any characters after. Wildcard uses "alex*", prefix uses "alex" without *.
  2. Step 2: Consider performance and usage

    Prefix queries are optimized and faster for this use case. Wildcard queries are slower and more flexible but unnecessary here.
  3. Final Answer:

    Prefix query is better because it is faster and matches all starting with "alex" -> Option A
  4. Quick Check:

    Prefix query faster for starts-with matching [OK]
Hint: Use prefix query for starts-with, wildcard for complex patterns [OK]
Common Mistakes:
  • Using wildcard when prefix is enough
  • Adding * in prefix query string
  • Thinking wildcard matches exact only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard