question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create operator for postgres trigrams (fuzzy string matching)

See original GitHub issue

Problem

For string comparisons, there are filter operators like equals, startsWith, endsWith and contains. Although these are already very useful, it would be nice to take full advantage of the pg_trgm extension. To my knowledge there is currently no way besides a raw SQL query to fuzzy match a string.

Update: I just realized that there are some issues going on about Full-Text-Search in prisma. So please just consider this as an API suggestion since ppl seem to be aware of the problem already (#7960)

Suggested solution

A suggested solution would be to create a new operator that can be used like the known string operators:

const searchString = "Meier"
const result = await prisma.user.findMany({
  where: {
    lastName: {
      similar: searchString,
      threshold: 0.45
    }
  }
});

// Would return last names "Meier", "Meyer", "Maier", ...

Alternatives

As an alternative, one could use a raw SQL query:

const searchString = "Meier";
const result = await prisma.$queryRaw(`SELECT * FROM User WHERE SIMILARITY(lastName, '${searchString}') > 0.45;`)

Material fuzzy string matching

https://www.postgresql.org/docs/13/pgtrgm.html https://www.freecodecamp.org/news/fuzzy-string-matching-with-postgresql/

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:50
  • Comments:25 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
javiermanzanocommented, Mar 29, 2022

+1 this will be really helpful. Happy to help with this 😃

16reactions
chuckstockcommented, Feb 25, 2022

+1 for visibility and request for this feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fuzzy string matching with Trigram and Trigraphs
A text string is composed of n number of trigrams which in PostgreSQL is displayed as an array of 3 character text values...
Read more >
How to use fuzzy string matching with Postgresql
You can enable it from psql using the command below: CREATE EXTENSION pg_trgm; This extension brings with it some helpful functions for fuzzy...
Read more >
Documentation: 15: F.35. pg_trgm - PostgreSQL
The pg_trgm module provides functions and operators for determining the similarity of alphanumeric text based on trigram matching, as well as index operator...
Read more >
Ordering fuzzy search results by relevancy using trigrams
In linguistics, a trigram is a group of three consecutive written units such as letters, syllables, or words. What Postgres will do is...
Read more >
A Handbook to Implement Fuzzy Search in PostgreSQL
How to use PostgreSQL fuzzy search methods like trigrams, Levenshtein matching, phonetic matching, and pattern ops for LIKE and Regex queries.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found