跳转到主要内容

pg_pinyin

PostgreSQL 拼音转写与检索辅助扩展

概览

扩展包名版本分类许可证语言
pg_pinyin0.0.8FTSMITRust
ID扩展名BinLibLoadCreateTrustReloc模式
2200pg_pinyin否是否是否是pinyin

optional tokenizer-input overload can integrate with pg_search.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.0.81817161514pg_pinyin-
RPMPIGSTY0.0.81817161514pg_pinyin_$v-
DEBPIGSTY0.0.81817161514postgresql-$v-pinyin-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
d13.x86_64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
d13.aarch64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
u22.x86_64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
u22.aarch64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
u24.x86_64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
u24.aarch64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
u26.x86_64
u26.aarch64
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8
PIGSTY 0.0.8

构建

您可以使用 pig build 命令构建 pg_pinyin 扩展的 RPM / DEB 包:

pig build pkg pg_pinyin         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_pinyin 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

安装
pig install pg_pinyin;          # 当前活跃 PG 版本安装
pig
pig ext install -y pg_pinyin -v 18  # PG 18
pig ext install -y pg_pinyin -v 17  # PG 17
pig ext install -y pg_pinyin -v 16  # PG 16
pig ext install -y pg_pinyin -v 15  # PG 15
pig ext install -y pg_pinyin -v 14  # PG 14
dnf
dnf install -y pg_pinyin_18       # PG 18
dnf install -y pg_pinyin_17       # PG 17
dnf install -y pg_pinyin_16       # PG 16
dnf install -y pg_pinyin_15       # PG 15
dnf install -y pg_pinyin_14       # PG 14
apt
apt install -y postgresql-18-pinyin   # PG 18
apt install -y postgresql-17-pinyin   # PG 17
apt install -y postgresql-16-pinyin   # PG 16
apt install -y postgresql-15-pinyin   # PG 15
apt install -y postgresql-14-pinyin   # PG 14

创建扩展:

CREATE EXTENSION pg_pinyin;

用法

来源:

pg_pinyin 对中文文本进行拉丁化,并提供分词器和查询辅助工具,适用于搜索应用。使用 pg_pinyin 可以创建稳定的拼音搜索键、分汉字文本或扩展拼音输入为 pg_search 正则表达式查询。

0.0.8 修复了并行拼音转换、分词器转换及后缀词典查询中的只读 SPI 调用。该版本使用 pgrx 0.19.3,支持 PostgreSQL 14-18 和 PostgreSQL 19 beta4。随版本提供的 0.0.7 到 0.0.8 迁移脚本不修改 SQL 对象,运行时修复位于共享库中。安装新版本扩展文件后,执行 ALTER EXTENSION pg_pinyin UPDATE TO ‘0.0.8’。Pigsty 包元数据单独维护。

创建扩展

CREATE EXTENSION pg_pinyin;

该扩展是可重定位的,并不需要 shared_preload_libraries 或服务器重启。

拉丁化文本

逐字符拉丁化或使用词感知分割:

SELECT pinyin_char_romanize('重庆');
SELECT pinyin_word_romanize('重庆火锅');
SELECT pinyin_word_romanize('重庆火锅', '_custom');

可选的 suffix 参数用于选择自定义词典表,不是输出分隔符。例如,_custom 对应 pinyin.pinyin_mapping_custom 和 pinyin.pinyin_words_custom,其中的条目优先于内置词典。词模式使用词典分词处理上下文读音。修改这些表后,调用 public.pinyin_clear_suffix_cache(’_custom’) 清理后缀词典缓存。

使用 pg_search 分词器输入

当安装了 pg_search 时,词拉丁化也接受 pg_search 分词器的结果:

SELECT pinyin_word_romanize(
  description::pdb.icu::text[]
)
FROM documents;

该重载返回拉丁化文本;它不暴露每分词一行的 API。在不需要 pg_search 分词的情况下使用纯文本重载。

构建一个 pg_search 查询

如果在安装 pg_pinyin 之前已安装了 pg_search,pg_pinyin 提供了一个类型化的重载,返回 pdb.query:

SELECT *
FROM documents
WHERE id @@@ pinyin_regex_phrase(
  'chong qing',
  slope => 1,
  max_expansions => 64,
  generated_pinyin => true
);

如果未安装 pg_search,则相同的入口点将作为错误报告的占位符而不是静默返回不同类型的值。在升级后按预期顺序安装依赖项并测试函数签名。

对象索引

  • pinyin_char_romanize(text [, suffix]) 返回基于字符的拼音文本。
  • pinyin_word_romanize(text [, suffix]) 返回词典分割的拼音文本。
  • pinyin_word_romanize(tokenizer_input [, suffix]) 接受一个 pg_search 分词器结果。
  • pinyin_regex_phrase(text, slope, max_expansions, generated_pinyin) 当该集成可用时,构建一个 pg_search 拼音短语查询。
  • pinyin_regex_phrase_patterns 是一个内部模式构建辅助函数;请优先使用公共查询函数。

运行注意事项

扩展在其 pinyin 模式中自带生成的字符和词字典。将这些表视为由扩展管理的数据,而不是应用程序表。拉丁化是规范化,不是翻译,并且可能需要应用端审查以处理模糊或领域特定的读音。

这个页面对您有帮助吗?