web dev//SQL//fuzzy matching

Fuzzy matching is text search that tolerates typos, partial matches and misspellings, and returns results ranked by similarity instead of requiring exact matches. The query `atention` matches *Attention* and `transform` matches *transformers*: the query does not need to be perfect.


Fuzzy matching is text search that tolerates typos, partial matches and misspellings, and returns results ranked by similarity instead of requiring exact matches. The query atention matches Attention and transform matches transformers: the query does not need to be perfect.

It works by comparing character n-grams (trigrams) or the edit distance (Levenshtein) between the query and the stored text.

It returns a similarity score between 0.0 and 1.0, so results can be ranked by relevance instead of just filtered yes or no.

The % operator in PostgreSQL (through pg_trgm) returns true if the similarity exceeds a threshold; combined with ORDER BY similarity() it gives ranked results.

Substring matching (LIKE '%term%') is exact and unranked: either the string contains the term or it does not. Fuzzy matching handles human error.

Full-text search (tsvector/tsquery) tokenizes and stems words and understands language structure. Fuzzy matching is character-level, dumber, but it catches typos that full-text search misses, so the two complement each other.