pgtrgm

Crates.iopgtrgm
lib.rspgtrgm
version0.4.0
created_at2026-01-09 20:57:59.513232+00
updated_at2026-01-10 13:55:41.388992+00
descriptionPostgreSQL pg_trgm extension support for Diesel
homepagehttps://github.com/martsokha/pgtrgm
repositoryhttps://github.com/martsokha/pgtrgm
max_upload_size
id2032833
size36,167
Oleh Martsokha (martsokha)

documentation

https://docs.rs/pgtrgm

README

pgtrgm

Build Crates.io Documentation

PostgreSQL pg_trgm extension support for Diesel.

A fork of triforce_rs, this crate provides bindings for PostgreSQL's pg_trgm extension, enabling trigram-based text similarity matching and fuzzy search.

Features

  • Full query builder integration with operators and functions
  • Trigram similarity operators (%, <%, %>, <<%, %>>)
  • Distance operators (<->, <<->, <->>, <<<->, <->>>)

Installation

Add this to your Cargo.toml:

[dependencies]
pgtrgm = { version = "0.4", features = ["diesel"] }

PostgreSQL Setup

Ensure the pg_trgm extension is enabled in your database:

CREATE EXTENSION IF NOT EXISTS pg_trgm;

For optimal performance, create a GIN index on columns you'll search:

CREATE INDEX users_name_trgm_idx ON users USING gin (name gin_trgm_ops);

Usage

use diesel::prelude::*;
use pgtrgm::prelude::*;

// Find similar names
let results = users::table
    .filter(users::name.trgm_similar_to("john"))
    .order_by(users::name.trgm_distance("john"))
    .load::<User>(&mut conn)?;

// Get similarity score
let results = users::table
    .select((users::name, similarity(users::name, "john")))
    .filter(users::name.trgm_similar_to("john"))
    .load::<(String, f32)>(&mut conn)?;

// Word similarity for matching within longer text
let results = articles::table
    .filter(articles::content.trgm_word_similar_to("database"))
    .load::<Article>(&mut conn)?;

Related Crates

Contributing

Contributions are welcome! Please read our Contributing Guide for details on how to submit pull requests, report issues, and contribute to the project.

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Commit count: 23

cargo fmt