自訂文字搜尋配置的行為很容易變得令人困惑。本節中描述的函數對於測試文字搜尋物件很有用。您可以測試完整的配置,或分別測試剖析器和字典。
函數 ts_debug
允許輕鬆測試文字搜尋配置。
ts_debug([config
regconfig
, ]document
text
, OUTalias
text
, OUTdescription
text
, OUTtoken
text
, OUTdictionaries
regdictionary[]
, OUTdictionary
regdictionary
, OUTlexemes
text[]
) returns setof record
ts_debug
顯示關於每個 文件
標記的資訊,這些標記由剖析器產生,並由配置的字典處理。它使用由 配置
指定的配置,如果省略該引數,則使用 default_text_search_config
。
ts_debug
為剖析器在文字中識別的每個標記傳回一個資料列。傳回的欄位如下
別名
text
— 標記類型的簡短名稱
描述
text
— 標記類型的描述
標記
text
— 標記的文字
字典
regdictionary[]
— 配置為此標記類型選取的字典
字典
regdictionary
— 識別標記的字典,如果沒有,則為 NULL
詞元
text[]
— 識別標記的字典產生的詞元,如果沒有,則為 NULL
;空陣列 ({}
) 表示它被識別為停用詞
這是一個簡單的例子
SELECT * FROM ts_debug('english', 'a fat cat sat on a mat - it ate a fat rats'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------+----------------+--------------+--------- asciiword | Word, all ASCII | a | {english_stem} | english_stem | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat} blank | Space symbols | | {} | | asciiword | Word, all ASCII | cat | {english_stem} | english_stem | {cat} blank | Space symbols | | {} | | asciiword | Word, all ASCII | sat | {english_stem} | english_stem | {sat} blank | Space symbols | | {} | | asciiword | Word, all ASCII | on | {english_stem} | english_stem | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | a | {english_stem} | english_stem | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | mat | {english_stem} | english_stem | {mat} blank | Space symbols | | {} | | blank | Space symbols | - | {} | | asciiword | Word, all ASCII | it | {english_stem} | english_stem | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | ate | {english_stem} | english_stem | {ate} blank | Space symbols | | {} | | asciiword | Word, all ASCII | a | {english_stem} | english_stem | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat} blank | Space symbols | | {} | | asciiword | Word, all ASCII | rats | {english_stem} | english_stem | {rat}
為了進行更廣泛的示範,我們首先為英語建立一個 public.english
配置和 Ispell 字典
CREATE TEXT SEARCH CONFIGURATION public.english ( COPY = pg_catalog.english ); CREATE TEXT SEARCH DICTIONARY english_ispell ( TEMPLATE = ispell, DictFile = english, AffFile = english, StopWords = english ); ALTER TEXT SEARCH CONFIGURATION public.english ALTER MAPPING FOR asciiword WITH english_ispell, english_stem;
SELECT * FROM ts_debug('public.english', 'The Brightest supernovaes'); alias | description | token | dictionaries | dictionary | lexemes -----------+-----------------+-------------+-------------------------------+----------------+------------- asciiword | Word, all ASCII | The | {english_ispell,english_stem} | english_ispell | {} blank | Space symbols | | {} | | asciiword | Word, all ASCII | Brightest | {english_ispell,english_stem} | english_ispell | {bright} blank | Space symbols | | {} | | asciiword | Word, all ASCII | supernovaes | {english_ispell,english_stem} | english_stem | {supernova}
在本範例中,單字 Brightest
被剖析器識別為 ASCII word
(別名 asciiword
)。對於這種標記類型,字典清單為 english_ispell
和 english_stem
。english_ispell
識別了這個單字,並將其簡化為名詞 bright
。english_ispell
字典無法識別單字 supernovaes
,因此它被傳遞到下一個字典,幸運的是,它被識別了(事實上,english_stem
是一個 Snowball 字典,它可以識別所有內容;這就是它被放置在字典清單末尾的原因)。
單字 The
被 english_ispell
字典識別為停用詞 (第 12.6.1 節),並且不會被索引。空格也被丟棄,因為配置沒有為它們提供任何字典。
您可以透過明確指定您想看到的欄位來減少輸出的寬度
SELECT alias, token, dictionary, lexemes FROM ts_debug('public.english', 'The Brightest supernovaes'); alias | token | dictionary | lexemes -----------+-------------+----------------+------------- asciiword | The | english_ispell | {} blank | | | asciiword | Brightest | english_ispell | {bright} blank | | | asciiword | supernovaes | english_stem | {supernova}
以下函數允許直接測試文字搜尋剖析器。
ts_parse(parser_name
text
,document
text
, OUTtokid
integer
, OUTtoken
text
) returnssetof record
ts_parse(parser_oid
oid
,document
text
, OUTtokid
integer
, OUTtoken
text
) returnssetof record
ts_parse
剖析給定的 文件
,並傳回一系列記錄,每個記錄對應於剖析產生的標記。每個記錄都包含一個 tokid
,顯示分配的標記類型,以及一個 token
,它是標記的文字。例如
SELECT * FROM ts_parse('default', '123 - a number'); tokid | token -------+-------- 22 | 123 12 | 12 | - 1 | a 12 | 1 | number
ts_token_type(parser_name
text
, OUTtokid
integer
, OUTalias
text
, OUTdescription
text
) returnssetof record
ts_token_type(parser_oid
oid
, OUTtokid
integer
, OUTalias
text
, OUTdescription
text
) returnssetof record
ts_token_type
傳回一個表格,描述指定的剖析器可以識別的每種標記類型。對於每種類型的標記,表格會提供整數 tokid
(剖析器使用它來標記該類型的標記)、alias
(在配置指令中命名標記類型)以及簡短的 description
。例如
SELECT * FROM ts_token_type('default'); tokid | alias | description -------+-----------------+------------------------------------------ 1 | asciiword | Word, all ASCII 2 | word | Word, all letters 3 | numword | Word, letters and digits 4 | email | Email address 5 | url | URL 6 | host | Host 7 | sfloat | Scientific notation 8 | version | Version number 9 | hword_numpart | Hyphenated word part, letters and digits 10 | hword_part | Hyphenated word part, all letters 11 | hword_asciipart | Hyphenated word part, all ASCII 12 | blank | Space symbols 13 | tag | XML tag 14 | protocol | Protocol head 15 | numhword | Hyphenated word, letters and digits 16 | asciihword | Hyphenated word, all ASCII 17 | hword | Hyphenated word, all letters 18 | url_path | URL path 19 | file | File or path name 20 | float | Decimal notation 21 | int | Signed integer 22 | uint | Unsigned integer 23 | entity | XML entity
ts_lexize
函數有助於字典測試。
ts_lexize(dict
regdictionary
,token
text
) returnstext[]
如果字典已知輸入 標記
,ts_lexize
會傳回詞元陣列;如果字典已知該標記但它是停用詞,則傳回空陣列;如果是未知單字,則傳回 NULL
。
範例
SELECT ts_lexize('english_stem', 'stars'); ts_lexize ----------- {star} SELECT ts_lexize('english_stem', 'a'); ts_lexize ----------- {}
ts_lexize
函數預期單個 標記,而不是文字。以下是一個可能令人困惑的情況
SELECT ts_lexize('thesaurus_astro', 'supernovae stars') is null; ?column? ---------- t
同義詞詞典 thesaurus_astro
知道片語 supernovae stars
,但 ts_lexize
失敗,因為它不剖析輸入文字,而是將其視為單個標記。使用 plainto_tsquery
或 to_tsvector
測試同義詞詞典,例如
SELECT plainto_tsquery('supernovae stars'); plainto_tsquery ----------------- 'sn'
如果您在文件中發現任何不正確、與您使用特定功能的經驗不符或需要進一步澄清的地方,請使用此表單報告文件問題。