pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
表 9.37、 表 9.38以及 表 9.39 总结了为全文检索提供的函数和操作符。PostgreSQL的文本检索功能的详细解释可参考第 12 章。
表 9.37. 文本搜索操作符
| Operator | 描述 | 示例 | Result |
|---|---|---|---|
@@ |
tsvector 是否匹配 tsquery? |
to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat') |
t |
@@@ |
@@ 的已弃用同义写法 |
to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat') |
t |
|| |
concatenate tsvectors |
'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector |
'a':1 'b':2,5 'c':3 'd':4 |
&& |
将 tsquery 进行 AND 组合 |
'fat | rat'::tsquery && 'cat'::tsquery |
( 'fat' | 'rat' ) & 'cat' |
|| |
把多个tsquery进行 OR 组合 |
'fat | rat'::tsquery || 'cat'::tsquery |
( 'fat' | 'rat' ) | 'cat' |
!! |
negate a tsquery |
!! 'cat'::tsquery |
!'cat' |
@> |
tsquery 是否包含另一个查询? |
'cat'::tsquery @> 'cat & rat'::tsquery |
f |
<@ |
tsquery 是否被包含于另一个查询? |
'cat'::tsquery <@ 'cat & rat'::tsquery |
t |
tsquery 包含操作符只考虑两个查询中列出的词位,忽略组合操作符。
除了表中列出的操作符,tsvector 和 tsquery 类型还定义了普通的 B-树比较操作符(=、< 等)。这些操作符对文本检索用处不大,但可以用于其他用途,例如在这些类型的列上建立唯一索引。
表 9.38. 文本搜索函数
| Function | Return Type | 描述 | 示例 | Result |
|---|---|---|---|---|
|
tsvector |
reduce document text to tsvector |
to_tsvector('english', 'The Fat Rats') |
'fat':2 'rat':3 |
|
integer |
number of lexemes in tsvector |
length('fat:2,4 cat:3 rat:5A'::tsvector) |
3 |
|
tsvector |
为tsvector的每个元素指派权重 |
setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A') |
'cat':3A 'fat':2A,4A 'rat':5A |
|
tsvector |
移除tsvector中的位置和权重 |
strip('fat:2,4 cat:3 rat:5A'::tsvector) |
'cat' 'fat' 'rat' |
|
tsquery |
规范化单词并转换为tsquery |
to_tsquery('english', 'The & Fat & Rats') |
'fat' & 'rat' |
|
tsquery |
忽略标点生成tsquery |
plainto_tsquery('english', 'The Fat Rats') |
'fat' & 'rat' |
|
integer |
tsquery中词素加操作符的数量 |
numnode('(fat & rat) | cat'::tsquery) |
5 |
|
text |
获取tsquery的可索引部分 |
querytree('foo & ! bar'::tsquery) |
'foo' |
|
float4 |
rank document for query | ts_rank(textsearch, query) |
0.818 |
|
float4 |
使用覆盖密度为查询对文档排名 | ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query) |
2.01317 |
|
text |
display a query match | ts_headline('x y z', 'z'::tsquery) |
x y <b>z</b> |
|
tsquery |
在查询中用替代子查询替换目标子查询 | ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery) |
'b' & ( 'foo' | 'bar' ) |
|
tsquery |
使用 SELECT 命令提供的目标和替代内容进行替换 |
SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases') |
'b' & ( 'foo' | 'bar' ) |
|
regconfig |
获取默认文本搜索配置 | get_current_ts_config() |
english |
|
trigger |
trigger function for automatic tsvector column update |
CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body) |
|
|
trigger |
trigger function for automatic tsvector column update |
CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body) |
所有接受一个可选的regconfig参数的文本检索函数在省略该参数时,会使用由default_text_search_config指定的配置。
表 9.39中的函数被单独列出,因为它们通常不被用于日常的文本检索操作。 它们有助于开发和调试新的文本检索配置。
表 9.39. 文本搜索调试函数
| Function | Return Type | 描述 | 示例 | Result |
|---|---|---|---|---|
|
setof record |
test a configuration | ts_debug('english', 'The Brightest supernovaes') |
(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ... |
|
text[] |
test a dictionary | ts_lexize('english_stem', 'stars') |
{star} |
|
setof record |
测试解析器 | ts_parse('default', 'foo - bar') |
(1,foo) ... |
|
setof record |
测试解析器 | ts_parse(3722, 'foo - bar') |
(1,foo) ... |
|
setof record |
获取解析器定义的词元类型 | ts_token_type('default') |
(1,asciiword,"Word, all ASCII") ... |
|
setof record |
获取解析器定义的词元类型 | ts_token_type(3722) |
(1,asciiword,"Word, all ASCII") ... |
|
setof record |
get statistics of a tsvector column |
ts_stat('SELECT vector from apod') |
(foo,10,15) ... |
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。