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

  • Mastering PostgreSQL 12
  • Hans Jürgen Sch?nig
  • 242字
  • 2021-08-20 10:00:32

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 doesn't actually show up in the original text. Still, PostgreSQL will return true. The reason is that want and wanted are both transformed into the same lexeme, so the result is true. Practically, this makes a lot of sense. Imagine that 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 following 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 easy to build complex search strings.

主站蜘蛛池模板: 阿拉善左旗| 舞钢市| 定襄县| 亳州市| 宝应县| 北京市| 普定县| 旌德县| 丰台区| 周口市| 盱眙县| 安岳县| 抚松县| 弥渡县| 虹口区| 墨玉县| 潍坊市| 泾川县| 南城县| 西林县| 文安县| 安宁市| 武山县| 延寿县| 蛟河市| 岱山县| 尤溪县| 康马县| 枣庄市| 嵊泗县| 安吉县| 大足县| 桐庐县| 正阳县| 西平县| 泰兴市| 运城市| 铁岭县| 舞阳县| 合水县| 彝良县|