您的位置:首页 > 数据库

postgresql----文本搜索类型和检索函数

2016-07-10 09:18 387 查看
postgresql提供两种数据类型用于支持全文检索:tsvector类型产生一个文档(以优化全文检索形式)和tsquery类型用于查询检索。

tsvector的值是一个无重复的lexemes排序列表(什么是lexemes?),比如将一个字符串转换为tsvector类型:

test=# SELECT $$the lexeme ' ' contains spaces$$::tsvector;
tsvector
----------------------------------------
' ' 'contains' 'lexeme' 'spaces' 'the'
(1 row)


可以在单词后面跟:数字表示单词在字符串中的位置,数字的范围是1-16383。

test=# SELECT $$a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12$$::tsvector;
tsvector
-------------------------------------------------------------------------------
'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
(1 row)


还可以在位置后面跟字母标识权,字母的范围是A,B,C,D,默认是D,不显示。

test=# SELECT $$a:1A fat:2B,4C cat:5D$$::tsvector;
tsvector
----------------------------
'a':1A 'cat':5 'fat':2B,4C
(1 row)


tsquery存储用于检索的词汇,并且使用布尔操作符&(AND),|(OR),!(NOT)来组合它们。!(NOT)结合的最紧密,而&(AND)结合的比|(OR)紧密,也可以用括号来强调分组。

test=# SELECT $$fat & rat$$::tsquery;
tsquery
---------------
'fat' & 'rat'
(1 row)

test=#
test=# SELECT $$fat & (rat | cat)$$::tsquery;
tsquery
---------------------------
'fat' & ( 'rat' | 'cat' )
(1 row)

test=#
test=# SELECT $$fat & rat & ! cat$$::tsquery;
tsquery
------------------------
'fat' & 'rat' & !'cat'
(1 row)


tsquery中的词汇可以用*进行标记来指定前缀匹配:

test=# SELECT to_tsvector('postgraduate');
to_tsvector
---------------
'postgradu':1
(1 row)

test=# SELECT to_tsquery( 'postgres:*');
to_tsquery
------------
'postgr':*
(1 row)

test=# SELECT to_tsvector('postgraduate') @@ to_tsquery( 'postgres:*');
?column?
----------
t
(1 row)


为什么呢???我也没明白,继续往下看吧!

文本检索函数和操作符

操作符

操作符返回类型描述示例结果
@@booleantsvector是否匹配tsqueryselect to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat');t
@@@boolean废弃的@@
||tsvector连接tsvectorselect 'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector;'a':1 'b':2,5 'c':3 'd':4
&&tsquerytsquery与select 'fat | rat'::tsquery && 'cat'::tsquery;( 'fat' | 'rat' ) & 'cat'
||tsquerytsquery或select 'fat | rat'::tsquery || 'cat'::tsquery;'fat' | 'rat' | 'cat'
!!tsquerytsquery非select !! 'cat'::tsquery;!'cat'
<->tsquerytsquery紧跟tsqueryselect to_tsquery('fat') <-> to_tsquery('rat');'fat' <-> 'rat'
@>booleantsquery包含另一个tsqueryselect 'cat'::tsquery @> 'cat & rat'::tsquery;f
<@booleantsquery是否包含于另一个tsqueryselect 'cat'::tsquery <@ 'cat & rat'::tsquery;t
函数

函数返回类型描述示例结果
array_to_tsvector(text[])tsvector将text数组转换成tsvectorselect array_to_tsvector('{fat,cat,rat}'::text[]);'fat' 'cat' 'rat'
get_current_ts_config()regconfig获取当前文本检索配置select get_current_ts_config();english
length(tsvector)integertsvector中单词个数select length('fat:2,4 cat:3 rat:5A'::tsvector);3
numnode(tsquery)integertsquery中的单词加上操作符的数量select numnode('(fat & rat) | cat'::tsquery);5
plainto_tsquery([ config regconfig , ] query text)tsquery忽略标点(punctuation)生成tsqueryselect plainto_tsquery('english', 'The Fat Rats');'fat' & 'rat'
phraseto_tsquery([ config regconfig , ] query text)tsquery忽略标点(punctuation)生成tsquery用于检索短语select phraseto_tsquery('english', 'The Fat Rats');'fat' <-> 'rat'
querytree(query tsquery)text获取tsquery可索引部分select querytree('foo & ! bar'::tsquery);'foo'
setweight(vector tsvector, weight "char")tsvector给tsvector每一个元素赋权select setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A');'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvector, weight "char", lexemestext[])tsvector给tsvector给指定元素赋权select setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}');'cat':3A 'fat':2,4 'rat':5A
strip(tsvector)tsvector删除tsvector中的位置和权select strip('fat:2,4 cat:3 rat:5A'::tsvector);'cat' 'fat' 'rat'
to_tsquery([ config regconfig , ] query text)tsquery标准化单词并转换为tsqueryselect to_tsquery('english', 'The & Fat & Rats');'fat' & 'rat'
to_tsvector([ config regconfig , ] document text)tsvector减少文本至tsvectorselect to_tsvector('english', 'The Fat Rats');'fat':2 'rat':3
ts_delete(vector tsvector, lexeme text)tsvector从tsvector中删除指定元素select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat');'cat':3 'rat':5A
ts_delete(vector tsvector, lexemes text[])tsvector从tsvector中删除指定的一组元素select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']);'cat':3
ts_filter(vector tsvector, weights "char"[])tsvector只查询指定权值的元素select ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}');'cat':3B 'rat':5A
ts_headline([ config regconfig, ] document text,

querytsquery [, options text ])

text显示一个查询匹配项select ts_headline('x y z', 'z'::tsquery);x y <b>z</b>
ts_rank([ weights float4[], ] vector tsvector,

querytsquery [, normalization integer ])

float4为查询进行文档排序select ts_rank('fat:2,4 cat:3b rat:5A'::tsvector, 'rat'::tsquery);0.607927
ts_rank_cd([ weights float4[], ] vector tsvector,

querytsquery [, normalization integer ])

float4使用覆盖密度为查询进行文档排序select ts_rank_cd('{0.1, 0.2, 0.4, 1.0}','fat:2,4 cat:3b rat:5A'::tsvector, 'rat'::tsquery);1
ts_rewrite(query tsquery, target tsquery,

substitute tsquery)

tsquery使用 substitute替换target中queryselect ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery);'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsquery, select text)tsquery从SELECT结果中得到的substitute和target来替换query,前提是SELECT必须要有结果SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')
tsquery_phrase(query1 tsquery, query2 tsquery)tsquery功能同操作符 <-> select tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'));'fat' <-> 'cat'
tsquery_phrase(query1 tsquery, query2 tsquery,

distanceinteger)

tsquery确保query1和query2之间距离最大为distanceselect tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10);'fat' <10> 'cat'
tsvector_to_array(tsvector)text[]将tsvector转换为数组select tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector);{cat,fat,rat}
tsvector_update_trigger()trigger更新tsvector列自动触发触发器函数
tsvector_update_trigger_column()trigger同上
unnest(tsvector, OUT lexeme text, OUT positionssmallint[], OUT weights text)setof record将tsvector扩展成行类型select unnest('fat:2,4 cat:3 rat:5A'::tsvector);(cat,{3},{D})
(fat,"{2,4}","{D,D}")
(rat,{5},{A})

工作中没有用过文本检索类型,英语也不好,有兴趣还是去看原版吧:

https://www.postgresql.org/docs/9.6/static/functions-textsearch.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: