Reverse Analyzer, Suffix or “Ends With” Search
By default, Elasticsearch matches from the start of words.
But what if you need to match from the end, for example, file extensions like .pdf or .zip?
Use a reverse token filter to flip the text, and then apply Edge N-Gram.
Example Mapping
PUT reverse_index
{
"settings": {
"analysis": {
"filter": {
"edge_reverse": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10
}
},
"analyzer": {
"reverse_autocomplete": {
"tokenizer": "standard",
"filter": ["lowercase", "reverse", "edge_reverse", "reverse"]
}
}
}
},
"mappings": {
"properties": {
"filename": {
"type": "text",
"analyzer": "reverse_autocomplete",
"search_analyzer": "standard"
}
}
}
}
Now, searching for “pdf” or “zip” will match “report.pdf” or “backup.zip”. Use for suffix-based search like extensions, domain endings, or last words.
Fuzzy Search, Handling Typos
Users often make spelling mistakes.
Fuzzy search helps Elasticsearch find results even with small typos or missing letters.
Concept
Fuzzy search uses Levenshtein edit distance, how many edits are needed to turn one word into another.
Examples:
- “iphon” → 1 edit away from “iphone”
- “googel” → 2 edits away from “google”
Example Query
GET products/_search
{
"query": {
"match": {
"name": {
"query": "iphon",
"fuzziness": "AUTO"
}
}
}
}
Matches “iPhone”, even though user typed “iphon”. You can also set fuzziness manually:
"fuzziness": 2
Performance Tip
Use fuzzy search only for short fields (titles, names),
because fuzzy queries are slower on long text fields.
Search-as-you-type — Built-In Autocomplete Field
From Elasticsearch 7.x onward, there’s an easier way:
the search_as_you_type field type — no need to define analyzers manually.
This field automatically creates small subfields (_2gram, _3gram, _index_prefix)
so it behaves like edge n-gram but optimized.
Example Mapping
PUT product_titles
{
"mappings": {
"properties": {
"title": { "type": "search_as_you_type" }
}
}
}
Example Query
GET product_titles/_search
{
"query": {
"multi_match": {
"query": "iph",
"type": "bool_prefix",
"fields": [
"title",
"title._2gram",
"title._3gram",
"title._index_prefix"
]
}
}
}
Matches “iPhone 15 Pro Max” as you type “iph”. Works with fuzziness: 1 for typo tolerance.
Combining Them for Best Experience
| Feature | Goal | Ideal Analyzer / Query |
|---|---|---|
| Autocomplete (prefix) | Find as user types | Edge N-Gram / search_as_you_type |
| Suffix search | Match ending (e.g. .pdf) | Reverse Edge N-Gram |
| Typo tolerance | Handle spelling errors | Fuzzy search (fuzziness: AUTO) |
| Mid-word match | Search within words | N-Gram |
| Phrase match | Keep order | match_phrase or match_phrase_prefix |
Example: All-In-One Setup
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"autocomplete": { "type": "text", "analyzer": "autocomplete" },
"reverse": { "type": "text", "analyzer": "reverse_autocomplete" },
"keyword": { "type": "keyword" }
}
}
}
}
Then use queries like:
{
"bool": {
"should": [
{ "match": { "title.autocomplete": "iphon" } },
{ "match": { "title.reverse": "pdf" } },
{ "match": { "title": { "query": "iphon", "fuzziness": 1 } } }
]
}
}
Covers prefix, suffix, and typo in one unified search pipeline.
