Redrock Postgres 搜索 英文
版本: 9.3 / 9.4 / 9.5 / 9.6 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17

9.13. 文本搜索函数和运算符 #

表 9.42表 9.43表 9.44 汇总了为全文搜索提供的函数和运算符。有关 PostgreSQL 的文本搜索工具的详细说明,请参阅 第 12 章

表 9.42. 文本搜索运算符

运算符

说明

示例

tsvector @@ tsquerybool

tsquery @@ tsvectorbool

tsvector 是否匹配 tsquery?(参数可以按任意顺序给出。)

to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')t

text @@ tsqueryboolean

隐式调用 to_tsvector() 之后,文本字符串是否匹配 tsquery

'fat cats ate rats' @@ to_tsquery('cat & rat')t

tsvector || tsvectortsvector

连接两个 tsvector。如果两个输入都包含词素位置,则会相应调整第二个输入的位置。

'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector'a':1 'b':2,5 'c':3 'd':4

tsquery && tsquerytsquery

将两个 tsquery 进行 AND 操作,生成一个匹配同时匹配两个输入查询的文档的查询。

'fat | rat'::tsquery && 'cat'::tsquery( 'fat' | 'rat' ) & 'cat'

tsquery || tsquerytsquery

将两个 tsquery 进行 OR 操作,生成一个匹配同时匹配任意一个输入查询的文档的查询。

'fat | rat'::tsquery || 'cat'::tsquery'fat' | 'rat' | 'cat'

!! tsquerytsquery

tsquery 取反,生成查询,其匹配不匹配输入查询的文档。

!! 'cat'::tsquery!'cat'

tsquery <-> tsquerytsquery

构造一个短语查询,如果两个输入查询在连续词汇素上匹配,则匹配。

to_tsquery('fat') <-> to_tsquery('rat')'fat' <-> 'rat'

tsquery @> tsqueryboolean

第一个 tsquery 是否包含第二个?(这仅考虑一个查询中出现的全部词汇素是否出现在另一个查询中,忽略组合运算符)。

'cat'::tsquery @> 'cat & rat'::tsqueryf

tsquery <@ tsqueryboolean

第一个 tsquery 是否包含在第二个中?(这仅考虑一个查询中出现的全部词汇素是否出现在另一个查询中,忽略组合运算符)。

'cat'::tsquery <@ 'cat & rat'::tsqueryt

'cat'::tsquery <@ '!cat & rat'::tsqueryt


除了这些专门运算符外,表格 9.1中显示的常规比较运算符还可以用于类型 tsvectortsquery。对于文本搜索而言这些并不十分有用,但允许在这些类型的列上构建唯一索引。

表格 9.43。文本搜索函数

函数

说明

示例

array_to_tsvector ( text[] ) → tsvector

将文本字符串数组转换为一个 tsvector。给定的字符串被用作词汇素,不经进一步处理。数组元素不得为空字符串或 NULL

array_to_tsvector('{fat,cat,rat}'::text[])'cat' 'fat' 'rat'

get_current_ts_config ( ) → regconfig

返回当前默认文本搜索配置的 OID (default_text_search_config 设置)。

get_current_ts_config()english

lengthtsvector)→ integer

返回 tsvector 中的词素数。

length('fat:2,4 cat:3 rat:5A'::tsvector)3

numnodetsquery)→ 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'

phraseto_tsquery('english', 'The Cat and Rats')'cat' <2> 'rat'

websearch_to_tsquery([config regconfig]query text)→ tsquery

根据指定配置或默认配置,将文本转换为 tsquery,并对单词进行归一化。有引号的单词序列转换为短语测试。单词 or 被理解为产生 OR 操作符,破折号产生 NOT 操作符;其他标点符号会被忽略。这近似于一些常用网络搜索工具的行为。

websearch_to_tsquery('english', '"fat rat" or cat dog')'fat' <-> 'rat' | 'cat' & 'dog'

querytree ( tsquery ) → text

生成 tsquery 中可索引部分的表示。结果为空或仅为 T 表示一个不可索引查询。

querytree('foo & ! bar'::tsquery)'foo'

setweight ( vector tsvector, weight "char" ) → tsvector

vector 的每个元素分配指定的 weight

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

