0
0
Elasticsearchquery~10 mins

Wildcard and prefix queries in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Wildcard and prefix queries
Start Query
Check Query Type
Prefix?
Build prefix
Search index with pattern
Return matching documents
End
The query starts by checking if it is a prefix or wildcard query, builds the pattern accordingly, searches the index, and returns matching documents.
Execution Sample
Elasticsearch
GET /my_index/_search
{
  "query": {
    "wildcard": { "user": "ki*y" }
  }
}
This query searches for documents where the 'user' field matches the pattern 'ki*y', where '*' matches any characters.
Execution Table
StepQuery PartActionPattern UsedResult
1Receive queryParse JSON querywildcard on userReady to process
2Identify query typeCheck if prefix or wildcardwildcardProceed with wildcard logic
3Build patternUse 'ki*y' as patternki*yPattern ready
4Search indexMatch documents with user matching 'ki*y'ki*yDocuments found: user='kitty', user='kiry'
5Return resultsSend matching documents backN/A2 documents returned
6EndQuery completeN/ADone
💡 Query ends after returning all documents matching the wildcard pattern.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
query_typeundefinedwildcardwildcardwildcardwildcard
patternundefinedundefinedki*yki*yki*y
matched_docsemptyemptyempty['kitty', 'kiry']['kitty', 'kiry']
Key Moments - 2 Insights
Why does the wildcard query use '*' in 'ki*y'?
The '*' is a special character that matches any sequence of characters, so 'ki*y' matches any word starting with 'ki' and ending with 'y', like 'kitty' or 'kiry' as shown in step 4 of the execution_table.
How is a prefix query different from a wildcard query?
A prefix query only matches documents where the field starts with a given string, without wildcards inside. The execution_table shows the query type check at step 2, where prefix and wildcard queries are handled differently.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pattern used at step 3?
A"*y"
B"ki"
C"ki*y"
D"k*y"
💡 Hint
Check the 'Pattern Used' column at step 3 in the execution_table.
At which step does the query identify it is a wildcard query?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Query Part' and 'Action' columns in the execution_table for step 2.
If the query was a prefix query instead, how would the pattern change?
AIt would use a pattern like "ki*" without other wildcards
BIt would use the same pattern "ki*y"
CIt would use a regex pattern
DIt would not use any pattern
💡 Hint
Recall the difference between prefix and wildcard queries explained in key_moments and the concept_flow.
Concept Snapshot
Wildcard and prefix queries in Elasticsearch:
- Wildcard queries use '*' and '?' to match any characters.
- Prefix queries match documents starting with a given string.
- Wildcard queries can match anywhere in the string.
- Prefix queries are faster and simpler.
- Use wildcard for flexible matching, prefix for fixed starts.
Full Transcript
This visual execution trace shows how Elasticsearch processes wildcard and prefix queries. The query starts by parsing the JSON input, then identifies if it is a prefix or wildcard query. For a wildcard query like 'ki*y', it builds the pattern and searches the index for matching documents. The example finds documents with user fields like 'kitty' and 'kiry'. Variables like query_type, pattern, and matched_docs change step by step. Key moments clarify why '*' is used and how prefix queries differ. The quiz tests understanding of pattern usage and query type identification. The snapshot summarizes the main points for quick recall.