Bird
Raised Fist0
Elasticsearchquery~20 mins

Phrase suggestions (did you mean) in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Phrase Suggestion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Phrase Suggestion Output for Misspelled Query
Given the following Elasticsearch phrase suggestion query, what is the expected output suggestion for the misspelled word "appl"?
Elasticsearch
{
  "suggest": {
    "text": "appl",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [{
          "field": "title.trigram",
          "suggest_mode": "always"
        }],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}
A{"simple_phrase":[{"text":"appl","highlighted":"<em>appl</em>"}]}
B{"simple_phrase":[{"text":"apply","highlighted":"<em>apply</em>"}]}
C{"simple_phrase":[{"text":"apple","highlighted":"<em>apple</em>"}]}
D{"simple_phrase":[]}
Attempts:
2 left
💡 Hint
Think about how phrase suggestions correct common misspellings using trigram analysis.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Purpose of Direct Generator in Phrase Suggestions
What is the main role of the "direct_generator" in an Elasticsearch phrase suggestion query?
AIt highlights the matched terms in the search results.
BIt generates candidate corrections based on the specified field and suggest mode.
CIt controls the number of suggestions returned by the query.
DIt filters out suggestions that do not match the original query exactly.
Attempts:
2 left
💡 Hint
Consider how Elasticsearch finds possible corrections for misspelled words.
❓ Predict Output
advanced
2:00remaining
Output of Phrase Suggestion with Multiple Candidates
What is the output of the following phrase suggestion query when the input text is "recieve" and the index contains "receive" and "recipe"?
Elasticsearch
{
  "suggest": {
    "text": "recieve",
    "phrase_suggester": {
      "phrase": {
        "field": "content.trigram",
        "size": 2,
        "gram_size": 3,
        "direct_generator": [{
          "field": "content.trigram",
          "suggest_mode": "always"
        }],
        "highlight": {
          "pre_tag": "<b>",
          "post_tag": "</b>"
        }
      }
    }
  }
}
A{"phrase_suggester":[{"text":"receive","highlighted":"<b>receive</b>"},{"text":"recipe","highlighted":"<b>recipe</b>"}]}
B{"phrase_suggester":[{"text":"recieve","highlighted":"<b>recieve</b>"}]}
C{"phrase_suggester":[{"text":"receipt","highlighted":"<b>receipt</b>"}]}
D{"phrase_suggester":[]}
Attempts:
2 left
💡 Hint
The suggester returns the top 2 candidates based on trigram similarity.
🔧 Debug
advanced
1:30remaining
Identify the Error in Phrase Suggestion Query
What error will this Elasticsearch phrase suggestion query produce?
Elasticsearch
{
  "suggest": {
    "text": "exampel",
    "phrase": {
      "field": "body.trigram",
      "gram_size": 3,
      "direct_generator": {
        "field": "body.trigram",
        "suggest_mode": "missing"
      }
    }
  }
}
ARuntimeError: suggest_mode 'missing' is invalid
BNo error, query runs successfully
CTypeError: field parameter missing in direct_generator
DSyntaxError: direct_generator must be an array, not an object
Attempts:
2 left
💡 Hint
Check the data type of direct_generator in the phrase suggestion syntax.
🧠 Conceptual
expert
2:00remaining
Why Use Phrase Suggestions Instead of Term Suggestions?
Why would you choose to use phrase suggestions (did you mean) over term suggestions in Elasticsearch?
APhrase suggestions consider the context of multiple words to correct phrases, reducing nonsensical corrections.
BPhrase suggestions are faster to compute than term suggestions.
CTerm suggestions do not support highlighting of corrections.
DPhrase suggestions only work on numeric fields, while term suggestions work on text.
Attempts:
2 left
💡 Hint
Think about how phrase suggestions handle multiple words compared to single words.

Practice

(1/5)
1. What is the main purpose of phrase suggestions (did you mean) in Elasticsearch?
easy
A. To help fix typos and suggest correct phrases in search queries
B. To sort search results by relevance
C. To filter documents based on date ranges
D. To aggregate numeric data for analytics

Solution

  1. Step 1: Understand phrase suggestions purpose

    Phrase suggestions are designed to detect and correct typos or mistakes in user search queries.
  2. Step 2: Compare with other features

    Sorting, filtering, and aggregations serve different purposes unrelated to typo correction.
  3. Final Answer:

    To help fix typos and suggest correct phrases in search queries -> Option A
  4. Quick 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
A. "suggest": {"my-suggestion": {"completion": {"field": "content"}}}
B. "suggest": {"my-suggestion": {"term": {"field": "content"}}}
C. "suggest": {"my-suggestion": {"text": "exampl", "phrase": {"field": "content"}}}
D. "suggest": {"my-suggestion": {"match": {"field": "content"}}}

Solution

  1. Step 1: Identify phrase suggestion syntax

    Phrase suggestions use the "phrase" type inside the suggest block with a specified field.
  2. Step 2: Check other options

    Options A, B, and D use other suggestion types like completion, term, or match, which are not phrase suggestions.
  3. Final Answer:

    "suggest": {"my-suggestion": {"text": "exampl", "phrase": {"field": "content"}}} -> Option C
  4. Quick 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
A. 1 suggestion
B. Unlimited suggestions
C. 5 suggestions
D. 3 suggestions

Solution

  1. Step 1: Identify the size parameter

    The "size" parameter inside the phrase suggestion controls how many suggestions are returned.
  2. Step 2: Check the value of size

    Here, "size" is set to 3, so Elasticsearch will return up to 3 suggestions.
  3. Final Answer:

    3 suggestions -> Option D
  4. Quick 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
A. "gram_size" should be an integer, not a string
B. "text" parameter is missing
C. "field" should be an array, not a string
D. Phrase suggestions don't support 'gram_size' parameter

Solution

  1. Step 1: Check the "gram_size" parameter type

    "gram_size" expects an integer value, but here it is given as a string "two".
  2. Step 2: Verify other parameters

    "text" and "field" are correctly specified; phrase suggestions do support "gram_size".
  3. Final Answer:

    "gram_size" should be an integer, not a string -> Option A
  4. Quick 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
A. { "suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } }
B. { "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } } }
C. { "suggest": { "my-suggest": { "text": "spel cheker", "term": { "field": "text", "gram_size": 2 } } } }
D. { "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": "2", "size": 2 } } } }

Solution

  1. Step 1: Check the suggest block structure

    The suggest block must have a named suggestion (e.g., "my-suggest") containing "text" and "phrase" parameters.
  2. 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.
  3. 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.
  4. Final Answer:

    { "suggest": { "my-suggest": { "text": "spel cheker", "phrase": { "field": "text", "gram_size": 2, "size": 2 } } } } -> Option B
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard