PostgreSQL 文本搜索函數(shù)和操作符

2021-08-27 09:55 更新

表 9.41, 表 9.42 and 表 9.43總結(jié)了為全文搜索提供的函數(shù)和操作符。PostgreSQL的文本搜索功能的詳細(xì)解釋可參考第 12 章。

表 9.41. 文本搜索操作符

操作符

描述

例子

tsvector @@ tsqueryboolean

tsquery @@ tsvectorboolean

tsvector匹配tsquery嗎?(參數(shù)可以按任意順序給出。)

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

text @@ tsqueryboolean

隱式調(diào)用to_tsvector()后的文本字符串匹配tsquery么 ?

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

tsvector @@@ tsqueryboolean

tsquery @@@ tsvectorboolean

這是@@已棄用的同義詞。

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

tsvector || tsvectortsvector

連接兩個(gè)tsvector。如果兩個(gè)輸入都包含詞素位置,則相應(yīng)地調(diào)整第二個(gè)輸入的位置。

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

tsquery && tsquerytsquery

ANDs兩個(gè)tsquery一起,生成一個(gè)匹配兩個(gè)輸入查詢(xún)的匹配文檔的查詢(xún)。

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

tsquery || tsquerytsquery

ORs兩個(gè)tsquery一起,生成一個(gè)匹配兩個(gè)輸入查詢(xún)的匹配文檔的查詢(xún)。

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

!! tsquerytsquery

否定tsquery,生成一個(gè)與輸入查詢(xún)不匹配的匹配文檔的查詢(xún)。

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

tsquery <-> tsquerytsquery

構(gòu)造一個(gè)短語(yǔ)查詢(xún),如果兩個(gè)輸入查詢(xún)?cè)谶B續(xù)的詞素上匹配,該查詢(xún)將進(jìn)行匹配。

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

tsquery @> tsqueryboolean

第一個(gè)tsquery包含了第二個(gè)嗎?(這只考慮出現(xiàn)在一個(gè)查詢(xún)中的所有詞素是否出現(xiàn)在另一個(gè)查詢(xún)中,忽略了組合操作符。)

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

tsquery <@ tsqueryboolean

第一個(gè)tsquery包含在第二個(gè)中嗎?(這只考慮出現(xiàn)在一個(gè)查詢(xún)中的所有詞素是否出現(xiàn)在另一個(gè)查詢(xún)中,而忽略了組合操作符。)

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

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


除了這些專(zhuān)用操作符之外, 表 9.1 中所示的常用比較操作符也適用于tsvectortsquery類(lèi)型。 它們對(duì)于文本搜索不是很有用,但是允許使用。例如,建在這些類(lèi)型列上的唯一索引。

表 9.42. 文本搜索函數(shù)

函數(shù)

描述

例子

array_to_tsvector ( text[] ) → tsvector

將詞素?cái)?shù)組轉(zhuǎn)換為tsvector。給定的字符串按原樣使用,不做進(jìn)一步處理。

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

get_current_ts_config ( ) → regconfig

返回當(dāng)前默認(rèn)文本搜索配置的OID(由 default_text_search_config 所設(shè)定的).

get_current_ts_config()english

length ( tsvector ) → integer

返回tsvector中的詞位數(shù)。

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

numnode ( tsquery ) → integer

返回tsquery中詞位和操作符的數(shù)目。

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

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

將文本轉(zhuǎn)換為tsquery,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。 字符串中的任何標(biāo)點(diǎn)符號(hào)都會(huì)被忽略(它不決定查詢(xún)操作符)。結(jié)果查詢(xún)匹配文本中包含所有非停止詞的文檔。

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

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

將文本轉(zhuǎn)換為tsquery,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。 字符串中的任何標(biāo)點(diǎn)符號(hào)都會(huì)被忽略(它不決定查詢(xún)操作符)。結(jié)果查詢(xún)匹配包含文本中所有非停止詞的短語(yǔ)。

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

將文本轉(zhuǎn)換為tsquery,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。引用的單詞序列被轉(zhuǎn)換為短語(yǔ)測(cè)試。 or一詞被理解為產(chǎn)生OR操作符,而破折號(hào)產(chǎn)生NOT操作符;其他標(biāo)點(diǎn)符號(hào)被忽略。這類(lèi)似于一些常見(jiàn)的網(wǎng)絡(luò)搜索工具的行為。

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

querytree ( tsquery ) → text

生成tsquery的可轉(zhuǎn)位部分的表示。結(jié)果為空或僅為T表示不可索引查詢(xún)。

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

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

將指定的weight賦給vector的每個(gè)元素。

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賦給列在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中移除位置和權(quán)重。

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

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

將文本轉(zhuǎn)換為tsquery,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。單詞必須由有效的tsquery操作符組合。

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

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

