官术网_书友最值得收藏!

Comparing strings

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 word wanted:

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 that wanted does not actually show up in the original text. Still, PostgreSQL will return true. The reason is that want and wanted are 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. With to_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, false is returned because bmw cannot be found in our input string. In the to_tsquery function, & means and and | means or. It is therefore easily possible to build complex search strings.

主站蜘蛛池模板: 博罗县| 清新县| 张北县| 南康市| 双鸭山市| 茶陵县| 镇安县| 饶河县| 阳西县| 监利县| 宜宾市| 丹巴县| 古浪县| 濉溪县| 丽水市| 永和县| 天津市| 阿克苏市| 红河县| 新郑市| 色达县| 崇文区| 盐源县| 昌图县| 襄汾县| 彭州市| 永春县| 武定县| 清徐县| 洪湖市| 佛坪县| 安溪县| 岗巴县| 梨树县| 东平县| 鹰潭市| 德钦县| 安平县| 双牌县| 额敏县| 霍州市|