vector 中列在 lexemes 中的元素分配指定的 weight。在 lexemes 中的字符串被当作词素原样解析,不会进行进一步处理。任何与 vector 中任何词素不匹配的字符串都会被忽略。

setweight('fat:2,4 cat:3 rat:5,6B'::tsvector, 'A', '{cat,rat}')'cat':3A 'fat':2,4 'rat':5A,6A

strip ( tsvector ) → tsvector

tsvector 中移除位置和权重。

strip('fat:2,4 cat:3 rat:5A'::tsvector)'cat' 'fat' 'rat'

to_tsquery ( [ config regconfig, ] query text ) → tsquery

将文本转换为 tsquery,并根据指定的或默认配置对单词进行规范化。单词必须由有效的 tsquery 运算符组合。

to_tsquery('english', 'The & Fat & Rats')'fat' & 'rat'

to_tsvector ( [ config regconfig, ] document text ) → tsvector

将文本转换为 tsvector,并根据指定的或默认配置对单词进行规范化。位置信息包含在结果中。

to_tsvector('english', 'The Fat Rats')'fat':2 'rat':3

to_tsvector ( [ config regconfig, ] document json ) → tsvector

to_tsvector ( [ config regconfig, ] document jsonb ) → tsvector

将 JSON 文档中的每个字符串值转换为 tsvector,并根据指定或默认配置对单词进行规范化。然后按文档顺序连接结果以生成输出。位置信息生成时,每对字符串值之间都存在一个停用词。(请注意,当输入为 jsonb 时,JSON 对象的各个字段的 文档顺序 取决于实现;请注意示例中的差异。)

to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::json)'dog':5 'fat':2 'rat':3

to_tsvector('english', '{"aa": "The Fat Rats", "b": "dog"}'::jsonb)'dog':1 'fat':4 'rat':5

json_to_tsvector ( [ config regconfig, ] document json, filter jsonb ) → tsvector

jsonb_to_tsvector ( [ config regconfig, ] document jsonb, filter jsonb ) → tsvector

选出 filter 请求的 JSON 文档中的每个元素,并将它们转换为 tsvector,根据指定或默认配置规范化单词。然后,按文档顺序将结果连接起来以生成输出。位置信息生成得好像在每对选定项目之间存在一个停止词。(注意,当输入为 jsonb 时,JSON 对象的字段的 文档顺序 与实现有关。)filter 必须是包含零个或多个以下关键字的 jsonb 数组:"string"(包含所有字符串值),"numeric"(包含所有数值),"boolean"(包含所有布尔值),"key"(包含所有键)或 "all"(包含以上所有值)。作为特例,filter 也可以是其中一个关键字的简单 JSON 值。

json_to_tsvector('english', '{"a": "The Fat Rats", "b": 123}'::json, '["string", "numeric"]')'123':5 'fat':2 'rat':3

json_to_tsvector('english', '{"cat": "The Fat Rats", "dog": 123}'::json, '"all"')'123':9 'cat':1 'dog':7 'fat':4 'rat':5

ts_delete ( vector tsvector, lexeme text ) → tsvector

vector 中删除给定 lexeme 的所有出现。将 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 中所有词素的出现。将 lexemes 中的字符串视为“原样”词素,无需进一步处理。不与 vector 中的任何词素匹配的字符串将被忽略。

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,7c rat:5A'::tsvector, '{a,b}')'cat':3B 'rat':5A

ts_headline ( [ config regconfig, ] document text, query tsquery [, options text ] ) → text

以缩略形式显示 document(必须是原始文本,而不是 tsvector)中与 query 匹配的项。匹配查询之前,文档中的词语将根据指定配置或默认配置进行规范化。有关此函数的使用,请参阅 第 12.3.4 节,其中还介绍了可用的 options

ts_headline('The fat cat ate the rat.', 'cat')The fat <b>cat</b> ate the rat.

ts_headline ( [ config regconfig, ] document json, query tsquery [, options text ] ) → text

ts_headline ( [ config regconfig, ] document jsonb, query tsquery [, options text ] ) → text

以缩略形式显示 JSON document 中字符串值中出现的与 query 匹配的项。有关更多详细信息,请参见 第 12.3.4 节

ts_headline('{"cat":"raining cats and dogs"}'::jsonb, 'cat'){"cat": "raining <b>cats</b> and dogs"}