將文本轉(zhuǎn)換為tsvector,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。結(jié)果中包含位置信息。

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文檔中的每個(gè)字符串值轉(zhuǎn)換為tsvector,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。 然后將結(jié)果按文檔順序連接起來(lái)以產(chǎn)生輸出。位置信息就像在每對(duì)字符串值之間存在一個(gè)停止詞一樣生成。 (注意,當(dāng)輸入為jsonb時(shí),JSON對(duì)象的字段的document order取決于實(shí)現(xiàn);請(qǐng)觀察這些例子中的差異。)

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請(qǐng)求的JSON文檔中的每個(gè)項(xiàng),并將每個(gè)項(xiàng)轉(zhuǎn)換為tsvector,根據(jù)指定的或默認(rèn)配置對(duì)單詞進(jìn)行標(biāo)準(zhǔn)化。 然后將結(jié)果按文檔順序連接起來(lái)以產(chǎn)生輸出。位置信息就像在每對(duì)選定的項(xiàng)目之間存在一個(gè)停止詞一樣生成。 (注意,當(dāng)輸入為jsonb時(shí),JSON對(duì)象字段的document order取決于實(shí)現(xiàn)。) filter 必須是一個(gè)jsonb數(shù)組,其中包含0個(gè)或多個(gè)關(guān)鍵字: "string"(包括所有字符串值), "numeric"(包括所有數(shù)值), "boolean"(包括所有布爾值), "key"(包括所有鍵),或 "all"(包括以上所有關(guān)鍵字)。 作為一種特殊情況,該filter也可以是這些關(guān)鍵字之一的簡(jiǎn)單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中刪除任何出現(xiàn)的給定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中出現(xiàn)的任何詞位。

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

以縮寫(xiě)形式顯示documentquery的匹配項(xiàng),該匹配項(xiàng)必須是原始文本,而不是tsvector。 在匹配查詢(xún)之前,文檔中的單詞將根據(jù)指定的或默認(rèn)的配置進(jìn)行規(guī)范化。 第 12.3.4 節(jié)中討論了該函數(shù)的使用,還描述了可用的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

以縮寫(xiě)形式顯示匹配JSONdocument中字符串值中的query。 更多細(xì)節(jié)請(qǐng)參閱 第 12.3.4 節(jié)。

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

計(jì)算一個(gè)分?jǐn)?shù),顯示vectorquery的匹配程度。詳情請(qǐng)參見(jiàn)第 12.3.3 節(jié)。

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

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

使用覆蓋密度算法計(jì)算一個(gè)分?jǐn)?shù),顯示vectorquery的匹配程度。 詳情參見(jiàn)第 12.3.3 節(jié)。

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

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

query中使用 substitute替換出現(xiàn)的target。 詳情參見(jiàn) 第 12.4.2.1 節(jié) 。

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

ts_rewrite ( query tsquery, select text ) → tsquery

根據(jù)目標(biāo)替換部分query,并替換通過(guò)執(zhí)行SELECT命令獲得的查詢(xún)。 詳情參見(jiàn)第 12.4.2.1 節(jié)

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

tsquery_phrase ( query1 tsquery, query2 tsquery ) → tsquery

構(gòu)造一個(gè)短語(yǔ)查詢(xún),在連續(xù)的詞位上搜索query1query2的匹配項(xiàng)(與<->操作符相同)。

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

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

構(gòu)造一個(gè)短語(yǔ)查詢(xún),用于搜索query1query2的匹配項(xiàng),這些匹配項(xiàng)恰好出現(xiàn)在distance詞位之間。

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

tsvector_to_array ( tsvector ) → text[]

tsvector轉(zhuǎn)換為詞位的數(shù)組。

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展開(kāi)為一組行,每個(gè)行對(duì)應(yīng)一個(gè)詞位。

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}


注意

所有接受一個(gè)可選的regconfig參數(shù)的文本搜索函數(shù)在該參數(shù)被忽略時(shí),使用由default_text_search_config指定的配置。

表 9.43中的函數(shù)被單獨(dú)列出,因?yàn)樗鼈兺ǔ2槐挥糜谌粘5奈谋舅阉鞑僮鳌?它們主要有助于開(kāi)發(fā)和調(diào)試新的文本搜索配置。

表 9.43. 文本搜索調(diào)試函數(shù)

函數(shù)

描述

例子

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

根據(jù)指定的或默認(rèn)的文本搜索配置從document中提取和標(biāo)準(zhǔn)化標(biāo)記,并返回關(guān)于每個(gè)標(biāo)記是如何處理的信息。 詳請(qǐng)參見(jiàn)第 12.8.1 節(jié)。

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

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

如果字典知道輸入標(biāo)記,則返回替換詞位數(shù)組;如果字典知道標(biāo)記,但它是停止詞,則返回空數(shù)組;如果它不是已知詞,則返回NULL。 詳情參見(jiàn)第 12.8.3 節(jié)。

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

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

使用命名的解析器從document中提取標(biāo)記。詳情參見(jiàn)第 12.8.2 節(jié)。

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

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

使用OID指定的解析器從document中提取標(biāo)記。 詳請(qǐng)參見(jiàn) 第 12.8.2 節(jié) 。

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

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

返回一個(gè)表,該表描述命名解析器可以識(shí)別的每種類(lèi)型的標(biāo)記。詳請(qǐng)參見(jiàn)第 12.8.2 節(jié)

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

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

返回一個(gè)表,該表描述OID指定的解析器可以識(shí)別的每種標(biāo)記類(lèi)型。詳請(qǐng)參見(jiàn)第 12.8.2 節(jié)。

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

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

執(zhí)行sqlquery,該查詢(xún)必須返回單個(gè)tsvector列,并返回關(guān)于數(shù)據(jù)中包含的每個(gè)不同詞位的統(tǒng)計(jì)信息。 詳請(qǐng)參見(jiàn)第 12.4.4 節(jié)。

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



以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)