【问题标题】:How to correctly create thesaurus dictionary for my custom text search configuration如何为我的自定义文本搜索配置正确创建同义词词典
【发布时间】:2020-06-30 07:49:13
【问题描述】:

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

【问题讨论】:

    标签: postgresql full-text-search


    【解决方案1】:

    你做的一切都是对的,除了一些例外:

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

    【讨论】:

    • 你说得对,但想想,to_tsvector('my_swedish', 'hello test') 返回了空结果。我更新了问题
    • 我意识到我在ALTER TEXT SEARCH CONFIGURATION 声明中犯了一个错误;固定的。编辑 thesaurus_my_swedish.ths 后必须重新启动 PostgreSQL。顺便说一句,如果您更改文件,则必须重建所有索引并更新所有依赖映射的列。
    • 但我对och - Öronmuffar och vant setFuskpäls och shearling 等有很多表达方式
    • 我想添加thesaurus_my_swedish.ths这个数据hello och world : how,但在那之后,当我的触发器尝试准备ts向量时抛出异常-SQLSTATE[F0000]: Config file error:7 ERROR: thesaurus sample word "och" i s a stop word (rule 1) HINT: Use "?" to represent a stop word within a sample phrase. CONTEXT: PL/pgSQL function category_configurations_ts_trigger() line 3 at assignment因为/usr/local/share/postgresql/tsearch_data/swedish.stop包含och。单独的问题?
    猜你喜欢
    • 2023-01-10
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    相关资源
    最近更新 更多