| PostgreSQL 9.3.4 文档 | ||||
|---|---|---|---|---|
| Prev | Up | Chapter 9. 函数和操作符 | Next | |
Table 9-36、 Table 9-37和 Table 9-38总结了为全文搜索提供的函数和操作符。PostgreSQL的文本搜索功能的详细解释可参考Chapter 12。
Table 9-36. 文本搜索操作符
| 操作符 | 描述 | 例子 | 结果 | 
|---|---|---|---|
| @@ | 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'::tsquery | f | 
| <@ | tsquery被包含? | 'cat'::tsquery <@ 'cat & rat'::tsquery | t | 
Note: tsquery的包含操作符只考虑两个查询中的词位,而忽略组合操作符。
除了显示在表中的操作符,还定义了tsvector和tsquery类型的普通B-tree比较操作符(=、<等)。它们对于文本搜索不是很有用,但是允许使用。例如,建在这些类型列上的唯一索引。
Table 9-37. 文本搜索函数
| 函数 | 返回类型 | 描述 | 例子 | 结果 | 
|---|---|---|---|---|
| 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' | 
| querytree(query tsquery) | text | 获得一个tsquery的可索引部分 | querytree('foo & ! bar'::tsquery) | 'foo' | 
| setweight(tsvector, "char") | tsvector | 为tsvector的每一个元素分配权重 | setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A') | 'cat':3A 'fat':2A,4A '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 | 缩减文档文本成tsvector | to_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) | 
Note: 所有接受一个可选的regconfig参数的文本搜索函数在该参数被忽略时,使用由default_text_search_config指定的配置。
Table 9-38中的函数被单独列出,因为它们通常不被用于日常的文本搜索操作。 它们有助于开发和调试新的文本搜索配置。
Table 9-38. 文本搜索调试函数
| 函数 | 返回类型 | 描述 | 例子 | 结果 | 
|---|---|---|---|---|
| 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) ... |