pg_search

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

概览

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

PG 17+ does not require dynamic loading

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.21.121817161514pg_search-
RPMPIGSTY0.21.121817161514pg_search_$v-
DEBPIGSTY0.21.121817161514postgresql-$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.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.7
d12.aarch64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.7
d13.x86_64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
d13.aarch64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.5
u22.x86_64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.7
u22.aarch64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.7
u24.x86_64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.20.7
u24.aarch64
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
PIGSTY 0.21.12
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

创建扩展

CREATE EXTENSION pg_search;

用法

https://docs.paradedb.com/documentation/getting-started/quickstart

CREATE EXTENSION pg_search;

ALTER SYSTEM SET paradedb.pg_search_telemetry TO 'off';

-- 创建 BM25 测试表
CALL paradedb.create_bm25_test_table(
  schema_name => 'public',
  table_name => 'mock_items'
);

SELECT description, rating, category FROM mock_items LIMIT 3;

-- 创建 BM25 索引(key_field 必须具有 UNIQUE 约束,每张表只能创建一个 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');

-- 使用 @@@ 操作符进行全文检索
SELECT description, rating, category
FROM mock_items
WHERE description @@@ 'keyboard' AND rating > 2
ORDER BY rating
LIMIT 5;

-- BM25 相关性评分
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, rating, category
FROM mock_items
WHERE description @@@ '"metal keyboard"';

-- 配置文本字段的分词器(例如英文词干提取)
DROP INDEX search_idx;
CREATE INDEX search_idx ON mock_items
USING bm25 (id, (description::pdb.simple('stemmer=english')), category)
WITH (key_field='id');

最后修改 2026-03-08: add extension catalog (baacba6)