📖 Scenario: You are working with a small Elasticsearch index that stores information about books in a library. You want to find books by searching for titles that start with certain letters or contain certain patterns.
🎯 Goal: Build Elasticsearch queries using wildcard and prefix to find books by title patterns.
📋 What You'll Learn
Create an Elasticsearch index called library with a title field
Write a wildcard query to find titles containing a specific pattern
Write a prefix query to find titles starting with a specific prefix
Print the JSON query objects for both queries
💡 Why This Matters
🌍 Real World
Searching books, products, or documents by partial text matches is common in search engines and apps.
💼 Career
Knowing how to write wildcard and prefix queries helps you build flexible search features in Elasticsearch for many real-world applications.
Progress0 / 4 steps
1
Create the Elasticsearch index mapping
Create a JSON object called library_index with a mappings field that defines a title field of type text.
Elasticsearch
Hint
Define library_index as a dictionary with mappings and properties for title.
2
Create a wildcard query for titles containing 'magic'
Create a JSON object called wildcard_query that uses a wildcard query on the title field with the value *magic*.
Elasticsearch
Hint
Use the wildcard query with title and value *magic*.
3
Create a prefix query for titles starting with 'harry'
Create a JSON object called prefix_query that uses a prefix query on the title field with the value harry.
Elasticsearch
Hint
Use the prefix query with title and value harry.
4
Print both queries
Write two print statements to display the wildcard_query and prefix_query JSON objects.
Elasticsearch
Hint
Use print(wildcard_query) and print(prefix_query).
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
Step 1: Understand the wildcard symbol *
The * symbol in wildcard queries matches zero or more characters in any position of the term.
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.
Final Answer:
Matches any sequence of characters in the search term -> Option D
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
Step 1: Identify prefix query syntax
Prefix queries use the prefix keyword with the field and the prefix string.
Step 2: Check other options
Wildcard queries use wildcard, match and term queries do not support prefix syntax with asterisk.
Final Answer:
{ "prefix": { "field_name": "pre" } } -> Option A
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
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 '*').
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.
Final Answer:
["john", "joan", "joanathan"] -> Option C
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
Step 1: Understand prefix query syntax
Prefix queries expect a string prefix without wildcards like '*'. The asterisk is not needed and causes no matches.
Step 2: Check other options
Field name is assumed valid, wildcard keyword is different query type, and match query is unrelated to prefix syntax.
Final Answer:
Prefix query should not include the asterisk (*) -> Option B
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?