HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to make middle and suffix matching using full text search?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
suffixfullsearchtextmakeusinghowandmiddlematching

Problem

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?

Solution

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

Code Snippets

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

Context

StackExchange Database Administrators Q#189084, answer score: 7

Revisions (0)

No revisions yet.