在AWS RDS Postgres上,如何使用词典和非重音全文搜索?

Oli*_*r D 11 postgresql amazon-rds

我想在AWS RDS上使用PostgreSQL 9.5.4,利用全文搜索,带停用词的词典,非重音全文搜索.

上下文:

打开'unaccent'后,即使我没有正确输入重音,这个全文(Json)查询也会找到'F(e-acute)vrier'

psql>select * from proto_model.product where to_tsvector((body ->> 'description')) @@ to_tsquery('Fevrier');
Run Code Online (Sandbox Code Playgroud)

使用英语词典,同样搜索"the","any","you"...将找不到任何内容,因为它们是英语词典中定义的"停用词"并被忽略.

问题:

在我当地的Postgres上,这根本不是问题.在托管AWS上,这是一个.AWS上的EC2 + Docker当然不是问题,但我现在专注于RDS Postgres.

在本地,默认值default_text_search_config(得到它psql>show all)是'pg_catalog.english',它使用英语词典和停用词.在RDS上,这是'pg_catalog.simple'.

1)在AWS中,我无法添加字典或修改字典,因为您需要您没有的文件系统访问权限.创建/更新字典AFAIK没有编程解决方案.

2)在AWS中,作为'postgres'用户或甚至可以创建的' rds_superuser ' ,我无法改变全局配置

psql>ALTER SYSTEM SET default_text_search_config = 'pg_catalog.english';
ERROR:  must be superuser to execute ALTER SYSTEM command
Run Code Online (Sandbox Code Playgroud)

此外,没有可以与新Postgres实例关联的RDS Postgres参数组,并且您无法添加缺失值!向'rds_superuser'(psql>grant all on schema public to ...)授予更多权限并没有帮助.

3)在AWS中,作为'postgres'或'rds_superuser',我可以为我的会话设置当前文本配置

psql>set default_text_search_config = 'pg_catalog.english'; 
SET
Run Code Online (Sandbox Code Playgroud)

4)遗憾的是,在AWS中,作为'postgres'或'rds_superuser',我无法改变搜索配置(全局)以忽略重音.这在当地工作正常.

psql>ALTER TEXT SEARCH CONFIGURATION english ALTER MAPPING FOR hword, hword_part, word WITH unaccent, english_stem;
ERROR:  must be owner of text search configuration english
Run Code Online (Sandbox Code Playgroud)

5)在AWS中,作为'postgres'或'rds_superuser',我可以创建一个新的搜索配置(英语+ Unaccent),但即使在我的会话中也无法将其设置为默认值!

psql>CREATE TEXT SEARCH CONFIGURATION english2 (copy=english);
CREATE...
psql>ALTER TEXT SEARCH CONFIGURATION english2 ALTER MAPPING FOR hword, hword_part, word WITH unaccent, english_stem;
ALTER...
psql>set default_text_search_config = 'pg_catalog.english2';
ERROR:  invalid value for parameter "default_text_search_config": "pg_catalog.english2"
Run Code Online (Sandbox Code Playgroud)

所以看来我已经煮熟了.

我能看到的最好的方法是自动关联个人psql>set default_text_search_config = ...用户连接和一组配置选项

psql>alter role somerole set default_text_search_config = 'pg_catalog.english';
psql>select * from pg_user; (the option is present by default for all my connections under this role)
Run Code Online (Sandbox Code Playgroud)

除了从AWS RDS迁移到EC2 + Docker之外,您是否知道(4)或(5)为我提供字典+ unaccent的任何解决方案?

谢谢

奥利维尔

小智 11

您可以通过更改数据库中的角色参数来进行更改,例如:

ALTER ROLE [role] IN DATABASE [database]
    SET default_text_search_config TO 'pg_catalog.english';
Run Code Online (Sandbox Code Playgroud)


小智 4

不要依赖default_text_search_config。

相反,请像#5 中那样创建您自己的文本搜索配置。然后使用两个参数形式to_tsvector来指定自定义文本搜索配置而不是默认配置:

SELECT * from mytable where to_tsvector('myconfig', description) 
    @@ to_tsquery('cat & dog')
Run Code Online (Sandbox Code Playgroud)

双参数版本的另一个好处to_tsvector是,它允许您使用“表达式索引”来支持文本搜索,而不是表中单独的 tsvector 列:

CREATE_INDEX mytable_tsv_idx ON mytable USING GIN
    (to_tsvector('myconfig', description));
-- This query will use the index
SELECT * from mytable WHERE to_tsvector('myconfig', description) 
    @@ to_tsquery('cat & dog');
-- This query, despite setting the default config, 
-- will not use the expression index.
SET default_text_search_config = 'myconfig';
SELECT * from mytable WHERE to_tsvector(description) 
    @@ to_tsquery('cat & dog');
Run Code Online (Sandbox Code Playgroud)

https://www.postgresql.org/docs/9.5/static/textsearch-tables.html#TEXTSEARCH-TABLES-INDEX