Phrase suggestions help users find the right words when they make typos or spelling mistakes. They show a "Did you mean" message to guide users to the correct search terms.
Phrase suggestions (did you mean) in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"suggest": {
"text": "user input phrase",
"phrase_suggestion_name": {
"phrase": {
"field": "field_name",
"size": 1,
"gram_size": 2
}
}
}
}Replace user input phrase with the text you want to check.
field_name is the field in your index to check for phrase suggestions.
Examples
content field.Elasticsearch
{
"suggest": {
"text": "quik brown fox",
"simple_phrase": {
"phrase": {
"field": "content"
}
}
}
}title field.Elasticsearch
{
"suggest": {
"text": "helo wrld",
"phrase_suggestion": {
"phrase": {
"field": "title",
"size": 2,
"gram_size": 3
}
}
}
}Sample Program
This request asks Elasticsearch to suggest corrections for the phrase "appl pie recip" in the recipe_text field of the my_index index.
Elasticsearch
POST /my_index/_search
{
"suggest": {
"text": "appl pie recip",
"phrase_suggestion": {
"phrase": {
"field": "recipe_text",
"size": 1,
"gram_size": 2
}
}
}
}Important Notes
Phrase suggestions work best with well-indexed text fields.
You can adjust size to get more or fewer suggestions.
gram_size controls how many words are checked together for better phrase matching.
Summary
Phrase suggestions help fix typos in search queries.
Use the suggest part of the search request with phrase type.
Adjust field, size, and gram_size to control suggestions.
Practice
1. What is the main purpose of phrase suggestions (did you mean) in Elasticsearch?
easy
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]
Hint: Phrase suggestions fix typos in queries [OK]
Common Mistakes:
- Confusing phrase suggestions with sorting or filtering
- Thinking phrase suggestions aggregate data
- Assuming phrase suggestions change document content
2. Which of the following is the correct way to specify a phrase suggestion in an Elasticsearch query?
easy
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]
Hint: Phrase suggestions use "phrase" type in suggest [OK]
Common Mistakes:
- Using term or completion instead of phrase
- Omitting the "field" inside phrase
- Misplacing the "text" parameter
3. Given this Elasticsearch phrase suggestion snippet, what will be the size of suggestions returned?
{
"suggest": {
"text": "quik brown",
"phrase": {
"field": "body",
"size": 3
}
}
}medium
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]
Hint: Check "size" to know number of suggestions [OK]
Common Mistakes:
- Assuming default size without checking parameter
- Confusing size with gram_size
- Thinking size limits characters, not suggestions
4. Identify the error in this phrase suggestion query:
{
"suggest": {
"my-suggest": {
"text": "helo world",
"phrase": {
"field": "content",
"gram_size": "two"
}
}
}
}medium
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]
Hint: Use numbers, not words, for gram_size [OK]
Common Mistakes:
- Passing gram_size as a string
- Removing the text parameter
- Changing field to an array unnecessarily
5. You want to suggest corrections for a user query with multiple words and control the phrase suggestion to consider 2-gram phrases. Which of the following is the correct way to configure the phrase suggestion in Elasticsearch?
hard
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]
Hint: Use named suggest with phrase and integer gram_size [OK]
Common Mistakes:
- Omitting the named suggestion key
- Using term instead of phrase
- Passing gram_size as a string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
