Elasticsearch Fuzzy and Autocomplete
Search boxes face messy input. Visitors mistype "elasticsarch," forget which spelling is right, and stop typing halfway through a word. Fuzzy search tolerates small spelling errors. Autocomplete suggests results while the visitor types. Both features make a search box feel fast and friendly.
The Idea Behind Fuzziness
Fuzzy matching counts the number of single-character edits between two words. An edit inserts a letter, deletes a letter, replaces a letter, or swaps two neighboring letters. This count is the edit distance, a measure named after the mathematician Vladimir Levenshtein.
Typed word Stored word Edits needed
──────────────── ──────────────── ─────────────────────────
"colour" "color" 1 (delete "u")
"serch" "search" 1 (insert "a")
"serach" "search" 1 (swap "ra" and "ar")
"mesure" "measure" 1 (insert "a")
"quikc" "quick" 1 (swap "kc")
Fuzziness in a Match Query
Add the fuzziness setting to a match query. The value AUTO adjusts the allowed edits to the word length.
GET /products/_search
{
"query": {
"match": {
"name": {
"query": "runing shoes",
"fuzziness": "AUTO"
}
}
}
}
| Word Length | Edits Allowed with AUTO |
|---|---|
| 1 to 2 characters | 0 (exact match) |
| 3 to 5 characters | 1 |
| 6 or more characters | 2 |
Short words get strict treatment because one edit changes them too much. "cat" and "car" differ by one letter yet mean different things.
The Fuzzy Query and Its Controls
GET /products/_search
{
"query": {
"fuzzy": {
"name": {
"value": "labtop",
"fuzziness": 2,
"prefix_length": 1,
"max_expansions": 50,
"transpositions": true
}
}
}
}
| Option | Purpose |
|---|---|
| fuzziness | Maximum edits allowed |
| prefix_length | Number of starting letters that must match exactly |
| max_expansions | Cap on how many similar terms Elasticsearch tries |
| transpositions | Counts a swap of two neighbors as one edit |
A prefix_length of 1 or 2 speeds up searches noticeably, because people rarely misspell the first letter.
Autocomplete Options Compared
| Technique | How It Works | Best For |
|---|---|---|
| prefix query | Matches terms that start with the typed text | Simple, small data |
| match_phrase_prefix | Matches words in order, last word as prefix | Short phrases |
| edge_ngram analyzer | Stores every word beginning at index time | Fast, flexible suggestions |
| search_as_you_type | Ready-made field type with built-in n-grams | Quick setup |
| completion suggester | In-memory structure built for suggestions | Very fast title lists |
How Edge N-Grams Work
An edge n-gram filter stores each beginning slice of a word as its own token. A visitor who types "pho" matches the stored token "pho" directly. No scanning of the whole term list is needed.
Word stored: "phone"
Edge n-grams (min 1, max 5):
p
ph
pho
phon
phone
Typed "pho" → matches the stored token "pho" → finds "phone"
Using the search_as_you_type Field
This field type creates the n-gram sub-fields automatically. It is the fastest way to build a working suggestion box.
PUT /movies
{
"mappings": {
"properties": {
"title": { "type": "search_as_you_type" }
}
}
}
GET /movies/_search
{
"query": {
"multi_match": {
"query": "the dark kn",
"type": "bool_prefix",
"fields": ["title", "title._2gram", "title._3gram"]
}
}
}
Typed: "the dark kn"
Matches:
The Dark Knight
The Dark Knight Rises
A Custom Edge N-Gram Setup
Custom analyzers offer more control. The index analyzer creates n-grams, and the search analyzer stays plain so the query itself does not split into n-grams.
PUT /cities
{
"settings": {
"analysis": {
"filter": {
"edge_filter": { "type": "edge_ngram", "min_gram": 2, "max_gram": 15 }
},
"analyzer": {
"autocomplete_index": {
"tokenizer": "standard",
"filter": ["lowercase", "edge_filter"]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "autocomplete_index",
"search_analyzer": "standard"
}
}
}
}
The Completion Suggester
The completion field type keeps suggestions in a compact in-memory structure. Lookups finish in a fraction of a millisecond.
PUT /songs
{ "mappings": { "properties": { "suggest": { "type": "completion" } } } }
POST /songs/_doc
{ "suggest": ["Blue Skies", "Blue Moon"] }
GET /songs/_search
{
"suggest": {
"song_hint": {
"prefix": "blu",
"completion": { "field": "suggest" }
}
}
}
Practical Advice
Start suggestions after the visitor types two or three characters to avoid huge result lists. Limit suggestions to five or ten items. Combine fuzziness with prefix matching carefully, since broad matching on very short input returns noisy results.
Combining Both Features
A polished search box often uses both techniques in sequence. Autocomplete serves suggestions while the visitor types. Fuzzy matching rescues the full search after the visitor presses Enter with a misspelled word.
Typing phase: "lapt" → autocomplete shows "laptop", "laptop bag"
Submit phase: "labtop" → fuzzy match finds "laptop" documents
Measure both features with real traffic. Watch which suggestions users click and which searches return no results. Those two reports show where to adjust n-gram sizes, fuzziness limits, and synonym lists.
