Carta Operum

Journal ◆ Server

Fuzzy search and facets in PostgreSQL

Forgiving a typo in 'dungarees' without adding a search engine to the stack.

The catalog has to forgive. Parents type on a phone, in a hurry, with a child in the other hand, and 'dungarees' arrives with a letter missing. A search engine would solve it and would also add a service to deploy, feed and keep in sync. A children's clothing catalog fits comfortably in PostgreSQL, so I start there.

Trigram matching does the forgiving. It compares the three-letter pieces of two words, so near misses still score well. Results rank by that similarity, then by whether the match is in the name or only in the description. Facets are the other half: price, size and colour are counted over the same filtered set, so every filter chip can say how many products it would leave.

The rule I keep is that facets are computed from the query and not from the page. If a shopper has chosen a size, the colours on offer are the colours that exist in that size. It costs a few more queries per request, and for a catalog of this size that is a fair price for leaving the search engine out.