Postgresql 中文操作指南

F.14. dict_xsyn — example synonym full-text search dictionary #

dict_xsyn(扩展同义词词典)是一个用于全文搜索的附加词典模板的示例。此词典类型用词组的同义词代替词,因此可以使用任何同义词来搜索一个词。

dict_xsyn (Extended Synonym Dictionary) is an example of an add-on dictionary template for full-text search. This dictionary type replaces words with groups of their synonyms, and so makes it possible to search for a word using any of its synonyms.

F.14.1. Configuration #

一个 dict_xsyn 词典接受以下选项:

A dict_xsyn dictionary accepts the following options:

规则文件的格式如下:

The rules file has the following format:

查看安装在 $SHAREDIR/tsearch_data/ 中的 xsyn_sample.rules,这是一个示例。

Look at xsyn_sample.rules, which is installed in $SHAREDIR/tsearch_data/, for an example.

F.14.2. Usage #

安装 dict_xsyn 扩展会基于它创建一个文本搜索模板 xsyn_template 和一个词典 xsyn,并带有默认参数。您可以更改参数,例如

Installing the dict_xsyn extension creates a text search template xsyn_template and a dictionary xsyn based on it, with default parameters. You can alter the parameters, for example

mydb# ALTER TEXT SEARCH DICTIONARY xsyn (RULES='my_rules', KEEPORIG=false);
ALTER TEXT SEARCH DICTIONARY

或基于模板创建新的词典。

or create new dictionaries based on the template.

要测试词典,您可以尝试

To test the dictionary, you can try

mydb=# SELECT ts_lexize('xsyn', 'word');
      ts_lexize
-----------------------
 {syn1,syn2,syn3}

mydb# ALTER TEXT SEARCH DICTIONARY xsyn (RULES='my_rules', KEEPORIG=true);
ALTER TEXT SEARCH DICTIONARY

mydb=# SELECT ts_lexize('xsyn', 'word');
      ts_lexize
-----------------------
 {word,syn1,syn2,syn3}

mydb# ALTER TEXT SEARCH DICTIONARY xsyn (RULES='my_rules', KEEPORIG=false, MATCHSYNONYMS=true);
ALTER TEXT SEARCH DICTIONARY

mydb=# SELECT ts_lexize('xsyn', 'syn1');
      ts_lexize
-----------------------
 {syn1,syn2,syn3}

mydb# ALTER TEXT SEARCH DICTIONARY xsyn (RULES='my_rules', KEEPORIG=true, MATCHORIG=false, KEEPSYNONYMS=false);
ALTER TEXT SEARCH DICTIONARY

mydb=# SELECT ts_lexize('xsyn', 'syn1');
      ts_lexize
-----------------------
 {word}

实际使用中将包括在文本搜索配置中包括该选项,如 Chapter 12 所述。可能如下所示:

Real-world usage will involve including it in a text search configuration as described in Chapter 12. That might look like this:

ALTER TEXT SEARCH CONFIGURATION english
    ALTER MAPPING FOR word, asciiword WITH xsyn, english_stem;