Create operator for postgres trigrams (fuzzy string matching)
See original GitHub issueProblem
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:
- Created 2 years ago
- Reactions:50
- Comments:25 (2 by maintainers)
Top GitHub Comments
+1 this will be really helpful. Happy to help with this 😃
+1 for visibility and request for this feature.