Bird
Raised Fist0
Elasticsearchquery~10 mins

Completion suggester in Elasticsearch - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the completion suggester in Elasticsearch?
easy
A. To update documents in bulk
B. To perform full-text search on documents
C. To aggregate data based on terms
D. To provide fast autocomplete suggestions as users type

Solution

  1. Step 1: Understand the role of completion suggester

    The completion suggester is designed to quickly suggest words or phrases as a user types input.
  2. Step 2: Differentiate from other Elasticsearch features

    Full-text search, aggregation, and bulk updates serve different purposes unrelated to autocomplete suggestions.
  3. Final Answer:

    To provide fast autocomplete suggestions as users type -> Option D
  4. Quick Check:

    Completion suggester = autocomplete suggestions [OK]
Hint: Completion suggester = fast autocomplete feature [OK]
Common Mistakes:
  • Confusing completion suggester with full-text search
  • Thinking it aggregates data
  • Assuming it updates documents
2. Which of the following is the correct way to define a completion field in an Elasticsearch mapping?
easy
A. { "properties": { "suggest": { "type": "completion" } } }
B. { "properties": { "suggest": { "type": "text" } } }
C. { "mappings": { "suggest": { "type": "completion" } } }
D. { "fields": { "suggest": { "type": "keyword" } } }

Solution

  1. Step 1: Identify correct mapping structure

    The completion field must be defined inside properties with type set to completion.
  2. Step 2: Compare options

    { "properties": { "suggest": { "type": "completion" } } } correctly uses properties and type: completion. { "properties": { "suggest": { "type": "text" } } } uses text type, which is incorrect. { "mappings": { "suggest": { "type": "completion" } } } wrongly places suggest directly under mappings. { "fields": { "suggest": { "type": "keyword" } } } uses fields and keyword type, which is incorrect.
  3. Final Answer:

    { "properties": { "suggest": { "type": "completion" } } } -> Option A
  4. Quick Check:

    Completion field in properties with type completion = { "properties": { "suggest": { "type": "completion" } } } [OK]
Hint: Completion field must be under properties with type 'completion' [OK]
Common Mistakes:
  • Placing completion field outside properties
  • Using text or keyword type instead of completion
  • Misnaming mapping keys
3. Given the following suggest query, what will be the expected output if the index contains documents with suggest fields 'apple', 'applet', and 'application'?
{
  "suggest": {
    "fruit-suggest": {
      "prefix": "app",
      "completion": {
        "field": "suggest"
      }
    }
  }
}
medium
A. ["apple", "applet", "application"]
B. ["app", "apple", "applet"]
C. ["application"]
D. []

Solution

  1. Step 1: Understand the prefix and completion suggester

    The prefix 'app' matches all suggest entries starting with 'app', which includes 'apple', 'applet', and 'application'.
  2. Step 2: Predict the output list

    The completion suggester returns all matching suggestions starting with the prefix, so all three words will be suggested.
  3. Final Answer:

    ["apple", "applet", "application"] -> Option A
  4. Quick Check:

    Prefix 'app' matches all three suggestions [OK]
Hint: Prefix matches all suggestions starting with it [OK]
Common Mistakes:
  • Expecting partial matches inside words
  • Thinking prefix must be exact full word
  • Assuming empty or partial results
4. Identify the error in this suggest query:
{
  "suggest": {
    "my-suggest": {
      "prefix": "ban",
      "completion": {
        "field": "suggestion"
      }
    }
  }
}
Assuming the mapping defines the completion field as suggest (not suggestion).
medium
A. The prefix value 'ban' is invalid
B. The suggest name 'my-suggest' is not allowed
C. The field name in completion should be 'suggest', not 'suggestion'
D. The suggest query must be inside 'query' key

Solution

  1. Step 1: Check field name in completion suggester

    The completion suggester must reference the correct field name defined in the mapping. Here, the mapping uses 'suggest', but the query uses 'suggestion'.
  2. Step 2: Validate other parts of the query

    The prefix 'ban' is valid, suggest name 'my-suggest' is allowed, and suggest queries do not require a 'query' key.
  3. Final Answer:

    The field name in completion should be 'suggest', not 'suggestion' -> Option C
  4. Quick Check:

    Field name mismatch causes error [OK]
Hint: Field name in query must match mapping exactly [OK]
Common Mistakes:
  • Using wrong field name in completion query
  • Thinking prefix value is invalid
  • Expecting suggest query inside 'query' key
5. You want to implement a completion suggester that suggests city names but excludes any suggestions that start with the letter 'x'. Which approach correctly applies this filter in the suggest query?
hard
A. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "skip_duplicates": true } } } }
B. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "contexts": { "first_letter": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "z"] } } } } }
C. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "contexts": { "first_letter": ["!x"] } } } } }
D. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "filter": { "term": { "first_letter": "x" } } } } } }

Solution

  1. Step 1: Understand filtering with contexts in completion suggester

    Completion suggester supports filtering using contexts. To exclude suggestions starting with 'x', you include all allowed first letters except 'x' in the context filter.
  2. Step 2: Analyze each option

    { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "skip_duplicates": true } } } } has no filtering. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "contexts": { "first_letter": ["!x"] } } } } } tries to exclude 'x' with '!x' which is invalid syntax. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "contexts": { "first_letter": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "z"] } } } } } explicitly lists all letters except 'x' in contexts, correctly filtering out 'x'. { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "filter": { "term": { "first_letter": "x" } } } } } } uses a 'filter' key which is not supported in completion suggester.
  3. Final Answer:

    { "suggest": { "city-suggest": { "prefix": "", "completion": { "field": "city_suggest", "contexts": { "first_letter": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "z"] } } } } } -> Option B
  4. Quick Check:

    Use contexts with allowed letters excluding 'x' [OK]
Hint: Use contexts with allowed letters, exclude 'x' by omission [OK]
Common Mistakes:
  • Using invalid '!x' syntax in contexts
  • Trying to use 'filter' inside completion suggester
  • Not applying context filtering at all
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