pg-smart-search
Core Concepts

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

  1. Linguistic Search (FTS): The fastest strategy. Uses dictionaries and indices to find root words (e.g., "running" → "run").
  2. ILIKE: If FTS returns nothing, the engine falls back to simple substring matching. Useful for partial matches or names.
  3. Trigram Fuzzy: If ILIKE fails, the engine uses PostgreSQLs pg_trgm to find words that are structurally similar. This handles typos (e.g., "laptpp" → "laptop").
  4. Keyboard Layout Correction (ISO 9): If Trigram fails, the engine checks if the query makes sense if the user typed in a different layout (e.g., Russian layout on an English keyboard: "ghbdjy" → "привет").

Implementation

This happens transparently inside the engine.search() method. You dont need to write complex OR clauses or conditional logic.

const results = await engine.search({
  query: "laptpp", // Typo handled automatically
  language: "ru", // Enables ISO 9 layout correction for Russian
});

The engine will try the fastest methods first, and only engage heavier computations if the faster ones yield zero results.