2025年9月25日: PostgreSQL 18 釋出!
支援的版本: 當前 (18) / 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

vector 的每個元素分配指定的 weight

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

vector 中列在 lexemes 中的元素分配指定的 weightlexemes 中的字串將按原樣用作詞位,無需進一步處理。與 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

根據 filter 請求的 JSON 文件中的每個項進行選擇,並將每個項轉換為 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 字串將按原樣用作詞位,無需進一步處理。

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

query 中用 substitute 替換 target 的出現。有關詳細資訊,請參閱 第 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 命令獲得的 targetsubstitute 來替換 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

構建一個短語查詢,搜尋 query1query2 在連續詞位上的匹配(與 <-> 運算子相同)。

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

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

構建一個短語查詢,搜尋 query1query2 相距正好 distance 個詞位的匹配。

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

tsvector_to_array ( tsvector ) → text[]

tsvector 轉換為詞位陣列。

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 展開為一組行,每行一個詞位。

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 中提取和標準化標記,並返回有關每個標記如何處理的資訊。有關詳細資訊,請參閱 第 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[]

如果輸入標記被字典識別,則返回一個包含替換詞位的陣列;如果標記被字典識別但它是停用詞,則返回一個空陣列;如果它不是一個已知的單詞,則返回 NULL。有關詳細資訊,請參閱 第 12.8.3 節

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

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

使用指定的解析器名稱從 document 中提取標記。有關詳細資訊,請參閱 第 12.8.2 節

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

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

使用 OID 指定的解析器從 document 中提取標記。有關詳細資訊,請參閱 第 12.8.2 節

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

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

返回一個描述指定解析器可以識別的每種標記型別的表。有關詳細資訊,請參閱 第 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 指定的解析器可以識別的每種標記型別的表。有關詳細資訊,請參閱 第 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 列),並返回有關資料中包含的每個不同詞位的統計資訊。有關詳細資訊,請參閱 第 12.4.4 節

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


提交更正

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