12.8. 測試與除錯

The behavior of a custom text search configuration can easily become confusing. The functions described in this section are useful for testing text search objects. You can test a complete configuration, or test parsers and dictionaries separately.

12.8.1. Configuration Testing

The functionts_debugallows easy testing of a text search configuration.

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_debugdisplays information about every token ofdocument_as produced by the parser and processed by the configured dictionaries. It uses the configuration specified byconfig_, ordefault_text_search_configif that argument is omitted.

ts_debugreturns one row for each token identified in the text by the parser. The columns returned are

  • aliastext— short name of the token type

  • descriptiontext— description of the token type

  • tokentext— text of the token

  • dictionariesregdictionary[]— the dictionaries selected by the configuration for this token type

  • dictionaryregdictionary— the dictionary that recognized the token, orNULLif none did

  • lexemestext[]— the lexeme(s) produced by the dictionary that recognized the token, orNULLif none did; an empty array ({}) means it was recognized as a stop word

Here is a simple example:

For a more extensive demonstration, we first create apublic.englishconfiguration and Ispell dictionary for the English language:

In this example, the wordBrightestwas recognized by the parser as anASCII word(aliasasciiword). For this token type the dictionary list isenglish_ispellandenglish_stem. The word was recognized byenglish_ispell, which reduced it to the nounbright. The wordsupernovaesis unknown to theenglish_ispelldictionary so it was passed to the next dictionary, and, fortunately, was recognized (in fact,english_stemis a Snowball dictionary which recognizes everything; that is why it was placed at the end of the dictionary list).

The wordThewas recognized by theenglish_ispelldictionary as a stop word (Section 12.6.1) and will not be indexed. The spaces are discarded too, since the configuration provides no dictionaries at all for them.

You can reduce the width of the output by explicitly specifying which columns you want to see:

12.8.2. Parser Testing

The following functions allow direct testing of a text search parser.

ts_parseparses the given_document_and returns a series of records, one for each token produced by parsing. Each record includes atokidshowing the assigned token type and atokenwhich is the text of the token. For example:

ts_token_typereturns a table which describes each type of token the specified parser can recognize. For each token type, the table gives the integertokidthat the parser uses to label a token of that type, thealiasthat names the token type in configuration commands, and a shortdescription. For example:

12.8.3. Dictionary Testing

Thets_lexizefunction facilitates dictionary testing.

ts_lexizereturns an array of lexemes if the input_token_is known to the dictionary, or an empty array if the token is known to the dictionary but it is a stop word, orNULLif it is an unknown word.

Examples:

Note

Thets_lexizefunction expects a single_token_, not text. Here is a case where this can be confusing:

The thesaurus dictionarythesaurus_astrodoes know the phrasesupernovae stars, butts_lexizefails since it does not parse the input text but treats it as a single token. Useplainto_tsqueryorto_tsvectorto test thesaurus dictionaries, for example:

Last updated

Was this helpful?