0
0
Elasticsearchquery~5 mins

Phrase suggestions (did you mean) in Elasticsearch

Choose your learning style9 modes available
Introduction

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.

When users type search queries with spelling errors.
To improve search results by suggesting correct phrases.
When you want to help users find what they mean even if they mistype.
To make search more friendly and reduce frustration.
When you want to suggest alternative phrases based on your data.
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
This example suggests corrections for the phrase "quik brown fox" using the content field.
Elasticsearch
{
  "suggest": {
    "text": "quik brown fox",
    "simple_phrase": {
      "phrase": {
        "field": "content"
      }
    }
  }
}
This example suggests up to 2 phrase corrections for "helo wrld" using 3-word groups from the 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
      }
    }
  }
}
OutputSuccess
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.