首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为我的自定义文本搜索配置正确创建辞典

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

Stack Overflow用户
提问于 2020-06-30 07:49:13
回答 1查看 1.4K关注 0票数 0

我使用PostgreSQL 11.8。对于Postgres,我使用了码头图像postgres:11-alpine。我想创建一个自定义全文搜索字典,查找基于一些单词的表达式,比如hello world应该变成hw

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

代码语言:javascript
复制
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手册:

代码语言:javascript
复制
CREATE TEXT SEARCH DICTIONARY thesaurus_my_swedish (
    TEMPLATE = thesaurus,
    DictFile = thesaurus_my_swedish,
    Dictionary = pg_catalog.swedish_stem
);

我面临着

代码语言:javascript
复制
ERROR:  could not open thesaurus file "/usr/local/share/postgresql/tsearch_data/thesaurus_my_swedish.ths": No such file or directory

然后我手动创建了该文件:

代码语言:javascript
复制
touch /usr/local/share/postgresql/tsearch_data/thesaurus_astro.ths

然后:

代码语言:javascript
复制
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

代码语言:javascript
复制
ALTER TEXT SEARCH CONFIGURATION swedish
    ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
    WITH thesaurus_my_swedish;

我发现了一个错误:

代码语言:javascript
复制
ERROR:  text search dictionary "thesaurus_my_swedish" does not exist

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

更新我在文件thesaurus_my_swedish.ths data hello world : hw中添加了

代码语言:javascript
复制
SELECT to_tsvector('my_swedish', 'hello world');

'hw':1回来了,

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

代码语言:javascript
复制
SELECT to_tsvector('swedish', 'hello test');
'hello':1 'test':2

怎么了?

更新

我明白,也需要添加pg_catalog.swedish_stem

代码语言:javascript
复制
ALTER TEXT SEARCH CONFIGURATION my_swedish
   ALTER MAPPING FOR asciihword, asciiword, hword, word
   WITH thesaurus_my_swedish, pg_catalog.swedish_stem;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-30 08:29:25

你把一切都做对了,只有几个例外:

  • thesaurus_my_swedish.ths不应该是空的,而是包含这样的规则(取自您的示例):

hello : hw

  • 您应该对现在使用swedish_stem的所有令牌类型使用新的字典,即

更改文本搜索配置my_swedish用于asciihword,asciiword,hword,word与thesaurus_my_swedish,swedish_stem;的更改映射

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

代码语言:javascript
复制
ERROR:  text search configuration "my_swedish" does not exist

您可能连接到了错误的数据库,或者再次删除了配置,或者它不在search_path上,您必须用它的模式对其进行限定。使用psql中的psql列出所有现有配置。

当然,在文本搜索配置中使用字典之前,您必须创建字典。

不要修改pg_catalog中的配置,这种修改将在升级后丢失。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62652659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档