ts_rank ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real

计算一个分数,表示vectorquery匹配得如何。有关详细信息,请参阅第 12.3.3 节

ts_rank(to_tsvector('raining cats and dogs'), 'cat')0.06079271

ts_rank_cd ( [ weights real[], ] vector tsvector, query tsquery [, normalization integer ] ) → real

计算一个分数,表示vectorquery匹配得如何,使用封面密度算法。有关详细信息,请参阅第 12.3.3 节

ts_rank_cd(to_tsvector('raining cats and dogs'), 'cat')0.1

ts_rewrite ( query tsquery, target tsquery, substitute tsquery ) → tsquery

使用substitute替换querytarget的所有出现。有关详细信息,请参阅第 12.4.2.1 节

ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery)'b' & ( 'foo' | 'bar' )

ts_rewrite ( query tsquery, select text ) → tsquery

根据通过执行SELECT命令获得的目标和替换来替换query的部分。有关详细信息,请参阅第 12.4.2.1 节

SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')'b' & ( 'foo' | 'bar' )

tsquery_phrase ( query1 tsquery, query2 tsquery ) → tsquery

构造一个短语查询,搜索query1query2在连续词素中匹配(与<->运算符相同)。

tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'))'fat' <-> 'cat'

tsquery_phrase ( query1 tsquery, query2 tsquery, distance integer ) → tsquery

构建一个短语查询,搜索正好间隔 distance 个词素的 query1query2 的匹配项。

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}

unnest ( tsvector ) → setof record ( lexeme text, positions smallint[], weights text )

tsvector 扩展为按词素分类的一组行。

select * from unnest('cat:3 fat:2,4 rat:5A'::tsvector)

 lexeme | positions | weights
--------+-----------+---------
 cat    | {3}       | {D}
 fat    | {2,4}     | {D,D}
 rat    | {5}       | {A}

注意

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

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

表 9.44. 文本搜索调试函数

函数

说明

示例

ts_debug ( [ config regconfig, ] document text ) → setof record ( alias text, description text, token text, dictionaries regdictionary[], dictionary regdictionary, lexemes text[] )

根据指定的或默认文本搜索配置,从 document 中提取和规范化标记,并返回有关如何处理每个标记的信息。有关详细信息,请参见 第 12.8.1 节

ts_debug('english', 'The Brightest supernovaes')(asciiword,"文字,所有 ASCII,The,{english_stem},english_stem,{}) ...

ts_lexize ( dict regdictionary, token text ) → text[]

如果输入令牌已知于字典,则返回替换词素的数组,如果该令牌已知于字典但它是一个停止字,则返回一个空数组,如果它不是一个已知字,则返回 NULL。有关详细信息,请参见 12.8.3 节

ts_lexize('english_stem', 'stars'){star}

ts_parse ( parser_name text, document text ) → setof record ( tokid integer, token text )

使用指定的解析器从 document 中提取令牌。有关详细信息,请参见 12.8.2 节

ts_parse('default', 'foo - bar')(1,foo) ...

ts_parse ( parser_oid oid, document text ) → setof record ( tokid integer, token text )

使用由 OID 指定的解析器从 document 中提取令牌。有关详细信息,请参见 12.8.2 节

ts_parse(3722, 'foo - bar')(1,foo) ...

ts_token_type ( parser_name text ) → setof record ( tokid integer, alias text, description text )

返回一个表格,描述指定解析器可以识别的每种类型的令牌。有关详细信息,请参见 12.8.2 节

ts_token_type('default')(1,asciiword,"词,所有 ASCII") ...

ts_token_type ( parser_oid oid ) → setof record ( tokid integer, alias text, description text )

返回一个表,它描述了由 OID 指定的解析器可以识别的每种令牌类型。有关详细信息,请参见第 12.8.2 节

ts_token_type(3722)(1,asciiword,"Word, all ASCII") ...

ts_stat ( sqlquery text [, weights text ] ) → setof record ( word text, ndoc integer, nentry integer )

执行sqlquery,它必须返回一个tsvector列,并且返回有关数据中包含的每个不同语素的统计信息。有关详细信息,请参见第 12.4.4 节

ts_stat('SELECT vector FROM apod')(foo,10,15) ...