To know how exactly the text is matched, you need the fuzzystrmatch
PostgreSQL module. It provides functions as difference
and levenshtein
that will give you an estimation of the similarity between two strings.
Then, you can build your query with some conditions like:
SELECT *
FROM image
WHERE name LIKE '%text%'
AND ( group_id = 10
OR second_field = 4
OR thirth_field = 5
)
ORDER BY ( (group_id=10)::int
+ (second_field=4)::int
+ (thirth_field=5)::int
) * weight_1
+ (strlen(name) - levenshtein('text',name)) * weight_2
You can adjust weight_1
and weight_2
to give preference to the text distance or the number of conditions met.