Completion suggester in Elasticsearch - Time & Space Complexity
When using the completion suggester in Elasticsearch, it's important to understand how the time to find suggestions changes as the number of indexed entries grows.
We want to know how the search speed changes when we add more data to the suggester.
Analyze the time complexity of the following Elasticsearch completion suggester query.
POST /products/_search
{
"suggest": {
"product-suggest": {
"prefix": "iph",
"completion": {
"field": "name_suggest"
}
}
}
}
This query asks Elasticsearch to suggest product names starting with "iph" using the completion suggester on the "name_suggest" field.
In the completion suggester, the main repeating operation is traversing the prefix tree (trie) built from all indexed suggestions.
- Primary operation: Navigating the trie nodes matching the prefix characters.
- How many times: Once per character in the prefix, not per total entries.
The time to find suggestions grows mostly with the length of the prefix typed, not the total number of suggestions stored.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 suggestions | Few steps, depends on prefix length |
| 1000 suggestions | Still about the same steps, depends on prefix length |
| 100,000 suggestions | Still about the same steps, depends on prefix length |
Pattern observation: The search time stays roughly the same as more suggestions are added, because it depends on prefix length, not total suggestions.
Time Complexity: O(k)
This means the time to get suggestions grows linearly with the length of the prefix typed, not with the total number of suggestions stored.
[X] Wrong: "The more suggestions stored, the slower the completion suggester will be."
[OK] Correct: The completion suggester uses a prefix tree, so it jumps directly to the prefix node. The total number of suggestions does not slow down the search much.
Understanding how the completion suggester scales helps you explain efficient search features in real applications, showing you know how data structures affect performance.
What if we changed the prefix to a very common short string? How would that affect the time complexity and the number of results returned?
Practice
completion suggester in Elasticsearch?Solution
Step 1: Understand the role of completion suggester
The completion suggester is designed to quickly suggest words or phrases as a user types input.Step 2: Differentiate from other Elasticsearch features
Full-text search, aggregation, and bulk updates serve different purposes unrelated to autocomplete suggestions.Final Answer:
To provide fast autocomplete suggestions as users type -> Option DQuick Check:
Completion suggester = autocomplete suggestions [OK]
- Confusing completion suggester with full-text search
- Thinking it aggregates data
- Assuming it updates documents
completion field in an Elasticsearch mapping?Solution
Step 1: Identify correct mapping structure
Thecompletionfield must be defined insidepropertieswithtypeset tocompletion.Step 2: Compare options
{ "properties": { "suggest": { "type": "completion" } } } correctly usespropertiesandtype: completion. { "properties": { "suggest": { "type": "text" } } } usestexttype, which is incorrect. { "mappings": { "suggest": { "type": "completion" } } } wrongly placessuggestdirectly undermappings. { "fields": { "suggest": { "type": "keyword" } } } usesfieldsandkeywordtype, which is incorrect.Final Answer:
{ "properties": { "suggest": { "type": "completion" } } } -> Option AQuick Check:
Completion field in properties with type completion = { "properties": { "suggest": { "type": "completion" } } } [OK]
- Placing completion field outside properties
- Using text or keyword type instead of completion
- Misnaming mapping keys
{
"suggest": {
"fruit-suggest": {
"prefix": "app",
"completion": {
"field": "suggest"
}
}
}
}Solution
Step 1: Understand the prefix and completion suggester
The prefix 'app' matches all suggest entries starting with 'app', which includes 'apple', 'applet', and 'application'.Step 2: Predict the output list
The completion suggester returns all matching suggestions starting with the prefix, so all three words will be suggested.Final Answer:
["apple", "applet", "application"] -> Option AQuick Check:
Prefix 'app' matches all three suggestions [OK]
- Expecting partial matches inside words
- Thinking prefix must be exact full word
- Assuming empty or partial results
{
"suggest": {
"my-suggest": {
"prefix": "ban",
"completion": {
"field": "suggestion"
}
}
}
}
Assuming the mapping defines the completion field as suggest (not suggestion).Solution
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'.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.Final Answer:
The field name in completion should be 'suggest', not 'suggestion' -> Option CQuick Check:
Field name mismatch causes error [OK]
- Using wrong field name in completion query
- Thinking prefix value is invalid
- Expecting suggest query inside 'query' key
Solution
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.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.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 BQuick Check:
Use contexts with allowed letters excluding 'x' [OK]
- Using invalid '!x' syntax in contexts
- Trying to use 'filter' inside completion suggester
- Not applying context filtering at all
Ready to prove it?
Pass the test and this pattern moves unknown
