0

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?

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

4 Comments

Why are there two spaces between username and first_name, but only one between first_name and last_name?
@Shien Just a typo
More importantly, what happens when the user types in both the first name and username (separated by a space)?
how would you index this table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.