9.13. 文本搜索功能和运算符

Table 9.40Table 9.41Table 9.42概述了为全文搜索提供的功能和运算符。有关 PostgreSQL 文本搜索功能的详细说明,请参见Chapter 12

表 9.40. Literals 搜索运算符

OperatorReturn TypeDescriptionExampleResult
@@booleantsvector匹配tsquery吗?to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t
@@@boolean@@的已弃用同义词to_tsvector('fat cats ate rats') @@@ to_tsquery('cat & rat')t
||tsvector串联tsvector s'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4
&&tsquerytsquery在一起'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'
||tsquerytsquery个在一起'fat | rat'::tsquery || 'cat'::tsquery( 'fat' | 'rat' ) | 'cat'
!!tsquery否定tsquery!! 'cat'::tsquery!'cat'
<->tsquerytsquery,然后是tsqueryto_tsquery('fat') <-> to_tsquery('rat')'fat' <-> 'rat'
@>booleantsquery包含另一个?'cat'::tsquery @> 'cat & rat'::tsqueryf
<@booleantsquery包含在中?'cat'::tsquery <@ 'cat & rat'::tsqueryt

Note

tsquery包含运算符仅考虑两个查询中列出的词素,而忽略合并运算符。

除了表中所示的运算符外,还为类型tsvectortsquery定义了普通的 B 树比较运算符(=<等)。这些对于文本搜索不是很有用,但是例如允许在这些类型的列上构建唯一索引。

表 9.41. Literals 搜索功能

FunctionReturn TypeDescriptionExampleResult
array_to_tsvector(text[])tsvector将词素数组转换为tsvectorarray_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'
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'
phraseto_tsquery([ config regconfig , ] query text)tsquery产生tsquery来搜寻词组,而忽略标点符号phraseto_tsquery('english', 'The Fat Rats')'fat' <-> 'rat'
querytree(query tsquery)text获取tsquery的可索引部分querytree('foo & ! bar'::tsquery)'foo'
setweight(vector tsvector, weight "char")tsvector将* weight 分配给 vector *的每个元素setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A')'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvector, weight "char", lexemes text[])tsvector将* weight 分配给 lexemes 中列出的 vector *元素setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 '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将文档 Literals 减少到tsvectorto_tsvector('english', 'The Fat Rats')'fat':2 'rat':3
to_tsvector([ config regconfig , ] document json(b))tsvector将文档中的每个字符串值都减小为tsvector,然后按文档 Sequences 将其串联以产生单个tsvectorto_tsvector('english', '{"a": "The Fat Rats"}'::json)'fat':2 'rat':3
ts_delete(vector tsvector, lexeme text)tsvector从* vector 中删除给定的 lexeme *ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat')'cat':3 'rat':5A
ts_delete(vector tsvector, lexemes text[])tsvector从* vector 中删除 lexemes *中出现的所有词素ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat'])'cat':3
ts_filter(vector tsvector, weights "char"[])tsvector从* vector 中仅选择具有给定 weights *的元素ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A
ts_headline([ config regconfig, ] document text, query tsquery [, options text ])text显示查询匹配ts_headline('x y z', 'z'::tsquery)x y <b>z</b>
ts_headline([ config regconfig, ] document json(b), query tsquery [, options text ])text显示查询匹配ts_headline('{"a":"x y z"}'::json, 'z'::tsquery){"a":"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在查询中将* target 替换为 substitute *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' )
tsquery_phrase(query1 tsquery, query2 tsquery)tsquery进行查询,搜索* query1 后跟 query2 *(与<->运算符相同)tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'
tsquery_phrase(query1 tsquery, query2 tsquery, distance integer)tsquery进行查询,搜索* query1 ,后跟 query2 * * distance *tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10)'fat' <10> 'cat'
tsvector_to_array(tsvector)text[]tsvector转换为词素数组tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector){cat,fat,rat}
tsvector_update_trigger()trigger触发功能,可自动更新tsvectorCREATE TRIGGER ... tsvector_update_trigger(tsvcol, 'pg_catalog.swedish', title, body)
tsvector_update_trigger_column()trigger触发功能,可自动更新tsvectorCREATE TRIGGER ... tsvector_update_trigger_column(tsvcol, configcol, title, body)
unnest(tsvector, OUT lexeme text, OUT positions smallint[], OUT weights text)setof recordtsvector扩展到一组行unnest('fat:2,4 cat:3 rat:5A'::tsvector)(cat,{3},{D}) ...

Note

当省略该参数时,所有接受可选regconfig参数的文本搜索功能都将使用default_text_search_config指定的配置。

Table 9.42中的功能被单独列出,因为它们通常不在日常文本搜索操作中使用。它们有助于开发和调试新的文本搜索配置。

表 9.42. 文本搜索调试功能

FunctionReturn TypeDescriptionExampleResult
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) ...