agnsaft
agnsaft

Reputation: 1881

PostgreSQL prevent non-matching tsqueries from matching tsvector

Given the following query:

select to_tsvector('fat cat ate rat') @@ plainto_tsquery('cats ate');

This query will return true as a result. Now, what if I don't want "cats" to also match the word "cat", is there any way I can prevent this?

Also, is there any way I can make sure that the tsquery matches the entire string in that particular order (e.g. the "cats ate" is counted as a single token rather than two). At the moment the following query will also match:

select to_tsvector('fat cat ate rat') @@ plainto_tsquery('ate cats');

Upvotes: 0

Views: 635

Answers (1)

Daniel Vérité
Daniel Vérité

Reputation: 61526

cat matching cats is due to english stemming, english being probably your default text search configuration. See the result of show default_text_search_config to be sure.

It can be avoided by using the simple configuration. Try the function calls with explicit text configurations:

select to_tsvector('simple', 'fat cat ate rat') @@ plainto_tsquery('simple', 'cats ate');

Or change it with:

set default_text_search_config='simple';

Upvotes: 2

Related Questions