After taking a brief look at the stemming process, it is time to figure out how a stemmed text can be compared to a user query. The following code snippet checks for the wordwanted:
test=# SELECT to_tsvector('english', 'A car, I want a car. I would not even mind having many cars') @@ to_tsquery('english', 'wanted'); ?column? ---------- t (1 row)
Note thatwanteddoes not actually show up in the original text. Still, PostgreSQL will returntrue. The reason is thatwantandwantedare both transformed to the same lexeme, so the result is true. Practically, this makes a lot of sense. Imagine you are looking for a car on Google. If you find pages selling cars, this is totally fine. Finding common lexemes is, therefore, an intelligent idea.
Sometimes, people are not only looking for a single word, but want to find a set of words. Withto_tsquery, this is possible, as shown in the next example:
test=# SELECT to_tsvector('english', 'A car, I want a car. I would not even mind having many cars') @@ to_tsquery('english', 'wanted & bmw'); ?column?
---------- f
(1 row)
In this case,falseis returned becausebmwcannot be found in our input string. In theto_tsqueryfunction,&meansandand|meansor. It is therefore easily possible to build complex search strings.