8.11.2. tsquery
8.11.2. tsquery
A tsquery value stores lexemes that are to be searched for, and can combine them using the Boolean operators & (AND), | (OR), and ! (NOT), as well as the phrase search operator <-> (FOLLOWED BY). There is also a variant <N> of the FOLLOWED BY operator, where N is an integer constant that specifies the distance between the two lexemes being searched for. <-> is equivalent to <1>.
tsquery值存储要搜索的词素,并可以使用布尔运算符&(AND),|(OR),和! (NOT),以及词组搜索运算符<->(FOLLOWED BY)将其组合。 FOLLOWED BY运算符还有一个变体<N>,其中N是一个整数常量,它指定要搜索的两个词素之间的距离。 <->等效于<1>。
Parentheses can be used to enforce grouping of these operators. In the absence of parentheses, ! (NOT) binds most tightly, <-> (FOLLOWED BY) next most tightly, then & (AND), with | (OR) binding the least tightly.
括号可用于强制对这些运算符进行分组。 在没有括号的情况下,! (NOT)优先级最高,<->(FOLLOWED BY)紧随其后,然后&(AND),最后是 | (OR)。
Here are some examples:
示例:
SELECT 'fat & rat'::tsquery;
tsquery
---------------
'fat' & 'rat'
SELECT 'fat & (rat | cat)'::tsquery;
tsquery
---------------------------
'fat' & ( 'rat' | 'cat' )
SELECT 'fat & rat & ! cat'::tsquery;
tsquery
------------------------
'fat' & 'rat' & !'cat'
Optionally, lexemes in a tsquery can be labeled with one or more weight letters, which restricts them to match only tsvector lexemes with one of those weights:
可选地,可以使用一个或多个权重字母来标记tsquery中的词素,这限制了它们仅匹配具有这些权重之一的tsvector词素:
SELECT 'fat:ab & cat'::tsquery;
tsquery
------------------
'fat':AB & 'cat'
Also, lexemes in a tsquery can be labeled with * to specify prefix matching:
此外,tsquery中的词素可以用*标记以指定前缀匹配:
SELECT 'super:*'::tsquery;
tsquery
-----------
'super':*
This query will match any word in a tsvector that begins with “super”.
上例中的查询将匹配tsvector中任何以super开头的词。
Quoting rules for lexemes are the same as described previously for lexemes in tsvector; and, as with tsvector, any required normalization of words must be done before converting to the tsquery type. The to_tsquery function is convenient for performing such normalization:
词素的相关规则与之前在tsvector中对词素的描述相同; 并且,与tsvector一样,在转换为tsquery类型之前,必须先完成所有必要的单词标准化操作。 to_tsquery函数可方便地执行此类标准化:
SELECT to_tsquery('Fat:ab & Cats');
to_tsquery
------------------
'fat':AB & 'cat'
Note that to_tsquery will process prefixes in the same way as other words, which means this comparison returns true:
注意, to_tsquery将以与其他词相同的方式处理前缀,这意味着此比较返回true:
SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
?column?
----------
t
because postgres gets stemmed to postgr:
因为postgres有postgr前缀:
SELECT to_tsvector( 'postgraduate' ), to_tsquery( 'postgres:*' );
to_tsvector | to_tsquery
---------------+------------
'postgradu':1 | 'postgr':*
which will match the stemmed form of postgraduate.
这与postgraduate的前缀相同。