Choose controlled strategies for prefixes, typos and advanced input.
course-products examples use the teaching mapping and product from chapter 5, “Mapping and field types”. Exercise walkthroughs below use the trainer’s separate public dataset.
- Prefixes and patterns
- Typos and expansions
- A safe user-facing syntax
Choose controlled strategies for prefixes, typos and advanced input.
The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.
Prefixes and patterns
prefix matches the start of a term. wildcard uses * and ?, and regexp matches indexed terms rather than arbitrarily scanning source strings. Leading wildcards may inspect many terms. Limiting input length and complexity is more effective than trying to compensate for expensive patterns with more servers.
Check your understandingTypos and expansions
Fuzzy matching expands a term by edit distance. fuzziness, prefix_length and max_expansions affect recall and cost. It is neither semantic search nor automatic transliteration. Fuzziness on short SKUs can return a different product. match_phrase_prefix and match_bool_prefix handle a final incomplete word, with different constraints on earlier words.
Check your understandingA safe user-facing syntax
query_string offers expressive syntax, but parse errors can reject a request. simple_query_string tolerates invalid syntax and lets you restrict operators. Both need explicit field, escaping and complexity policies. Do not forward arbitrary public DSL to the cluster: the server must control fields, access filters and resource limits.
Bound fuzzy expansions and evaluate real typo queries.
{
"query": {
"match": {
"name": {
"query": "headphnes",
"fuzziness": "AUTO",
"prefix_length": 1,
"max_expansions": 20
}
}
}
}How to read this result
Bound fuzzy expansions and evaluate real typo queries.
Catalog reasoning example
“headphnes” is a spelling variation of headphones; “for running” expresses an intent absent from that word. Fuzzy search can help the former, not necessarily the latter. Prefix handles an unfinished term beginning; an internal wildcard has different costs. Combining every strategy through should without measurement can increase candidates and latency while burying exact results.
Check your understandingHandle a typo
Fuzziness allows a bounded number of edits between analyzed query terms and indexed terms. AUTO chooses the edit distance from term length; it does not permit arbitrary similarity. The example travle can match travel via a transposition. This technique is useful for typos, not for replacing keyword identifiers or business restrictions.
A fuzzy query still operates on analyzed words. It is not a wildcard such as an arbitrary substring pattern. More permissive search can add unwanted candidates, so introduce fuzziness only when the requirement asks for typo tolerance. In this trainer expensive scripts, wildcard and regexp queries are deliberately unavailable.
Try a different scenario
{
"query": {
"match": {
"name": {
"query": "travle",
"fuzziness": "AUTO"
}
}
},
"size": 2,
"_source": [
"name",
"price"
]
}Elasticsearch response
{
"hits": {
"total": {
"value": 60,
"relation": "eq"
},
"hits": [
{
"_id": "3",
"_score": 1.3610321,
"_source": {
"name": "Sony Travel Speaker 3",
"price": 100
}
},
{
"_id": "13",
"_score": 1.3610321,
"_source": {
"name": "Apple Travel Speaker 13",
"price": 100
}
}
]
}
}How to read this result
Matching titles use the correctly spelled travel rather than the typo. The response demonstrates bounded token correction, not substring search.
Check your understanding
Answer before expanding each explanation.
1. What does regexp match?
Indexed terms in a field.
2. Why can *phone be expensive?
A leading wildcard expands dictionary traversal.
3. Does fuzziness correct meaning?
No, it operates on edit distance.
4. What fits a simple user syntax?
Often simple_query_string with restricted operators and fields.
5. Where should DSL be restricted?
At the server boundary before Elasticsearch.
Key takeaways
- Indexed terms in a field.
- A leading wildcard expands dictionary traversal.
- No, it operates on edit distance.
- Often simple_query_string with restricted operators and fields.
- At the server boundary before Elasticsearch.
Official reference
Ready to put this into practice?
Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.