Smart Hybrid Fallback
Learn how pg-smart-search gracefully degrades from FTS to ILIKE, Trigram Fuzzy, and Keyboard Layout Correction.
Smart Hybrid Fallback
Users make mistakes. They misspell words, type in the wrong keyboard layout, or use partial words. A search engine that only returns exact matches will fail these users.
pg-smart-search implements a smart fallback chain to maximize result relevance.
The Fallback Chain
- FTS + Standard (hedged, not sequential): The engine races Full-Text Search against a combined ILIKE-substring-and-trigram-prefilter query, returning whichever finds results first and cancelling the other. These two don't run one-after-another — see Architecture for the hedging details.
- Trigram Fuzzy: If neither of the above found anything, the engine re-queries using
word_similarity()against a dynamic threshold. This handles typos (e.g., "laptpp" → "laptop"). - Keyboard Layout Correction: Only if
language: 'ru'was passed and still zero results — the engine remaps the query as if it were typed on the wrong keyboard layout (e.g., "ghbdjy" typed on a US layout → "привет" on a RU one) and retries the fuzzy search. - ISO 9 Transliteration: Still
language: 'ru'and still zero results — the engine transliterates the (Latin) query into Cyrillic using the ISO 9:1995 mapping and retries the fuzzy search once more.
Steps 3 and 4 only ever trigger when you explicitly pass language: 'ru' — they are not
attempted for other languages or when language is omitted.
Implementation
This happens transparently inside the engine.search() method. You don't need to write complex OR clauses or conditional logic.
const results = await engine.search({
query: "laptpp", // Typo handled automatically (step 2, any language)
language: "ru", // Also enables steps 3 and 4 for this call
});The engine tries the fastest methods first, and only engages heavier computations if the faster ones yield zero results.