Postgresql – How to make middle and suffix matching using full text search

full-text-searchpostgresql

I'm trying to find a string on a database using FTS on Postgres:

select name
from users
where to_tsvector('simple', name) @@ to_tsquery('simple', 'ad:*')

But the :* search only the words start with ad.

I found this question Get partial match from Get partial match from GIN indexed TSVECTOR column column.

But it doesn't mention how to solve the problem of middle and suffix matching using FTS?

Best Answer

The "Full" in Full Text Search means match full words, not parts of words

You should use RegExp or LIKE or tri-gram matching instead

You can mimic suffix matching by reversing words in your index and also reversing your queries, but this takes more space:

select
reverse('brown fox')::tsvector @@ (reverse('rown') || ':*')::tsquery --true