Instead, instead of breaking into an array, broken into many, then rearrange the summation of similarities
with users as ( select user_id, name from (values (1, 'John Smith')) t(user_id, name) ) select user_id, sum(similarity('smth', name_part)) from ( select user_id, regexp_split_to_table(name, E'\\s+') as name_part from users ) users where 'smth' % name_part group by user_id order by 2 desc ; user_id | sum
I'm not sure if you want to eliminate those parts of the name for which the similarity is below the threshold. If you donβt just omit the where clause
By the way, why do you need to split the name? Why not just compare the entire name with the search bar?
with users as ( select user_id, name from (values (1, 'John Smith')) t(user_id, name) ) select similarity('jon smth', name), * from users order by 1 desc ; similarity | user_id | name
source share