trgm

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

pg/trgm

The pg_trgm extension for sqlb. Needs CREATE EXTENSION pg_trgm.

SQL Helper
a % b Similar
a <% b, a <<% b WordSimilar, StrictWordSimilar
a <-> b, a <<-> b Distance, WordDistance, for ORDER BY with a GiST index
similarity, word_similarity, strict_word_similarity Similarity, WordSimilarity, StrictWordSimilarity
show_trgm ShowTrgm

The % family is served by gin_trgm_ops; the <-> family needs gist_trgm_ops. ILIKE '%x%' on a trigram-indexed column is pg.ILike, not a helper here, because the operator is core Postgres; only the index is the extension's.

needle := sqlb.Param("jon smith")
sel.Exprs(name, sqlb.As(trgm.Similarity(name, needle), "sim")).
	From(users).Where(trgm.Similar(name, needle)).
	Order(sqlb.OrderBy(trgm.Distance(name, needle), sqlb.SortAsc)).Limit(10)

Documentation

Overview

Package trgm is the pg_trgm extension: trigram similarity operators and functions. Needs CREATE EXTENSION pg_trgm. The operators are served by a GIN index with gin_trgm_ops, and the distance operators by GiST with gist_trgm_ops.

Index

Constants

This section is empty.

Variables

View Source
var (
	GinOps  = pg.NewOpClass[string]("gin_trgm_ops")
	GistOps = pg.NewOpClass[string]("gist_trgm_ops")
)

GinOps and GistOps are the trigram operator classes. GIN is the usual choice for similarity and LIKE search; GiST also serves ordering by distance. A nullable column takes pg.NullableOps(GinOps).

View Source
var Extension = pg.Extension{Name: "pg_trgm"}

Extension is the pg_trgm extension this package's constructs need, for pg.CreateExtension(Extension), so a library installs what it uses without spelling its name.

Functions

func Distance

func Distance(a, b sqlb.Expr[string]) sqlb.Expr[float32]

Distance renders a <-> b, the KNN distance for ORDER BY with a GiST index.

func ShowTrgm

func ShowTrgm(e sqlb.Expr[string]) sqlb.Expr[[]string]

ShowTrgm renders show_trgm(e), the trigrams themselves. It is a debugging call: it shows why two strings do or do not match.

func Similar

func Similar(a, b sqlb.Expr[string]) sqlb.Predicate

Similar renders a % b: similarity above pg_trgm.similarity_threshold.

func Similarity

func Similarity(a, b sqlb.Expr[string]) sqlb.Expr[float32]

Similarity renders similarity(a, b), the trigram overlap from 0 to 1. It is the value behind the % operator, for a SELECT item or a threshold of the caller's own.

func StrictWordSimilar

func StrictWordSimilar(a, b sqlb.Expr[string]) sqlb.Predicate

StrictWordSimilar renders a <<% b, the word-boundary form of WordSimilar: the match must cover whole words, not a prefix of one.

func StrictWordSimilarity

func StrictWordSimilarity(a, b sqlb.Expr[string]) sqlb.Expr[float32]

StrictWordSimilarity renders strict_word_similarity(a, b), the value behind <<%.

func WordDistance

func WordDistance(a, b sqlb.Expr[string]) sqlb.Expr[float32]

WordDistance renders a <<-> b, the KNN distance matching WordSimilar.

func WordSimilar

func WordSimilar(a, b sqlb.Expr[string]) sqlb.Predicate

WordSimilar renders a <% b: a matches a word inside b, above pg_trgm.word_similarity_threshold.

func WordSimilarity

func WordSimilarity(a, b sqlb.Expr[string]) sqlb.Expr[float32]

WordSimilarity renders word_similarity(a, b), the value behind <%.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL