postgresql----文本搜索類型和檢索函數


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)

 

為什么呢???我也沒明白,繼續往下看吧!

 

文本檢索函數和操作符

操作符

操作符 返回類型 描述 示例 結果
@@ boolean tsvector是否匹配tsquery select to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat'); t
@@@ boolean 廢棄的@@    
|| tsvector 連接tsvector select 'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector; 'a':1 'b':2,5 'c':3 'd':4
&& tsquery tsquery與 select 'fat | rat'::tsquery && 'cat'::tsquery; ( 'fat' | 'rat' ) & 'cat'
|| tsquery tsquery或 select 'fat | rat'::tsquery || 'cat'::tsquery; 'fat' | 'rat' | 'cat'
!! tsquery tsquery非 select !! 'cat'::tsquery; !'cat'
<-> tsquery tsquery緊跟tsquery select to_tsquery('fat') <-> to_tsquery('rat'); 'fat' <-> 'rat'
@> boolean tsquery包含另一個tsquery select 'cat'::tsquery @> 'cat & rat'::tsquery; f
<@ boolean tsquery是否包含於另一個tsquery select 'cat'::tsquery <@ 'cat & rat'::tsquery; t

函數

函數 返回類型 描述 示例 結果
array_to_tsvector(text[]) tsvector 將text數組轉換成tsvector select array_to_tsvector('{fat,cat,rat}'::text[]); 'fat' 'cat' 'rat'
get_current_ts_config() regconfig 獲取當前文本檢索配置 select get_current_ts_config(); english
length(tsvector) integer tsvector中單詞個數 select length('fat:2,4 cat:3 rat:5A'::tsvector); 3
numnode(tsquery) integer tsquery中的單詞加上操作符的數量 select numnode('(fat & rat) | cat'::tsquery); 5
plainto_tsquery([ config regconfig , ] query text) tsquery 忽略標點(punctuation)生成tsquery select 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 tsvectorweight "char") tsvector 給tsvector每一個元素賦權 select setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A'); 'cat':3A 'fat':2A,4A 'rat':5A
setweight(vector tsvectorweight "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 標准化單詞並轉換為tsquery select to_tsquery('english', 'The & Fat & Rats'); 'fat' & 'rat'
to_tsvector([ config regconfig , ] document text) tsvector 減少文本至tsvector select to_tsvector('english', 'The Fat Rats'); 'fat':2 'rat':3
ts_delete(vector tsvectorlexeme text) tsvector 從tsvector中刪除指定元素 select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat'); 'cat':3 'rat':5A
ts_delete(vector tsvectorlexemes text[]) tsvector 從tsvector中刪除指定的一組元素 select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']); 'cat':3
ts_filter(vector tsvectorweights "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 tsquerytarget tsquery,

 substitute tsquery)

tsquery 使用 substitute替換target中query select ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery); 'b' & ( 'foo' | 'bar' )
ts_rewrite(query tsqueryselect text) tsquery 從SELECT結果中得到的substitute和target來替換query,前提是SELECT必須要有結果 SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases')  
tsquery_phrase(query1 tsqueryquery2 tsquery) tsquery 功能同操作符 <->  select tsquery_phrase(to_tsquery('fat'), to_tsquery('cat')); 'fat' <-> 'cat'

tsquery_phrase(query1 tsqueryquery2 tsquery

distanceinteger)

tsquery 確保query1和query2之間距離最大為distance select 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM