pg_textsearch
带有BM25排序的全文搜索扩展
仓库
timescale/pg_textsearch
https://github.com/timescale/pg_textsearch
源码
pg_textsearch-0.5.0.tar.gz
pg_textsearch-0.5.0.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_textsearch | 0.5.0 | FTS | PostgreSQL | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 2180 | pg_textsearch | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 相关扩展 | pg_search pgroonga pg_bigm zhparser pg_trgm rum biscuit fuzzystrmatch |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.5.0 | 1817161514 | pg_textsearch | - |
| RPM | PIGSTY | 0.5.0 | 1817161514 | pg_textsearch_$v | - |
| DEB | PIGSTY | 0.5.0 | 1817161514 | postgresql-$v-textsearch | - |
| OS / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 0.5.0 el8.x86_64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el8.x86_64.rpm
| PIGSTY 0.5.0 el8.x86_64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el8.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el8.aarch64 | PIGSTY 0.5.0 el8.aarch64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el8.aarch64.rpm
| PIGSTY 0.5.0 el8.aarch64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el8.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el9.x86_64 | PIGSTY 0.5.0 el9.x86_64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el9.x86_64.rpm
| PIGSTY 0.5.0 el9.x86_64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el9.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el9.aarch64 | PIGSTY 0.5.0 el9.aarch64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el9.aarch64.rpm
| PIGSTY 0.5.0 el9.aarch64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el9.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el10.x86_64 | PIGSTY 0.5.0 el10.x86_64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el10.x86_64.rpm
| PIGSTY 0.5.0 el10.x86_64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el10.x86_64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| el10.aarch64 | PIGSTY 0.5.0 el10.aarch64.pg18 : pg_textsearch_18 pg_textsearch_18-0.5.0-1PIGSTY.el10.aarch64.rpm
| PIGSTY 0.5.0 el10.aarch64.pg17 : pg_textsearch_17 pg_textsearch_17-0.5.0-1PIGSTY.el10.aarch64.rpm
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d12.x86_64 | PIGSTY 0.5.0 d12.x86_64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~bookworm_amd64.deb
| PIGSTY 0.5.0 d12.x86_64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~bookworm_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d12.aarch64 | PIGSTY 0.5.0 d12.aarch64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~bookworm_arm64.deb
| PIGSTY 0.5.0 d12.aarch64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~bookworm_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d13.x86_64 | PIGSTY 0.5.0 d13.x86_64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~trixie_amd64.deb
| PIGSTY 0.5.0 d13.x86_64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~trixie_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| d13.aarch64 | PIGSTY 0.5.0 d13.aarch64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~trixie_arm64.deb
| PIGSTY 0.5.0 d13.aarch64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~trixie_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u22.x86_64 | PIGSTY 0.5.0 u22.x86_64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~jammy_amd64.deb
| PIGSTY 0.5.0 u22.x86_64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~jammy_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u22.aarch64 | PIGSTY 0.5.0 u22.aarch64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~jammy_arm64.deb
| PIGSTY 0.5.0 u22.aarch64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~jammy_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u24.x86_64 | PIGSTY 0.5.0 u24.x86_64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~noble_amd64.deb
| PIGSTY 0.5.0 u24.x86_64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~noble_amd64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
| u24.aarch64 | PIGSTY 0.5.0 u24.aarch64.pg18 : postgresql-18-textsearch postgresql-18-textsearch_0.5.0-1PIGSTY~noble_arm64.deb
| PIGSTY 0.5.0 u24.aarch64.pg17 : postgresql-17-textsearch postgresql-17-textsearch_0.5.0-1PIGSTY~noble_arm64.deb
| PIGSTY MISS | PIGSTY MISS | PIGSTY MISS |
构建
您可以使用 pig build 命令构建 pg_textsearch 扩展的 RPM / DEB 包:
pig build pkg pg_textsearch # 构建 RPM / DEB 包
安装
您可以直接安装 pg_textsearch 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_textsearch; # 当前活跃 PG 版本安装
pig ext install -y pg_textsearch -v 18 # PG 18
pig ext install -y pg_textsearch -v 17 # PG 17
dnf install -y pg_textsearch_18 # PG 18
dnf install -y pg_textsearch_17 # PG 17
apt install -y postgresql-18-textsearch # PG 18
apt install -y postgresql-17-textsearch # PG 17
创建扩展:
CREATE EXTENSION pg_textsearch;
用法
使用 BM25 评分和 Block-Max WAND 优化的现代排序文本搜索。语法简单,支持快速 top-k 查询、并行索引构建和分区表。
添加到 shared_preload_libraries:
shared_preload_libraries = 'pg_textsearch'
CREATE EXTENSION pg_textsearch;
快速开始
CREATE TABLE documents (id bigserial PRIMARY KEY, content text);
INSERT INTO documents (content) VALUES
('PostgreSQL is a powerful database system'),
('BM25 is an effective ranking function'),
('Full text search with custom scoring');
-- 创建 BM25 索引
CREATE INDEX docs_idx ON documents USING bm25(content) WITH (text_config='english');
-- 使用 <@> 运算符查询(返回负 BM25 分数,越低匹配越好)
SELECT * FROM documents
ORDER BY content <@> 'database system'
LIMIT 5;
查询
-- 从列自动检测索引
SELECT * FROM documents
ORDER BY content <@> 'database system'
LIMIT 5;
-- 显式指定索引
SELECT * FROM documents
WHERE content <@> to_bm25query('database system', 'docs_idx') < -1.0;
过滤
前置过滤在评分前缩减行数(适合选择性强的过滤器):
CREATE INDEX ON documents (category_id);
SELECT * FROM documents
WHERE category_id = 123
ORDER BY content <@> 'search terms'
LIMIT 10;
后置过滤先应用 BM25 扫描,再过滤:
SELECT * FROM documents
WHERE content <@> to_bm25query('search terms', 'docs_idx') < -5.0
ORDER BY content <@> 'search terms'
LIMIT 10;
索引选项
| 选项 | 默认值 | 说明 |
|---|---|---|
text_config | (必需) | PostgreSQL 文本搜索配置 |
k1 | 1.2 | 词频饱和参数 |
b | 0.75 | 长度归一化参数 |
CREATE INDEX ON documents USING bm25(content)
WITH (text_config='english', k1=1.5, b=0.8);
-- 语言特定配置
CREATE INDEX ON french_docs USING bm25(content) WITH (text_config='french');
CREATE INDEX ON german_docs USING bm25(content) WITH (text_config='german');
数据类型
bm25query — 表示 BM25 评分查询:
SELECT to_bm25query('search query text', 'docs_idx');
-- docs_idx:search query text