zhparser

中文分词,全文搜索解析器

概览

扩展包名版本分类许可证语言
zhparser2.3FTSPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
2130zhparser-
相关扩展pg_trgm rum pg_search pgroonga pgroonga_database pg_bigm pg_tokenizer vchord_bm25

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY2.31817161514zhparser-
RPMPIGSTY2.31817161514zhparser_$v-
DEBPIGSTY2.31817161514postgresql-$v-zhparser-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
d13.x86_64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
d13.aarch64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
u22.x86_64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
u22.aarch64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
u24.x86_64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
u24.aarch64
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3
PIGSTY 2.3

构建

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

pig build pkg zhparser         # 构建 RPM / DEB 包

安装

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

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

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

pig install zhparser;          # 当前活跃 PG 版本安装
pig ext install -y zhparser -v 18  # PG 18
pig ext install -y zhparser -v 17  # PG 17
pig ext install -y zhparser -v 16  # PG 16
pig ext install -y zhparser -v 15  # PG 15
pig ext install -y zhparser -v 14  # PG 14
dnf install -y zhparser_18       # PG 18
dnf install -y zhparser_17       # PG 17
dnf install -y zhparser_16       # PG 16
dnf install -y zhparser_15       # PG 15
dnf install -y zhparser_14       # PG 14
apt install -y postgresql-18-zhparser   # PG 18
apt install -y postgresql-17-zhparser   # PG 17
apt install -y postgresql-16-zhparser   # PG 16
apt install -y postgresql-15-zhparser   # PG 15
apt install -y postgresql-14-zhparser   # PG 14

创建扩展

CREATE EXTENSION zhparser;

用法

GitHub: amutu/zhparser

zhparser 是基于 SCWS(Simple Chinese Word Segmentation)分词库的 PostgreSQL 中文全文搜索扩展。

功能特性

  • 为 PostgreSQL 全文搜索提供中文分词
  • 基于 SCWS(简易中文分词)库
  • 支持自定义词典(TXT 和 XDB 格式)
  • 数据库级自定义词表(v2.1 起)
  • 多个可调参数控制分词行为

快速开始

-- 创建扩展
CREATE EXTENSION zhparser;

-- 创建使用 zhparser 的文本搜索配置
CREATE TEXT SEARCH CONFIGURATION chinese (PARSER = zhparser);

-- 添加词类映射
ALTER TEXT SEARCH CONFIGURATION chinese ADD MAPPING FOR n,v,a,i,e,l WITH simple;

-- 测试中文分词
SELECT to_tsvector('chinese', '小明硕士毕业于中国科学院计算所,后在日本京都大学深造');

-- 创建表和中文全文搜索索引
CREATE TABLE articles (id serial PRIMARY KEY, title text, body text);

CREATE INDEX articles_body_idx ON articles
  USING gin (to_tsvector('chinese', body));

-- 中文全文搜索查询
SELECT * FROM articles
  WHERE to_tsvector('chinese', body) @@ to_tsquery('chinese', '中国');

配置参数

zhparser 提供多个 GUC 参数控制分词行为:

参数默认值说明
zhparser.punctuation_ignoreoff忽略所有标点符号
zhparser.seg_with_dualityoff对长词进行二元分词
zhparser.dict_in_memoryoff将整个词典加载到内存
zhparser.multi_shortoff短词复合分词
zhparser.multi_dualityoff二元复合分词
zhparser.multi_zmainoff首次复合分词中的关键词
zhparser.multi_zalloff使用所有复合分词

词类

zhparser 支持 SCWS 的以下词类:

代码说明
a形容词
b区别词
c连词
d副词
e叹词
f方位词
g词根
h前缀
i成语
j简称
k后缀
l临时习语
m数词
n名词
o拟声词
p介词
q量词
r代词
s处所词
t时间词
u助词
v动词
w标点符号
x未知
y语气词
z状态词

自定义词典

基于文件的词典

将自定义词典文件放在共享目录(通常为 $SHAREDIR/tsearch_data/):

  • TXT 格式:每行一个词
  • XDB 格式:编译后的 SCWS 词典格式

自定义词典优先于内置词典。

数据库级自定义词(v2.1+)

-- 通过 zhparser 内置表添加自定义词
INSERT INTO zhparser.zhprs_custom_word VALUES ('中国科学院计算所');

-- 重新加载自定义词典(同步后需重新连接才能生效)
SELECT sync_zhprs_custom_word();

-- 验证带自定义词的分词结果
SELECT to_tsvector('chinese', '小明硕士毕业于中国科学院计算所');

Docker 快速启动

docker run --name pgzhparser -d \
  -e POSTGRES_PASSWORD=somepassword \
  zhparser/zhparser:bookworm-16

最后修改 2026-03-14: update extension metadata (953cbd0)