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



我使用 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');

返回'hw':1

但是奥特尔的话呢?因为to_tsvector('my_swedish', 'hello test')返回空,所以应该像默认瑞典语一样返回

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中的配置,升级后此类修改将丢失。

最新更新