pg_search

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

概览

扩展包名版本分类许可证语言
pg_search0.23.0FTSAGPL-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.23.01817161514pg_search-
RPMPIGSTY0.23.01817161514pg_search_$v-
DEBPIGSTY0.23.01817161514postgresql-$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;

用法

pg_search 是 ParadeDB 提供的、基于 BM25 的 PostgreSQL 搜索扩展。上游 README 说明支持从 PostgreSQL 15 开始,而 v0.23.0 的自托管安装文档仍要求在 CREATE EXTENSION 之前先 preload 该库。

启用并创建扩展

shared_preload_libraries = 'pg_search'
CREATE EXTENSION pg_search;

v0.23.0 的自托管扩展文档说明提供了面向 Postgres 15+ 的预编译二进制包。

创建 BM25 索引

quickstart 示例使用 bm25 access method,并要求指定唯一键字段:

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

v0.23.0 release 还提到,现在可以按字段调节 BM25 的 k1b 参数。

查询操作符与辅助函数

当前 quickstart 使用以下查询操作符:

  • |||:析取匹配,等价于 term1 OR term2
  • &&&:合取匹配,等价于 term1 AND term2

示例:

SELECT description, rating
FROM mock_items
WHERE description ||| 'running shoes'
ORDER BY rating
LIMIT 5;
SELECT description, pdb.score(id)
FROM mock_items
WHERE description &&& 'running shoes'
ORDER BY score DESC
LIMIT 5;
SELECT description, pdb.snippet(description), pdb.score(id)
FROM mock_items
WHERE description ||| 'running shoes'
ORDER BY score DESC
LIMIT 5;

说明

开发分支 README 已把安装和用法细节指向官方文档站,而不是在 README 中内联维护 SQL 细节。因此,对当前 pg_search 语法而言,quickstart 才是最权威的用法来源;它反映的是 0.20 之后的 API,而不是一些次级资料里仍能看到的旧 @@@ 示例。


最后修改 2026-04-19: update extension stub docs (aa5941a)