I have a tiny dataset (~1000 rows). Each row has a username, first name and last name. Can I do a fuzzy search on these three fields by using pg_trgm and concatenating the three fields together with two spaces between each? Alternatively, is there a better method to search through this set of users, using trigrams or any other method?
Add a comment
|
1 Answer
select format('%s %s %s', username, first_name, last_name)
from t
order by greatest (
similarity(_name, username),
similarity(_name, first_name),
similarity(_name, last_name)
) desc
limit 10
or
select s
from t, format('%s %s %s', username, first_name, last_name) s(s)
order by word_similarity(_name, s) desc
limit 10
4 Comments
Shien
Why are there two spaces between
username and first_name, but only one between first_name and last_name?Clodoaldo Neto
@Shien Just a typo
Shien
More importantly, what happens when the user types in both the first name and username (separated by a space)?
jackbravo
how would you index this table?