9.3 9.4 9.5 9.6 10 11 12 13
阿里云PostgreSQL 问题报告 纠错本页面

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

表 9-37表 9-38表 9-39总结了为全文搜索提供的函数和操作符。PostgreSQL的文本搜索功能的详细解释可参考第 12 章

表 9-37. 文本搜索操作符

操作符描述例子结果
@@ 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的包含操作符只考虑两个查询中的词位,而忽略组合操作符。

除了显示在表中的操作符,还定义了tsvectortsquery类型的普通B-tree比较操作符(=<等)。它们对于文本搜索不是很有用,但是允许使用。例如,建在这些类型列上的唯一索引。

表 9-38. 文本搜索函数

函数返回类型描述例子结果
get_current_ts_config() regconfig获得默认文本搜索配置get_current_ts_config()english
length(tsvector) integertsvector中的词位数length('fat:2,4 cat:3 rat:5A'::tsvector)3
numnode(tsquery) integertsquery中词位外加操作符的数目 numnode('(fat & rat) | cat'::tsquery)5
plainto_tsquery([ config regconfig , ] query text) tsquery产生tsquery但忽略标点符号plainto_tsquery('english', 'The Fat Rats')'fat' & 'rat'
querytree(query tsquery) text获得一个tsquery的可索引部分querytree('foo & ! bar'::tsquery)'foo'
setweight(tsvector, "char") tsvectortsvector的每一个元素分配权重setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
strip(tsvector) tsvectortsvector中移除位置和权重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'
to_tsvector([ config regconfig , ] document text) tsvector缩减文档文本成tsvectorto_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
ts_headline([ config regconfig, ] document text, query tsquery [, options text ]) text显示一个查询匹配ts_headline('x y z', 'z'::tsquery)x y <b>z</b>
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_rewrite(query tsquery, target tsquery, substitute tsquery) tsquery在查询内用 substitute 替换 target 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' )
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-39中的函数被单独列出,因为它们通常不被用于日常的文本搜索操作。 它们有助于开发和调试新的文本搜索配置。

表 9-39. 文本搜索调试函数

函数返回类型描述例子结果
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) ...
<
/BODY >