11

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?

András Váczi
  • 31,278
  • 13
  • 101
  • 147
Slim
  • 241
  • 3
  • 9

2 Answers2

7

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
Neil McGuigan
  • 8,423
  • 4
  • 39
  • 56
2

I don't think that that is possible with FTS. It seems like a better job for pg_trgm, which can accelerate like and ilike (and their symbolic forms, ~~ and ~~*).

jjanes
  • 39,726
  • 3
  • 37
  • 48