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

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

表 9.40. Literals 搜索运算符

Operator Return Type Description Example Result
@@ boolean tsvector匹配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
&& tsquery tsquery在一起 'fat | rat'::tsquery && 'cat'::tsquery ( 'fat' | 'rat' ) & 'cat'
|| tsquery tsquery个在一起 'fat | rat'::tsquery || 'cat'::tsquery ( 'fat' | 'rat' ) | 'cat'
!! tsquery 否定tsquery !! 'cat'::tsquery !'cat'
<-> tsquery tsquery,然后是tsquery to_tsquery('fat') <-> to_tsquery('rat') 'fat' <-> 'rat'
@> boolean tsquery包含另一个? 'cat'::tsquery @> 'cat & rat'::tsquery f
<@ boolean tsquery包含在中? 'cat'::tsquery <@ 'cat & rat'::tsquery t

Note

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

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

表 9.41. Literals 搜索功能

Function Return Type Description Example Result
array_to_tsvector(text[]) tsvector 将词素数组转换为tsvector array_to_tsvector('{fat,cat,rat}'::text[]) 'cat' 'fat' 'rat'
get_current_ts_config() regconfig 获取默认的文本搜索配置 get_current_ts_config() english
length(tsvector) integer tsvector中的词素数量 length('fat:2,4 cat:3 rat:5A'::tsvector) 3
numnode(tsquery) integer tsquery中的词素加运算符的数量 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) 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'
to_tsvector([ config regconfig , ] document text) tsvector 将文档 Literals 减少到tsvector to_tsvector('english', 'The Fat Rats') 'fat':2 'rat':3
to_tsvector([ config regconfig , ] document json(b)) tsvector 将文档中的每个字符串值都减小为tsvector,然后按文档 Sequences 将其串联以产生单个tsvector to_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 触发功能,可自动更新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)
unnest(tsvector, OUT lexeme text, OUT positions smallint[], OUT weights text) setof record tsvector扩展到一组行 unnest('fat:2,4 cat:3 rat:5A'::tsvector) (cat,{3},{D}) ...

Note

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

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

表 9.42. 文本搜索调试功能

Function Return Type Description Example 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 测试配置 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) ...
上一章 首页 下一章