数据仓库服务 GAUSSDB(DWS)-文本搜索类型:tsquery

时间:2024-01-26 16:15:20

tsquery

tsquery类型表示一个检索条件,存储用于检索的词汇,并且使用布尔操作符&(AND),|(OR)和!(NOT)将这些词汇进行组合,圆括号用来强调操作符的分组。如果没有圆括号,(NOT)的优先级最高,其次是&(AND),最后是|(OR)。to_tsquery函数及plainto_tsquery函数会将单词转换为tsquery类型前进行规范化处理。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
SELECT 'fat & rat'::tsquery;
    tsquery    
---------------
 'fat' & 'rat'
(1 row)

SELECT 'fat & (rat | cat)'::tsquery;
          tsquery          
---------------------------
 'fat' & ( 'rat' | 'cat' )
(1 row)

SELECT 'fat & rat & ! cat'::tsquery;
        tsquery         
------------------------
 'fat' & 'rat' & !'cat'
(1 row)

tsquery中的词汇可以用一个或多个权重字母来标记,这些权重字母限制词汇只能与匹配权重的tsvector词汇进行匹配。

1
2
3
4
5
SELECT 'fat:ab & cat'::tsquery;
     tsquery      
------------------
 'fat':AB & 'cat'
(1 row)

同样,tsquery中的词汇可以用*标记来指定前缀匹配。例如:这个查询可以匹配tsvector中以“super”开始的任意单词。

1
2
3
4
5
SELECT 'super:*'::tsquery;
  tsquery  
-----------
 'super':*
(1 row)

需注意,前缀匹配会首先被文本搜索分词器处理。例如:postgres中提取的词干是postgr,匹配到了postgraduate,也就意味着下面的结果为真:

1
2
3
4
5
SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' ) AS RESULT;
  result  
----------
 t
(1 row)
1
2
3
4
5
SELECT to_tsquery('postgres:*');
 to_tsquery 
------------
 'postgr':*
(1 row)

to_tsquery函数会将单词转换为tsquery类型前进行规范化处理。'Fat:ab & Cats'规范化转为tsquery类型结果如下:

1
2
3
4
5
SELECT to_tsquery('Fat:ab & Cats');
    to_tsquery    
------------------
 'fat':AB & 'cat'
(1 row)
support.huaweicloud.com/sqlreference-820-dws/dws_06_0018.html