Trigrams, Similarity, and Searches

Learn about the similarity and search functions offered by the pg_trgm extension.

The idea behind trigrams is simple and very effective. Split your text into a consecutive series of three letters. That’s it. Then you can compare two texts based on how many consecutive three-letter series (trigrams) are common. That’s the notion of similarity. It works surprisingly well and doesn’t depend on the language used.

Similarity function in trigrams

In the following query, we show trigrams extracted from several attempts at spelling the name “Tommy” and then the similarity value obtained when comparing tomy and dim to tom.

Get hands-on with 1200+ tech skills courses.