什么是高级?这就叫高级—openGauss(341)

18 阅读1分钟

#openGauss #入门 #安装 #数据库 #开源

知识来源:docs-opengauss.osinfra.cn/zh/

下面是使用中文分词法排序查询的例子:

openGauss=# CREATE TABLE tsearch.ts_ngram(id int, body text);
openGauss=# INSERT INTO tsearch.ts_ngram VALUES(1, '中文');
openGauss=# INSERT INTO tsearch.ts_ngram VALUES(2, '中文检索');
openGauss=# INSERT INTO tsearch.ts_ngram VALUES(3, '检索中文');
--精确匹配
openGauss=# SELECT id, body, ts_rank_cd(to_tsvector('ngram',body), query) AS rank FROM tsearch.ts_ngram, to_tsquery('中文') query WHERE query @@ to_tsvector(body);
 id | body | rank 
----+------+------
  1 | 中文 |   .1
(1 row)

--模糊匹配
openGauss=# SELECT id, body, ts_rank_cd(to_tsvector('ngram',body), query) AS rank FROM tsearch.ts_ngram, to_tsquery('中文') query WHERE query @@ to_tsvector('ngram',body);
 id |   body   | rank 
----+----------+------
  3 | 检索中文 |   .1
  1 | 中文     |   .1
  2 | 中文检索 |   .1
(3 rows)

排序要遍历每个匹配的tsvector,因此资源消耗多,可能会因为I/O限制导致排序慢。可是这是很难避免的,因为实际查询中通常会有大量的匹配。

#openGauss #入门 #安装 #数据库 #开源

知识来源:docs-opengauss.osinfra.cn/zh/