0
0
Elasticsearchquery~5 mins

Fuzzy matching in Elasticsearch

Choose your learning style9 modes available
Introduction

Fuzzy matching helps find words that are close to what you typed, even if there are small mistakes or typos.

When a user types a search word with a typo but you still want to find the right results.
When you want to match similar words that sound alike or look alike.
When searching names or addresses that might have spelling variations.
When you want to improve search experience by forgiving small errors.
When matching data from different sources that may have slight differences.
Syntax
Elasticsearch
{
  "query": {
    "fuzzy": {
      "field_name": {
        "value": "search_term",
        "fuzziness": "AUTO",
        "prefix_length": 1
      }
    }
  }
}

fuzziness controls how many changes (like letter swaps or missing letters) are allowed.

prefix_length means how many letters at the start must match exactly.

Examples
This searches the name field for words like "jon", "john", or "jonh".
Elasticsearch
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "jon",
        "fuzziness": "AUTO"
      }
    }
  }
}
This looks for cities similar to "bostn" allowing up to 2 changes but the first 2 letters must match exactly.
Elasticsearch
{
  "query": {
    "fuzzy": {
      "city": {
        "value": "bostn",
        "fuzziness": 2,
        "prefix_length": 2
      }
    }
  }
}
Sample Program

This query searches the product_name field for terms similar to "iphon", like "iphone" or "iphonr". It helps find products even if the user misspells the name.

Elasticsearch
{
  "query": {
    "fuzzy": {
      "product_name": {
        "value": "iphon",
        "fuzziness": "AUTO",
        "prefix_length": 1
      }
    }
  }
}
OutputSuccess
Important Notes

Fuzzy matching can slow down searches if used on large data without limits.

Use prefix_length to improve performance by requiring some exact matches at the start.

Fuzziness set to AUTO lets Elasticsearch decide the best number of allowed changes based on word length.

Summary

Fuzzy matching helps find words that are close to the search term, fixing small typos.

It uses fuzziness to control how many letter changes are allowed.

Setting prefix_length improves speed by requiring some exact letters at the start.