如何为我的自定义文本搜索配置正确创建同义词词典

How to correctly create thesaurus dictionary for my custom text search configuration

我使用 PostgreSQL 11.8。对于 Postgres,我使用 docker 图像 postgres:11-alpine。我想为基于某些词的表达式创建自定义全文搜索字典,例如 hello world 应该变成 hw.

首先我有一个自定义的全文搜索配置my_swedish:

CREATE TEXT SEARCH CONFIGURATION my_swedish (
   COPY = swedish
);

ALTER TEXT SEARCH CONFIGURATION my_swedish
   DROP MAPPING FOR hword_asciipart;
ALTER TEXT SEARCH CONFIGURATION my_swedish
   DROP MAPPING FOR hword_part;

对于这个配置,我想创建和使用字典。为此,我遵循 PostgreSQL 手册:

CREATE TEXT SEARCH DICTIONARY thesaurus_my_swedish (
    TEMPLATE = thesaurus,
    DictFile = thesaurus_my_swedish,
    Dictionary = pg_catalog.swedish_stem
);

我面临

ERROR:  could not open thesaurus file "/usr/local/share/postgresql/tsearch_data/thesaurus_my_swedish.ths": No such file or directory

然后我手动创建了文件:

touch /usr/local/share/postgresql/tsearch_data/thesaurus_astro.ths

然后:

ALTER TEXT SEARCH CONFIGURATION my_swedish
    ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
    WITH thesaurus_my_swedish;

 ERROR:  text search configuration "my_swedish" does not exist

当我把它改成默认的时候swedish

ALTER TEXT SEARCH CONFIGURATION swedish
    ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
    WITH thesaurus_my_swedish;

我收到错误:

ERROR:  text search dictionary "thesaurus_my_swedish" does not exist

如何为我的自定义测试搜索配置正确创建同义词词典?

更新 我在我的文件中添加了 thesaurus_my_swedish.ths 数据 hello world : hw 现在

SELECT to_tsvector('my_swedish', 'hello world');

returned 'hw':1 ,

但是其他单词呢?因为 to_tsvector('my_swedish', 'hello test') return 是空的,所以应该 return 像默认的瑞典语一样编辑

SELECT to_tsvector('swedish', 'hello test');
'hello':1 'test':2

怎么了?

更新

明白了,还需要加上pg_catalog.swedish_stem

ALTER TEXT SEARCH CONFIGURATION my_swedish
   ALTER MAPPING FOR asciihword, asciiword, hword, word
   WITH thesaurus_my_swedish, pg_catalog.swedish_stem;

除了少数例外,您做的一切都正确:

  • thesaurus_my_swedish.ths 不应为空,但应包含如下规则(取自您的示例):

    hello world : hw
    
  • 您应该为现在使用 swedish_stem 的所有令牌类型使用新字典,即

    ALTER TEXT SEARCH CONFIGURATION my_swedish
       ALTER MAPPING FOR asciihword, asciiword, hword, word
       WITH thesaurus_my_swedish, swedish_stem;
    

这个错误很神秘,不应该发生的:

ERROR:  text search configuration "my_swedish" does not exist

也许您连接到错误的数据库,或者您再次删除了配置,或者它不在 search_path 上并且您必须使用它的架构来限定它。在 psql 中使用 \dF *.* 列出所有现有配置。

当然,您必须先创建字典,然后才能在文本搜索配置中使用它。

请勿修改pg_catalog中的配置,此类修改在升级后会丢失。