websearch_to_tsquery with optional words

Viewed 263

When I use websearch_to_tsquery and search for something like where to_tsvector('english', description) @@ websearch_to_tsquery('virtual reality') I get plenty of results. when I add a word that doesn't exist anywhere in the description it gets back 0 such as where to_tsvector('english', description) @@ websearch_to_tsquery('virtual reality fsdfasjkwnejkfb')

How can I get the results of the closest matches vs all words matching?

1 Answers

So after trying for quite some time I discovered that in my case the to_tsquery function needed the config parameter in order to return a value other than 0 when there was clearly a closer match.

The function definition from Postgresql:

to_tsquery([ config regconfig, ] querytext text) returns tsquery

How I ended up using it:

select ts_rank_cd(to_tsvector('english', 'ring the police'), to_tsquery('english', 'police'))
select ts_rank_cd(to_tsvector('english', 'ring the police'), to_tsquery('english', 'police | xyzz'))

both functions above return 0.1 while the following returns 0

select ts_rank_cd(to_tsvector('english', 'ring the police'), to_tsquery('police'))

I am pretty sure that's what's missing in your case too

Related