📖 Scenario: You are building a simple search feature for a bookstore website. Users often make typos when typing book titles. To help users find books even with small spelling mistakes, you want to use fuzzy matching in Elasticsearch.
🎯 Goal: Create an Elasticsearch query that uses fuzzy matching to find books by title, allowing for small typos.
📋 What You'll Learn
Create an index called books with a title field of type text.
Add a document with the title "The Great Gatsby".
Create a variable called search_title with the value "Gret Gatsb" (a misspelled title).
Write a fuzzy match query using search_title to find the book with a small typo.
Complete the query with a fuzziness parameter set to 1.
💡 Why This Matters
🌍 Real World
Fuzzy matching helps users find information even if they make spelling mistakes or typos in search queries, improving user experience on search engines and websites.
💼 Career
Understanding fuzzy matching is useful for roles involving search engine development, data retrieval, and improving user interfaces for search functionality.
Progress0 / 4 steps
1
Create the books index with a title field
Create an Elasticsearch index called books with a mapping that defines a title field of type text.
Elasticsearch
Hint
Use the mappings key with properties to define the title field as text.
2
Add a document with the title "The Great Gatsby"
Add a document to the books index with the field title set to "The Great Gatsby".
Elasticsearch
Hint
Use the document JSON with the title field exactly as shown.
3
Create a variable search_title with the misspelled title
Create a variable called search_title and set it to the string "Gret Gatsb".
Elasticsearch
Hint
Assign the string "Gret Gatsb" to the variable search_title.
4
Write a fuzzy match query using search_title with fuzziness 1
Write an Elasticsearch query that uses a match query on the title field with the value from search_title. Add the parameter fuzziness set to 1 to allow small typos.
Elasticsearch
Hint
Use a match query with fuzziness set to 1 to allow small typos.
Practice
(1/5)
1. What does the fuzziness parameter control in an Elasticsearch fuzzy query?
easy
A. The maximum length of the search term
B. The number of allowed letter changes to match similar words
C. The minimum number of exact matches required
D. The number of search results returned
Solution
Step 1: Understand the role of fuzziness
The fuzziness parameter sets how many letters can be changed, added, or removed to still count as a match.
Step 2: Compare options with definition
Only The number of allowed letter changes to match similar words correctly describes this as controlling allowed letter changes for similar words.
Final Answer:
The number of allowed letter changes to match similar words -> Option B
Quick Check:
Fuzziness = allowed letter changes [OK]
Hint: Fuzziness = allowed letter changes in matching [OK]
Common Mistakes:
Confusing fuzziness with result count
Thinking fuzziness limits search term length
Mixing fuzziness with prefix_length
2. Which of the following is the correct syntax to add fuzziness in an Elasticsearch query DSL?
Fuzziness 0 allows no letter differences (exact match). Prefix_length 1 means first letter must match exactly.
Step 2: Check each option against rules
"roam" matches exactly. "foam" differs in first letter (prefix_length=1), so no. "roams" has extra letter, no exact match. "roan" differs by one letter after prefix ('m' vs 'n'), but fuzziness=0 allows no changes, so no.
Final Answer:
"roam" -> Option A
Quick Check:
Exact match with prefix_length = "roam" [OK]
Hint: Prefix_length fixes first letters; fuzziness=0 requires exact match [OK]
Common Mistakes:
Ignoring prefix_length effect on first letter
Assuming all similar words match regardless of prefix
But it returns no results even though documents with "plastics" and "elustics" exist. What is the likely problem?
medium
A. prefix_length is too high, blocking matches with differences in first 3 letters
B. fuzziness value must be a boolean, not a number
C. query string "elastic" is misspelled
D. match query does not support fuzziness parameter
Solution
Step 1: Analyze prefix_length effect
Prefix_length 3 means first 3 letters must match exactly. If documents differ in those letters, they won't match.
Step 2: Check fuzziness and query validity
Fuzziness accepts numbers; "2" is valid. Query "elastic" is correct. Match query supports fuzziness.
Final Answer:
prefix_length is too high, blocking matches with differences in first 3 letters -> Option A
Quick Check:
High prefix_length blocks fuzzy matches = prefix_length is too high, blocking matches with differences in first 3 letters [OK]
Hint: High prefix_length blocks fuzzy matches early [OK]
Common Mistakes:
Thinking fuzziness must be boolean
Ignoring prefix_length effect
Assuming match query disallows fuzziness
5. You want to find documents with names similar to "johnson" allowing up to 2 letter changes but want to speed up search by requiring the first 2 letters to match exactly. Which query is best?