Phrase suggestions (did you mean) in Elasticsearch - Time & Space Complexity
When Elasticsearch suggests alternative phrases for misspelled queries, it runs extra checks to find close matches.
We want to understand how the time needed grows as the number of possible suggestions increases.
Analyze the time complexity of the following Elasticsearch phrase suggestion query.
POST /_search
{
"suggest": {
"phrase-suggest": {
"text": "quikc brown fox",
"phrase": {
"field": "content",
"size": 5
}
}
}
}
This query asks Elasticsearch to suggest correct phrases similar to "quikc brown fox" from the "content" field, returning up to 5 suggestions.
Look at what repeats when Elasticsearch processes phrase suggestions.
- Primary operation: Checking each candidate phrase against the input phrase to find close matches.
- How many times: For each word in the input phrase, Elasticsearch compares it to many possible terms in the index.
As the input phrase gets longer or the index has more terms, the number of comparisons grows.
| Input Size (words) | Approx. Operations |
|---|---|
| 3 | Thousands of comparisons for each word |
| 10 | Many tens of thousands of comparisons |
| 100 | Millions of comparisons |
Pattern observation: The work grows roughly with the number of words times the number of candidate terms, so it increases quickly as input or index size grows.
Time Complexity: O(n * m)
This means the time grows proportionally to the number of words in the input phrase (n) times the number of candidate terms in the index (m).
[X] Wrong: "The suggestion time depends only on the input phrase length, not on the index size."
[OK] Correct: Elasticsearch must compare each input word to many terms in the index, so a larger index means more work and longer time.
Understanding how suggestion queries scale helps you explain search performance and design better user experiences.
"What if we limited the number of candidate terms Elasticsearch checks for each word? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand phrase suggestions purpose
Phrase suggestions are designed to detect and correct typos or mistakes in user search queries.Step 2: Compare with other features
Sorting, filtering, and aggregations serve different purposes unrelated to typo correction.Final Answer:
To help fix typos and suggest correct phrases in search queries -> Option AQuick Check:
Phrase suggestions = typo correction [OK]
- Confusing phrase suggestions with sorting or filtering
- Thinking phrase suggestions aggregate data
- Assuming phrase suggestions change document content
Solution
Step 1: Identify phrase suggestion syntax
Phrase suggestions use the "phrase" type inside the suggest block with a specified field.Step 2: Check other options
Options A, B, and D use other suggestion types like completion, term, or match, which are not phrase suggestions.Final Answer:
"suggest": {"my-suggestion": {"text": "exampl", "phrase": {"field": "content"}}} -> Option CQuick Check:
Phrase suggestion uses "phrase" type [OK]
- Using term or completion instead of phrase
- Omitting the "field" inside phrase
- Misplacing the "text" parameter
{
"suggest": {
"text": "quik brown",
"phrase": {
"field": "body",
"size": 3
}
}
}Solution
Step 1: Identify the size parameter
The "size" parameter inside the phrase suggestion controls how many suggestions are returned.Step 2: Check the value of size
Here, "size" is set to 3, so Elasticsearch will return up to 3 suggestions.Final Answer:
3 suggestions -> Option DQuick Check:
Size = 3 means 3 suggestions [OK]
- Assuming default size without checking parameter
- Confusing size with gram_size
- Thinking size limits characters, not suggestions
{
"suggest": {
"my-suggest": {
"text": "helo world",
"phrase": {
"field": "content",
"gram_size": "two"
}
}
}
}Solution
Step 1: Check the "gram_size" parameter type
"gram_size" expects an integer value, but here it is given as a string "two".Step 2: Verify other parameters
"text" and "field" are correctly specified; phrase suggestions do support "gram_size".Final Answer:
"gram_size" should be an integer, not a string -> Option AQuick Check:
gram_size = integer, not string [OK]
- Passing gram_size as a string
- Removing the text parameter
- Changing field to an array unnecessarily
Solution
Step 1: Check the suggest block structure
The suggest block must have a named suggestion (e.g., "my-suggest") containing "text" and "phrase" parameters.Step 2: Verify phrase suggestion parameters
"gram_size" should be an integer (2), and "size" controls number of suggestions. { "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } } } correctly uses these.Step 3: Eliminate incorrect options
{ "suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } } misses the named suggestion key. { "suggest": { "my-suggest": { "text": "spel cheker", "term": { "field": "text", "gram_size": 2 } } } } uses "term" instead of "phrase". { "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": "2", "size": 2 } } } } passes "gram_size" as a string, which is invalid.Final Answer:
{ "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } } } -> Option BQuick Check:
Named suggest + phrase + integer gram_size = correct [OK]
- Omitting the named suggestion key
- Using term instead of phrase
- Passing gram_size as a string
Ready to prove it?
Pass the test and this pattern moves unknown
