Simple spelling suggestions

Edit on GitHub

Spelling suggestions provide the users with alternative search terms when the search query does not return any results:

Spell checking

Translation: Unfortunately there were 0 results for your exact search term “hammer holk”. Did you possibly mean hammer holz?

This is one of the simplest features you can build with Elasticsearch and also one that your users expect to see. Elasticsearch has a highly configurable term suggester, which enables suggestions based on the edit distance between indexed terms and search terms. We recommend putting all attributes that are suitable for spelling suggestions—for example, short strings such as product and category names where you are sure that they are spelled correctly—into a single document field:

"suggestion_terms": [
  "Fortis Fäustel, mit Eschen-Stiel"
]

Suggestion terms are then indexed by splitting them by whitespace and lowercasing. The same goes for search terms that will be compared with the indexed terms.

"suggestion_terms" : {
  "type" : "string",
  "analyzer" : "term_suggestion_analyzer",
}

At query time, a suggest part is added to every query. It tries to return the closest tokens (based on edit distance) for all terms that were not matched in the query. For tokens that match at least one document, no suggestions are going to be calculated. In case you have doubts about the quality of suggestion_terms, you can fetch several suggestions per term from Elasticsearch and then use some heuristics in the backend to select one that exhibits a good combination of distance score and term frequency.

"suggest": {
  "spelling-suggestion": {
    "text": "hammmer",
    "term": {
      "field": "suggestion_terms",
      "size": 1
    }
  }
}