模糊与通配符查询
实现模糊匹配以应对拼写错误,并使用通配符查询执行基于模式的搜索,增强搜索容错能力。
模糊与通配符查询 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Search Beyond Exact Matches
Imagine searching for 'apple' but typing 'aple'. Traditional exact searches fail! This lesson introduces techniques to make your search more forgiving and powerful.
We'll explore how to handle typos and search for patterns, ensuring users find what they need even with slight inaccuracies.
What is Fuzzy Search?
Fuzzy search helps you find documents that are 'similar' to your search term, even if there are minor spelling mistakes or variations.
It's incredibly useful for:
- Correcting user typos
- Matching variations in spelling
- Improving user experience by being more tolerant
How Fuzziness Works: Levenshtein
Elasticsearch's fuzzy matching is often based on the Levenshtein distance algorithm. This algorithm measures the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into the other.
For example, the Levenshtein distance between 'apple' and 'aple' is 1 (one deletion). Between 'apple' and 'apply' is 2 (one substitution, one insertion).
Your First Fuzzy Query
You can add fuzziness to a match query. The fuzziness parameter controls the maximum Levenshtein distance allowed.
Try running this example. It will find 'apple' even if you search for 'aple':
GET /products/_search
{
"query": {
"match": {
"name": {
"query": "aple",
"fuzziness": "AUTO"
}
}
}
}Fuzzy Parameters: AUTO & Length
The fuzziness parameter can be set to AUTO (recommended) or a number (0, 1, or 2).
- AUTO: Elasticsearch calculates the allowed edit distance based on the term's length. Shorter terms allow fewer edits.
- prefix_length: You can specify a number of initial characters that must exactly match. This can improve performance and relevance.
What are Wildcard Queries?
Wildcard queries allow you to search for patterns within text using special characters. They're useful when you know only part of a term or want to match a family of terms.
Unlike fuzzy queries that correct typos, wildcards help you broaden your search based on specific patterns.
Wildcard Operators
Wildcard queries use two main operators:
*(asterisk): Matches zero or more characters. For example,appl*would match 'apple', 'application', 'appliance'.?(question mark): Matches any single character. For example,appl?would match 'apply' but not 'apple'.
Wildcard in Action
Let's see a wildcard query in practice. This query will find documents where the product_code field starts with 'ABC' and has any characters following it.
Remember, wildcard queries are typically case-sensitive on keyword fields and can be slow on text fields.
GET /products/_search
{
"query": {
"wildcard": {
"product_code": {
"value": "ABC*"
}
}
}
}Wildcard Query Cautions
While powerful, wildcard queries, especially those with leading wildcards (e.g., *term), can be computationally expensive and slow.
- They don't use the inverted index efficiently.
- They have to scan many terms to find matches.
- Consider using
match_phrase_prefixorcompletion suggestersfor 'autocomplete' type functionality instead.
Fuzzy vs. Wildcard: When to Use
Choosing between fuzzy and wildcard depends on your goal:
- Fuzzy Queries: Best for handling minor typos and spelling variations. Ideal when you expect a close but not exact match.
- Wildcard Queries: Best for pattern matching and when you know parts of a term but not the whole thing. Be mindful of performance, especially with leading wildcards.
Test Your Knowledge
Which of the following statements are TRUE regarding fuzzy and wildcard queries in Elasticsearch?
Recap: Flexible Search
You've mastered two powerful techniques for more flexible search:
- Fuzzy Queries: Leverage the Levenshtein distance to find matches despite typos, using
fuzziness(e.g.,AUTO). - Wildcard Queries: Search for patterns using
*(zero or more chars) and?(single char). Use with caution due to potential performance impacts.
These methods make your search more fault-tolerant and user-friendly!
常见问题解答
「模糊与通配符查询」课时是免费的吗?
是的 — 「模糊与通配符查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「模糊与通配符查询」这节课中我会学到什么?
实现模糊匹配以应对拼写错误,并使用通配符查询执行基于模式的搜索,增强搜索容错能力。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「模糊与通配符查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。