选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0
历史版本PostgreSQL 9.0 已于 2015 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

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

表 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 包含操作符只考虑两个查询中列出的词位,忽略组合操作符。

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

表 9.38. 文本搜索函数

Function Return Type 描述 示例 Result
to_tsvector([ config regconfig , ] document text) tsvector reduce document text to tsvector to_tsvector('english', 'The Fat Rats') 'fat':2 'rat':3
length(tsvector) integer number of lexemes in tsvector 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 规范化单词并转换为tsquery to_tsquery('english', 'The & Fat & Rats') 'fat' & 'rat'
plainto_tsquery([ config regconfig , ] query text) tsquery 忽略标点生成tsquery plainto_tsquery('english', 'The Fat Rats') 'fat' & 'rat'
numnode(tsquery) integer tsquery中词素加操作符的数量 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 rank document for query 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 display a query match 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 trigger function for automatic tsvector column update CREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column() 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
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 test a configuration ts_debug('english', 'The Brightest supernovaes') (asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...
ts_lexize(dict regdictionary, token text) text[] test a dictionary 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 get statistics of a tsvector column ts_stat('SELECT vector from apod') (foo,10,15) ...

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。