pg_search

ParadeDB BM25算法全文检索插件,ES全文检索

概览

扩展包名版本分类许可证语言
pg_search0.22.6FTSAGPL-3.0Rust
ID扩展名BinLibLoadCreateTrustReloc模式
2100pg_searchparadedb
相关扩展pgroonga pgroonga_database pg_bestmatch vchord_bm25 pg_bigm zhparser pg_tokenizer pg_trgm

bm25 am conflicts with pg_textsearch; PG15-16 require shared_preload_libraries while PG17-18 do not.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.22.61817161514pg_search-
RPMPIGSTY0.22.61817161514pg_search_$v-
DEBPIGSTY0.22.61817161514postgresql-$v-pg-search-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64PIGSTY MISS
el10.aarch64PIGSTY MISS
d12.x86_64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7
d12.aarch64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7
d13.x86_64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
d13.aarch64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.5
u22.x86_64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7
u22.aarch64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7
u24.x86_64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7
u24.aarch64
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.22.6
PIGSTY 0.20.7

构建

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

pig build pkg pg_search         # 构建 DEB 包

安装

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

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

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

pig install pg_search;          # 当前活跃 PG 版本安装
pig ext install -y pg_search -v 18  # PG 18
pig ext install -y pg_search -v 17  # PG 17
pig ext install -y pg_search -v 16  # PG 16
pig ext install -y pg_search -v 15  # PG 15
dnf install -y pg_search_18       # PG 18
dnf install -y pg_search_17       # PG 17
dnf install -y pg_search_16       # PG 16
dnf install -y pg_search_15       # PG 15
apt install -y postgresql-18-pg-search   # PG 18
apt install -y postgresql-17-pg-search   # PG 17
apt install -y postgresql-16-pg-search   # PG 16
apt install -y postgresql-15-pg-search   # PG 15

预加载配置

shared_preload_libraries = 'pg_search';

创建扩展

CREATE EXTENSION pg_search;

用法

README | Quickstart | Install docs

pg_search 是 ParadeDB 为 PostgreSQL 提供的全文检索扩展。它基于 Tantivy,为堆表提供 BM25 索引和查询能力。当前上游文档说明,它支持 PostgreSQL 15 及以上版本。

入门

安装文档强调一个关键要求:必须把 pg_search 加入 shared_preload_libraries,这样后台工作进程才能处理索引写入。

shared_preload_libraries = 'pg_search'

随后启用扩展:

CREATE EXTENSION pg_search;
ALTER SYSTEM SET paradedb.pg_search_telemetry TO 'off';

创建 BM25 索引

Quickstart 展示了在堆表上创建 BM25 索引的方式,且需要唯一键字段:

CREATE INDEX search_idx ON mock_items
USING bm25 (id, description, category, rating, in_stock, created_at, metadata, weight_range)
WITH (key_field='id');

现有文档强调,key_field 必须唯一,BM25 索引是搜索查询的核心访问方法。

查询

@@@ 操作符用于执行搜索查询:

SELECT description, rating, category
FROM mock_items
WHERE description @@@ 'keyboard' AND rating > 2
ORDER BY rating
LIMIT 5;

ParadeDB 还提供用于相关性评分和摘要高亮的辅助函数:

SELECT description, paradedb.score(id)
FROM mock_items
WHERE description @@@ 'keyboard'
ORDER BY paradedb.score(id) DESC
LIMIT 5;

SELECT description, paradedb.snippet(description), paradedb.score(id)
FROM mock_items
WHERE description @@@ 'keyboard'
ORDER BY paradedb.score(id) DESC
LIMIT 5;

短语搜索支持用引号包裹表达式:

SELECT description
FROM mock_items
WHERE description @@@ '"metal keyboard"';

文本配置

Quickstart 还展示了如何为文本字段配置分词器,例如英文词干提取:

CREATE INDEX search_idx ON mock_items
USING bm25 (id, (description::pdb.simple('stemmer=english')), category)
WITH (key_field='id');

更深入的部署和运行说明请参考上游 ParadeDB 文档站,它是该项目的主文档入口。


最后修改 2026-04-14: update extension catalog (fa7cf58)