解析查询

openGauss提供了函数to_tsquery和plainto_tsquery将查询转换为tsquery数据类型,to_tsquery提供比plainto_tsquery更多的功能,但对其输入要求更严格。

to_tsquery([ config regconfig, ] querytext text) returns tsquery

to_tsquery从querytext中创建一个tsquery,querytext必须由布尔运算符& (AND),| (OR)和! (NOT)分割的单个token组成。这些运算符可以用圆括弧分组。换句话说,to_tsquery输入必须遵循tsquery输入的通用规则,具体请参见文本搜索类型。不同的是基本tsquery以token表面值作为输入,而to_tsquery使用指定或默认分词器将每个token标准化成词素,并依据分词器丢弃属于停用词的token。例如:

openGauss=# SELECT to_tsquery('english', 'The & Fat & Rats');
   to_tsquery   
---------------
 'fat' & 'rat'
(1 row)

像在基本tsquery中的输入一样,weight(s)可以附加到每个词素来限制它只匹配那些有相同weight(s)的tsvector词素。比如:

openGauss=# SELECT to_tsquery('english', 'Fat | Rats:AB');
    to_tsquery    
------------------
 'fat' | 'rat':AB
(1 row)

同时,*也可以附加到词素来指定前缀匹配:

openGauss=# SELECT to_tsquery('supern:*A & star:A*B');
        to_tsquery        
--------------------------
 'supern':*A & 'star':*AB
(1 row)

这样的词素将匹配tsquery中指定字符串和权重的项。

plainto_tsquery([ config regconfig, ] querytext text) returns tsquery

plainto_tsquery将未格式化的文本querytext变换为tsquery。类似于to_tsvector,文本被解析并且标准化,然后在存在的词之间插入&(AND)布尔算子。

比如:

openGauss=# SELECT plainto_tsquery('english', 'The Fat Rats');
 plainto_tsquery 
-----------------
 'fat' & 'rat'
(1 row)

请注意,plainto_tsquery无法识别布尔运算符、权重标签,或在其输入中的前缀匹配标签:

openGauss=# SELECT plainto_tsquery('english', 'The Fat & Rats:C');
   plainto_tsquery   
---------------------
 'fat' & 'rat' & 'c'
(1 row)

在这里,所有输入的标点符号作为空格符号丢弃。

意见反馈
编组 3备份
openGauss 2024-03-19 00:47:31
取消