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

  • Mastering PostgreSQL 9.6
  • Hans Jurgen Schonig
  • 223字
  • 2021-07-09 19:57:23

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 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 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.

主站蜘蛛池模板: 博兴县| 普格县| 海南省| 沁阳市| 福清市| 磴口县| 个旧市| 疏附县| 台南县| 玛纳斯县| 遂川县| 太仓市| 峨边| 石首市| 平原县| 宁都县| 桦南县| 左贡县| 平和县| 泽普县| 永丰县| 定西市| 中江县| 右玉县| 南靖县| 通许县| 丹巴县| 沙雅县| 安义县| 会昌县| 铅山县| 滦南县| 奉节县| 元阳县| 安远县| 海原县| 尼木县| 清新县| 天水市| 藁城市| 三门县|