↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3
历史版本。 PostgreSQL 8.4 已结束支持。 请参阅 当前版本手册.

9.13. 文本搜索函数和操作符 #

表 9.36、表 9.37以及表 9.38总结了为全文检索提供的函数和操作符。PostgreSQL的文本检索功能的详细解释可参考第 12 章。

表 9.36. 文本搜索操作符

操作符描述示例结果
@@ tsvector 是否匹配 tsquery?to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t
@@@ @@ 的已弃用同义写法to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat')t
|| 串接 tsvector'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'
!! 求 tsquery 的反!! 'cat'::tsquery!'cat'
@> tsquery 是否包含另一个查询?'cat'::tsquery @> 'cat & rat'::tsqueryf
<@ tsquery 是否被包含于另一个查询?'cat'::tsquery <@ 'cat & rat'::tsqueryt

注意

tsquery 包含操作符只考虑两个查询中列出的词位,忽略组合操作符。

除了表中列出的操作符,tsvector 和 tsquery 类型还定义了普通的 B-树比较操作符(=、< 等)。这些操作符对文本检索用处不大,但可以用于其他用途,例如在这些类型的列上建立唯一索引。

表 9.37. 文本搜索函数

函数返回类型描述示例结果
to_tsvector([ config regconfig , ] document text)tsvector把文档文本归约为 tsvectorto_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
length(tsvector)integertsvector 中词位的数量length('fat:2,4 cat:3 rat:5A'::tsvector)3
setweight(tsvector, "char")tsvector为tsvector的每个元素指派权重setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
strip(tsvector)tsvector移除tsvector中的位置和权重strip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'
to_tsquery([ config regconfig , ] query text)tsquery规范化单词并转换为tsqueryto_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'
plainto_tsquery([ config regconfig , ] query text)tsquery忽略标点生成tsqueryplainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'
numnode(tsquery)integertsquery中词素加操作符的数量 numnode('(fat & rat) | cat'::tsquery)5
querytree(query tsquery)text获取tsquery的可索引部分querytree('foo & ! bar'::tsquery)'foo'
ts_rank([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ])float4为查询对文档排名ts_rank(textsearch, query)0.818
ts_rank_cd([ weights float4[], ] vector tsvector, query tsquery [, normalization integer ])float4使用覆盖密度为查询对文档排名ts_rank_cd('{0.1, 0.2, 0.4, 1.0}', textsearch, query)2.01317
ts_headline([ config regconfig, ] document text, query tsquery [, options text ])text显示查询匹配ts_headline('x y z', 'z'::tsquery)x y <b>z</b>
ts_rewrite(query tsquery, target tsquery, substitute tsquery)tsquery在查询中用替代子查询替换目标子查询ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsquery, select text)tsquery使用 SELECT 命令提供的目标和替代内容进行替换SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )
get_current_ts_config()regconfig获取默认文本搜索配置get_current_ts_config()english
tsvector_update_trigger()trigger自动更新 tsvector 列的触发器函数CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column()trigger自动更新 tsvector 列的触发器函数CREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body)

注意

所有接受一个可选的regconfig参数的文本检索函数在省略该参数时,会使用由default_text_search_config指定的配置。

表 9.38中的函数被单独列出,因为它们通常不被用于日常的文本检索操作。它们有助于开发和调试新的文本检索配置。

表 9.38. 文本搜索调试函数

函数返回类型描述示例结果
ts_debug([ config regconfig, ] document text, OUT alias text, OUT description text, OUT token text, OUT dictionaries regdictionary[], OUT dictionary regdictionary, OUT lexemes text[])setof record测试配置ts_debug('english', 'The Brightest supernovaes')(asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(dict regdictionary, token text)text[]测试词典ts_lexize('english_stem', 'stars'){star}
ts_parse(parser_name text, document text, OUT tokid integer, OUT token text)setof record测试解析器ts_parse('default', 'foo - bar')(1,foo) ...
ts_parse(parser_oid oid, document text, OUT tokid integer, OUT token text)setof record测试解析器ts_parse(3722, 'foo - bar')(1,foo) ...
ts_token_type(parser_name text, OUT tokid integer, OUT alias text, OUT description text)setof record获取解析器定义的词元类型ts_token_type('default')(1,asciiword,"Word, all ASCII") ...
ts_token_type(parser_oid oid, OUT tokid integer, OUT alias text, OUT description text)setof record获取解析器定义的词元类型ts_token_type(3722)(1,asciiword,"Word, all ASCII") ...
ts_stat(sqlquery text, [ weights text, ] OUT word text, OUT ndoc integer, OUT nentry integer)setof record获取 tsvector 列的统计信息ts_stat('SELECT vector from apod')(foo,10,15) ...

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.