Bier.Fuzzy (bier v0.1.0)

Copy Markdown View Source

Approximate name matching for PostgREST's "Perhaps you meant …" hints.

PostgREST keeps a FuzzySet per exposed schema (SchemaCache.dbTablesFuzzyIndex) and asks it for the single best match above a minimum score (getFuzzyHint, Error.hs#L400, minScore = 0.75 for table and procedure names). The fuzzyset package scores a candidate by edit distance normalized against the longer of the two names, which is what score/2 computes: an 8-character name one edit away scores 1 - 1/8 = 0.875 and clears the threshold, while four edits away scores 0.5 and does not.

Matching is case-insensitive, mirroring the normalization FuzzySet applies before indexing.

Summary

Functions

The best-scoring candidate for term, or nil when none reaches min_score.

Similarity of two already-normalized names in 0.0..1.0: one minus the edit distance over the length of the longer name.

Functions

best_match(term, candidates, min_score)

@spec best_match(String.t(), [String.t()], float()) :: String.t() | nil

The best-scoring candidate for term, or nil when none reaches min_score.

Ties are broken by candidate name so the hint is stable across schema-cache reloads (a FuzzySet is likewise deterministic, but its insertion order is not something Bier's introspection guarantees).

A term longer than 64 characters is not scored at all and yields nil — see the note above the constant.

score(a, b)

@spec score(String.t(), String.t()) :: float()

Similarity of two already-normalized names in 0.0..1.0: one minus the edit distance over the length of the longer name.