Typo Tolerance
Typo tolerance allows Orama to find relevant results even when users make spelling mistakes. Using Levenshtein distance, Orama can match terms that are similar but not identical to the indexed content.
How Typo Tolerance Works
Orama uses the Levenshtein distance algorithm to measure the similarity between the search term and indexed terms. The Levenshtein distance represents the minimum number of single-character edits (insertions, deletions, or substitutions) needed to transform one string into another.
Basic Typo Tolerance
Use the tolerance parameter to enable typo tolerance:
import { create , insert , search } from '@orama/orama'
const db = create ({
schema: {
title: 'string' ,
description: 'string'
}
})
insert ( db , {
title: 'Wireless Headphones' ,
description: 'Premium wireless headphones with noise cancellation'
})
const results = search ( db , {
term: 'hedphones' , // Typo: missing 'a'
tolerance: 1 // Allow 1 character difference
})
// Will match "headphones" despite the typo
Tolerance Levels
The tolerance parameter accepts numeric values indicating the maximum Levenshtein distance:
No Tolerance (Exact Match)
Tolerance of 1
Tolerance of 2
Tolerance of 3
const results = search ( db , {
term: 'hedphones' ,
tolerance: 0 // Default - no typo tolerance
})
// Will NOT match "headphones"
Maximum Levenshtein distance between search term and indexed terms. Higher values allow more typos but may reduce precision.
Common Typo Examples
Missing Characters
// Indexed: "headphones"
const results = search ( db , {
term: 'hedphones' , // Missing 'a'
tolerance: 1
})
// ✓ Matches
// Indexed: "headphones"
const results = search ( db , {
term: 'headpphones' , // Extra 'p'
tolerance: 1
})
// ✓ Matches
Substituted Characters
// Indexed: "headphones"
const results = search ( db , {
term: 'headphonez' , // 's' replaced with 'z'
tolerance: 1
})
// ✓ Matches
Transposed Characters
// Indexed: "headphones"
const results = search ( db , {
term: 'haedphones' , // 'ea' transposed to 'ae'
tolerance: 2 // Requires 2 edits
})
// ✓ Matches
Multiple Term Queries
Tolerance applies to each term in multi-word queries:
const results = search ( db , {
term: 'wireles hedphones' , // Two typos
tolerance: 1
})
// Matches "wireless headphones"
// - "wireles" matches "wireless" (1 edit)
// - "hedphones" matches "headphones" (1 edit)
Each term in the query is evaluated independently with the specified tolerance level.
Tolerance with Property Filtering
Combine typo tolerance with property-specific search:
const results = search ( db , {
term: 'wireles' ,
properties: [ 'title' , 'description' ],
tolerance: 1
})
Tolerance with Filters
Use typo tolerance alongside filters:
const results = search ( db , {
term: 'hedphones' ,
tolerance: 1 ,
where: {
price: {
lt: 100
},
inStock: true
}
})
Tolerance with Boosting
Combine with field boosting for prioritized fuzzy matching:
const results = search ( db , {
term: 'wireles hedphones' ,
tolerance: 1 ,
boost: {
title: 2 ,
description: 1
}
})
Choosing the Right Tolerance
Start with 1
Tolerance of 1 handles most common typos (missing, extra, or substituted character) without sacrificing precision: search ( db , { term: 'query' , tolerance: 1 })
Use 2 for Longer Terms
For searches with longer terms (8+ characters), tolerance of 2 can be appropriate: search ( db , { term: 'accommodation' , tolerance: 2 })
Avoid High Tolerance
Tolerance of 3+ may return too many irrelevant results: // May match too broadly
search ( db , { term: 'cat' , tolerance: 3 })
Consider Query Length
Longer queries can handle higher tolerance better than short queries
Tolerance vs Exact Search
The exact and tolerance parameters serve different purposes:
Exact Search (Case-Sensitive)
Fuzzy Search (Typo Tolerant)
const results = search ( db , {
term: 'Headphones' ,
exact: true ,
tolerance: 0 // Exact must have tolerance 0
})
// Only matches exact phrase "Headphones" (case-sensitive)
The exact and tolerance parameters are mutually exclusive. When exact: true, tolerance must be 0.
Tolerance in Hybrid Search
Typo tolerance only affects the full-text search portion of hybrid search:
const results = search ( db , {
mode: 'hybrid' ,
term: 'hedphones' , // Typo
tolerance: 1 , // Applies to full-text search
vector: {
value: embeddings ,
property: 'embedding'
},
hybridWeights: {
text: 0.6 ,
vector: 0.4
}
})
Vector search is not affected by the tolerance parameter. It uses cosine similarity for semantic matching.
Higher Tolerance = Slower Search
Each level of tolerance increases computation time: // Fast
search ( db , { term: 'query' , tolerance: 0 })
// Slower
search ( db , { term: 'query' , tolerance: 2 })
Long Terms are More Expensive
Fuzzy matching on longer terms requires more computation: // More expensive
search ( db , { term: 'accommodation' , tolerance: 2 })
// Less expensive
search ( db , { term: 'hotel' , tolerance: 2 })
Limit Search Properties
Reduce properties searched to improve performance: search ( db , {
term: 'query' ,
tolerance: 1 ,
properties: [ 'title' ] // Faster than all properties
})
Use Threshold
Combine with threshold to reduce result set: search ( db , {
term: 'query' ,
tolerance: 1 ,
threshold: 1 // Require at least one match
})
Practical Examples
E-commerce Search
// Handle product name typos
const results = search ( db , {
term: 'smasung galxy' , // Multiple typos
tolerance: 1 ,
properties: [ 'name' , 'brand' ],
boost: {
name: 2
}
})
// Matches "Samsung Galaxy"
Documentation Search
// Handle technical term typos
const results = search ( db , {
term: 'authentcation' , // Missing 'i'
tolerance: 1 ,
properties: [ 'title' , 'content' ],
boost: {
title: 3
}
})
// Matches "authentication"
User Search
// Handle name typos
const results = search ( db , {
term: 'Micheal' , // Common misspelling
tolerance: 1 ,
properties: [ 'firstName' , 'lastName' ]
})
// Matches "Michael"
Best Practices
Use Moderate Tolerance Tolerance of 1-2 balances flexibility and precision
Adjust by Use Case E-commerce might use tolerance 1, spell-checkers might use 2-3
Test with Real Data Validate tolerance settings with actual user queries
Monitor Performance Higher tolerance increases search time - measure impact
Full-Text Search Learn about full-text search fundamentals
Boosting Combine typo tolerance with field boosting
Filters Apply filters to fuzzy search results
Hybrid Search Use typo tolerance in hybrid search