PostgreSQL 中文参考文档
🌐 英文原版 官方 ↗

9.13. 全文检索函数与操作符 #

Table 9.42、 Table 9.43 和 Table 9.44 总结了为全文检索提供的函数和操作符。 关于 PostgreSQL 的文本检索设施的详细解释, 请参见 Chapter 12。

Table 9.42. 文本搜索操作符

操作符

说明

示例

tsvector @@ tsquery → boolean

tsquery @@ tsvector → boolean

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

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

text @@ tsquery → boolean

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

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

tsvector || tsvector → tsvector

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

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

tsquery && tsquery → tsquery

把两个 tsquery 做 AND, 产生一个匹配同时匹配两个输入查询的文档的查询。

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

tsquery || tsquery → tsquery

把两个 tsquery 做 OR, 产生一个匹配任一输入查询所匹配文档的查询。

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

!! tsquery → tsquery

对一个 tsquery 取反, 产生一个匹配不匹配输入查询的文档的查询。

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

tsquery <-> tsquery → tsquery

构造一个短语查询, 它在两个输入查询于相继词位上匹配时成功匹配。

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

tsquery @> tsquery → boolean

第一个 tsquery 是否包含第二个? (这只考虑一个查询中出现的所有词位是否都出现在另一个查询中, 而忽略组合操作符。)

'cat'::tsquery @> 'cat & rat'::tsquery → f

tsquery <@ tsquery → boolean

第一个 tsquery 是否被包含在第二个之中? (这只考虑一个查询中出现的所有词位是否都出现在另一个查询中, 而忽略组合操作符。)

'cat'::tsquery <@ 'cat & rat'::tsquery → t

'cat'::tsquery <@ '!cat & rat'::tsquery → t


除了这些专用操作符之外, Table 9.1 中展示的常规比较操作符也可用于 tsvector 和 tsquery 类型。 这些对文本检索不是很有用, 但允许例如在这些类型的列上构建唯一索引。

Table 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

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'

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

把指定的 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 赋给 vector 中列于 lexemes 的元素。 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

选择 JSON 文档中由 filter 请求的每个项, 并把每一项转换为 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

以缩写的形式显示 query 在 document 中的匹配, 该文档必须是原始文本, 而非 tsvector。 在与查询匹配之前, 文档中的词会按照指定的或默认的配置规范化。 这个函数的使用在 Section 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 的匹配。 更多细节请参见 Section 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

计算一个表示 vector 与 query 匹配程度的分数。 细节请参见 Section 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

使用覆盖密度算法计算一个表示 vector 与 query 匹配程度的分数。 细节请参见 Section 12.3.3。

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

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

在 query 内把出现的 target 替换为 substitute。 细节请参见 Section 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 的若干部分。 细节请参见 Section 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

构造一个短语查询, 搜索 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}

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}


Note

所有接受可选 regconfig 参数的文本检索函数, 在省略该参数时都会使用 default_text_search_config 所指定的配置。

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

Table 9.44. 文本搜索调试函数

函数

说明

示例

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

按照指定的或默认的文本检索配置, 从 document 中提取并规范化记号, 并返回关于每个记号如何被处理的信息。 细节请参见 Section 12.8.1。

ts_debug('english', 'The Brightest supernovaes') → (asciiword,"Word, all ASCII",The,{english_stem},english_stem,{}) ...

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

如果输入记号对该词典是已知的, 则返回一个由替换词位组成的数组; 如果该记号对词典是已知的但它是一个停用词, 则返回一个空数组; 如果它不是一个已知的词, 则返回 NULL。 细节请参见 Section 12.8.3。

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

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

使用命名的解析器从 document 中提取记号。 细节请参见 Section 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 中提取记号。 细节请参见 Section 12.8.2。

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

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

返回一个表, 描述命名的解析器能够识别的每种记号类型。 细节请参见 Section 12.8.2。

ts_token_type('default') → (1,asciiword,"Word, all ASCII") ...

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

返回一个表, 描述由 OID 指定的一个解析器能够识别的每种记号类型。 细节请参见 Section 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 列, 并返回关于数据中每个不同词位的统计信息。 细节请参见 Section 12.4.4。

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