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

12.8. 文字搜尋測試和除錯 #

自定義文字搜尋配置的行為很容易變得令人困惑。本節介紹的函式對於測試文字搜尋物件很有用。您可以測試一個完整的配置,也可以單獨測試解析器和詞典。

12.8.1. 配置測試 #

函式 ts_debug 允許輕鬆測試文字搜尋配置。

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

ts_debug 顯示由解析器生成並由配置的詞典處理的 document 的每個詞元的資訊。它使用 config 指定的配置,或者如果省略該引數,則使用 default_text_search_config

ts_debug 為解析器在文字中標識的每個詞元返回一行。返回的列是

  • alias text — 詞元型別的簡稱

  • description text — 詞元型別的描述

  • token text — 詞元的文字

  • dictionaries regdictionary[] — 配置為該詞元型別選擇的詞典

  • dictionary regdictionary — 識別該詞元的詞典,如果沒有任何詞典識別則為 NULL

  • lexemes text[] — 識別該詞元的詞典生成的詞位(lexeme)列表,如果沒有任何詞典識別則為 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_ispellenglish_stem。該單詞被 english_ispell 識別,並將其縮減為名詞 bright。單詞 supernovaesenglish_ispell 詞典未知的,因此被傳遞給下一個詞典,並且幸運的是,它被識別了(事實上,english_stem 是一個 Snowball 詞典,它能識別所有詞;這就是它被放在詞典列表末尾的原因)。

單詞 Theenglish_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}

12.8.2. 解析器測試 #

以下函式允許直接測試文字搜尋解析器。

ts_parse(parser_name text, document text,
         OUT tokid integer, OUT token text) returns setof record
ts_parse(parser_oid oid, document text,
         OUT tokid integer, OUT token text) returns setof record

ts_parse 解析給定的 document,並返回一系列記錄,每條記錄代表解析產生的每個詞元。每條記錄都包含一個 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, OUT tokid integer,
              OUT alias text, OUT description text) returns setof record
ts_token_type(parser_oid oid, OUT tokid integer,
              OUT alias text, OUT description text) returns setof 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

12.8.3. 詞典測試 #

函式 ts_lexize 方便了詞典測試。

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

ts_lexize 如果輸入 token 是詞典已知的,則返回一個詞位陣列;如果詞典已知該詞元但它是一個停用詞,則返回一個空陣列;如果它是一個未知詞,則返回 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_tsqueryto_tsvector 來測試同義詞詞典,例如:

SELECT plainto_tsquery('supernovae stars');
 plainto_tsquery
-----------------
 'sn'

提交更正

如果您在文件中看到任何不正確的內容、與您對特定功能的體驗不符的內容,或者需要進一步說明的內容,請使用 此表格 報告文件問題。