支援的版本:目前 (17) / 16 / 15 / 14 / 13
開發版本:devel
不再支援的版本:12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3

9.13. 文字搜尋函數和運算符 #

表 9.42表 9.43表 9.44 總結了為全文搜尋提供的函數和運算符。有關 PostgreSQL 文字搜尋功能的詳細說明,請參閱第 12 章

表 9.42. 文字搜尋運算符

運算符

描述

範例

tsvector @@ tsqueryboolean

tsquery @@ tsvectorboolean

tsvector 是否與 tsquery 匹配?(參數可以以任何順序給出。)

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

text @@ tsqueryboolean

文字字串在隱式調用 to_tsvector() 後,是否與 tsquery 匹配?

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

tsvector || tsvectortsvector

串聯兩個 tsvector。如果兩個輸入都包含語素位置,則相應地調整第二個輸入的位置。

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

tsquery && tsquerytsquery

將兩個 tsquery 與 AND 運算結合在一起,產生一個與同時匹配兩個輸入查詢的文件匹配的查詢。

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

tsquery || tsquerytsquery

將兩個 tsquery 與 OR 運算結合在一起,產生一個與匹配任何一個輸入查詢的文件匹配的查詢。

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

!! tsquerytsquery

否定 tsquery,產生一個與不匹配輸入查詢的文件匹配的查詢。

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

tsquery <-> tsquerytsquery

建構一個詞組查詢,如果兩個輸入查詢在連續的語素處匹配,則匹配。

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

tsquery @> tsqueryboolean

第一個 tsquery 是否包含第二個?(這僅考慮一個查詢中出現的所有語素是否出現在另一個查詢中,忽略組合運算符。)

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

tsquery <@ tsqueryboolean

第一個 tsquery 是否包含在第二個中?(這僅考慮一個查詢中出現的所有語素是否出現在另一個查詢中,忽略組合運算符。)

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

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


除了這些專用運算符之外,表 9.1 中顯示的常用比較運算符也適用於 tsvectortsquery 類型。這些對於文字搜尋不是很有用,但允許例如在這些類型的列上建構唯一索引。

表 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 字串被視為詞素(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

以縮寫形式顯示 querydocument 中的匹配項,document 必須是原始文字,而不是 tsvector。在與查詢匹配之前,文件中的單字會根據指定或預設的組態進行正規化。此函數的用法在第 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 的匹配項。有關更多詳細資料,請參閱第 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

計算一個分數,顯示 vectorquery 的匹配程度。 有關詳細資料,請參閱第 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

計算一個分數,顯示 vectorquery 的匹配程度,使用覆蓋密度演算法。 有關詳細資料,請參閱第 12.3.3 節

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

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

querytarget 的出現替換為 substitute。 有關詳細資料,請參閱第 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 指令所獲得的目標 (target) 與替換 (substitute) 內容,替換 query 的部分內容。詳情請參閱第 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

建構一個片語查詢,尋找連續詞位 (lexeme) 中 query1query2 的匹配項(與 <-> 運算子相同)。

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

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

建構一個片語查詢,尋找間隔恰好 distance 個詞位的 query1query2 的匹配項。

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

tsvector_to_array ( tsvector ) → text[]

tsvector 轉換為詞位 (lexeme) 的陣列。

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 展開為一組列,每列一個詞位 (lexeme)。

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}

注意

所有接受可選 regconfig 引數的文字搜尋函式,如果省略該引數,將使用由 default_text_search_config 指定的組態。

表 9.44 中的函式被單獨列出,因為它們通常不用於日常文字搜尋操作。它們主要有助於開發和偵錯新的文字搜尋組態。

表 9.44. 文字搜尋偵錯函式

函數

描述

範例

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

根據指定的或預設的文字搜尋組態,從 document 中提取和正規化 (normalize) 詞符 (token),並返回有關每個詞符 (token) 如何被處理的資訊。詳情請參閱第 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[]

如果輸入詞符 (token) 為字典所知,則返回替換詞位 (lexeme) 的陣列;如果該詞符 (token) 為字典所知,但它是停用詞 (stop word),則返回一個空陣列;如果它不是已知的單字,則返回 NULL。詳情請參閱第 12.8.3 節

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

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

使用指定的剖析器 (parser) 從 document 中提取詞符 (token)。詳情請參閱第 12.8.2 節

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

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

使用由 OID 指定的剖析器 (parser) 從 document 中提取詞符 (token)。詳情請參閱第 12.8.2 節

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

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

返回一個表格,描述指定名稱的剖析器 (parser) 可以識別的每種類型的詞符 (token)。詳情請參閱第 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 指定的剖析器 (parser) 可以識別的每種類型的詞符 (token)。詳情請參閱第 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 欄位,並返回有關資料中包含的每個不同詞位 (lexeme) 的統計資訊。詳情請參閱第 12.4.4 節

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


提交更正

如果您在文件中發現任何不正確、與您特定功能的使用經驗不符或需要進一步澄清之處,請使用此表單回報文件問題。