0
0
Elasticsearchquery~10 mins

Completion suggester in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Completion suggester
User types prefix
Elasticsearch receives prefix
Completion suggester queries index
Find matching suggestions
Return suggestions to user
User sees autocomplete options
The completion suggester takes a typed prefix, searches the index for matching suggestions, and returns them to the user for autocomplete.
Execution Sample
Elasticsearch
{
  "suggest": {
    "song-suggest": {
      "prefix": "nir",
      "completion": { "field": "suggest" }
    }
  }
}
This query asks Elasticsearch to suggest completions for the prefix 'nir' using the 'suggest' field.
Execution Table
StepActionInput/StateOutput/Result
1Receive prefix inputUser types 'nir'Prefix = 'nir'
2Query completion suggesterPrefix = 'nir'Search 'suggest' field for matches starting with 'nir'
3Find matching suggestionsIndex contains 'nirvana', 'nina simone', 'nirvikalpa'Matches found: 'nirvana', 'nina simone', 'nirvikalpa'
4Return suggestionsMatches foundSuggestions returned: ['nirvana', 'nina simone', 'nirvikalpa']
5User sees suggestionsSuggestions returnedAutocomplete options shown to user
💡 Execution stops after suggestions are returned to the user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
prefix'''nir''nir''nir''nir''nir'
matches[][][]['nirvana', 'nina simone', 'nirvikalpa']['nirvana', 'nina simone', 'nirvikalpa']['nirvana', 'nina simone', 'nirvikalpa']
suggestions[][][][]['nirvana', 'nina simone', 'nirvikalpa']['nirvana', 'nina simone', 'nirvikalpa']
Key Moments - 3 Insights
Why does the completion suggester only return suggestions starting with the prefix?
Because the completion suggester is designed to match terms that start with the given prefix, as shown in execution_table step 3 where only matches starting with 'nir' are found.
What happens if no matches are found for the prefix?
If no matches are found, the matches list remains empty and no suggestions are returned, as would be seen if step 3 had an empty matches array.
Can the completion suggester return partial matches inside words?
No, it only returns suggestions that start with the prefix, not partial matches inside words, as demonstrated in step 3 where only terms starting with 'nir' are matched.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'matches' after step 3?
A[]
B['nirvana']
C['nirvana', 'nina simone', 'nirvikalpa']
D['simone', 'nirvikalpa']
💡 Hint
Check the 'matches' variable in the variable_tracker after step 3 and the output in execution_table row 3.
At which step does Elasticsearch return the suggestions to the user?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Output' columns in execution_table row 4.
If the user typed 'ni' instead of 'nir', how would the matches change?
AMore matches starting with 'ni' would appear
BFewer matches would appear
CMatches would be the same
DNo matches would appear
💡 Hint
Consider how prefix length affects matches in the completion suggester as shown in execution_table step 3.
Concept Snapshot
Completion suggester syntax:
{
  "suggest": {
    "name": {
      "prefix": "text",
      "completion": { "field": "fieldname" }
    }
  }
}

It returns autocomplete suggestions starting with the prefix.
Only terms starting with prefix are matched.
Useful for fast, prefix-based search suggestions.
Full Transcript
The completion suggester in Elasticsearch works by taking a prefix typed by the user, then searching the index for terms that start with that prefix in a special completion field. The process starts when the user types a prefix like 'nir'. Elasticsearch receives this prefix and queries the completion suggester on the specified field. It finds all matching suggestions that start with 'nir', such as 'nirvana', 'nina simone', and 'nirvikalpa'. These suggestions are then returned to the user to show autocomplete options. The execution table shows each step from receiving input to returning suggestions. Variables like 'prefix', 'matches', and 'suggestions' track the state through the process. Key points include that only terms starting with the prefix are matched, no partial matches inside words are returned, and if no matches exist, no suggestions are returned. The visual quiz tests understanding of the matches after searching, when suggestions are returned, and how changing the prefix affects matches. The concept snapshot summarizes the syntax and behavior of the completion suggester for quick reference.