الاستعلامات التقريبية واستعلامات أحرف البدل
طبّق المطابقة التقريبية للتعامل مع الأخطاء الإملائية، واستعلامات أحرف البدل للبحث القائم على الأنماط، مما يعزز تحمّل البحث للأخطاء.
الاستعلامات التقريبية واستعلامات أحرف البدل درس مجاني في Elasticsearch & Full Text Search Systems على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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!
الأسئلة الشائعة
هل درس «الاستعلامات التقريبية واستعلامات أحرف البدل» مجاني؟
نعم — نص درس «الاستعلامات التقريبية واستعلامات أحرف البدل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Elasticsearch & Full Text Search Systems، انتقل إلى CoddyKit PRO. تتضمن دورة Elasticsearch & Full Text Search Systems 4 دروس في المجموع.
ماذا ستتعلم في «الاستعلامات التقريبية واستعلامات أحرف البدل»؟
طبّق المطابقة التقريبية للتعامل مع الأخطاء الإملائية، واستعلامات أحرف البدل للبحث القائم على الأنماط، مما يعزز تحمّل البحث للأخطاء. تتمرن على Elasticsearch & Full Text Search Systems مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Elasticsearch & Full Text Search Systems؟
لا تُشترط خبرة سابقة. Elasticsearch & Full Text Search Systems على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الاستعلامات التقريبية واستعلامات أحرف البدل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Elasticsearch & Full Text Search Systems هذا؟
نعم. كل درس في Elasticsearch & Full Text Search Systems يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- عمليات البحث عن العبارات والقرب
- الاستعلامات التقريبية واستعلامات أحرف البدل
- تمييز نتائج البحث
- التجميعات للبحث ذي الأوجه