文章摘要
pg_textsearch是PostgreSQL的开源扩展,提供基于BM25算法的相关性排序全文搜索功能。该项目由Timescale开发,采用开源许可。
文章总结
PostgreSQL 全文搜索扩展 pg_textsearch
项目概述
pg_textsearch 是 PostgreSQL 的一个开源扩展,提供基于 BM25 算法的相关性排序全文搜索功能。该项目由 Timescale 公司开发,采用 PostgreSQL 开源许可证。
核心功能
- BM25 相关性排序:实现现代信息检索标准算法
- 简单查询语法:使用
ORDER BY content <@> '搜索词'进行排序 - 支持多种语言:可与 PostgreSQL 文本搜索配置(english, french等)配合使用
- 高性能查询:通过 Block-Max WAND 优化实现快速 top-k 查询
- 并行索引构建:支持大型表的并行索引构建
- 分区表支持:兼容 PostgreSQL 分区表
技术细节
兼容性
支持 PostgreSQL 17 和 18 版本
安装方式
- 预编译二进制包:可从 Releases 页面下载
- 源码编译:
bash git clone https://github.com/timescale/pg_textsearch cd pg_textsearch make && sudo make install
基本使用
- 修改 postgresql.conf 添加:
shared_preload_libraries = 'pg_textsearch' - 创建扩展:
sql CREATE EXTENSION pg_textsearch; - 创建索引:
sql CREATE INDEX docs_idx ON documents USING bm25(content) WITH (text_config='english');
性能特点
- 内存表架构:采用 memtable 设计实现高效写入
- 并行构建:支持通过
max_parallel_maintenance_workers配置并行度 - 性能优化:
- 强制合并段:
SELECT bm25_force_merge('docs_idx') - 结合 LIMIT 使用 ORDER BY 以启用 Block-Max WAND 优化
- 默认启用段压缩
- 强制合并段:
项目状态
当前版本为 v1.0.0,已可用于生产环境。项目路线图(Roadmap)中列出了未来计划的功能。
开发背景
项目原名 Tapir(Textual Analysis for Postgres Information Retrieval),现仍以貘(tapir)作为吉祥物,源代码中保留了相关命名。
项目采用标准的开源协作模式,欢迎通过 Issues 和 Discussions 参与贡献。
评论总结
评论总结:
- 对TJ Green的认可
- 认为TJ不是普通的程序员 "TJ is not your average vibe coder" (jascha_eng) "shoutout to tj for being super responsive on github issues" (diwank)
- 技术功能讨论
- 对BM25实现的疑问 "doesn't appear to have a mechanism to say 'filter for just documents matching terms X and Y'" (simonw) "That -5.0 is a magic number" (simonw)
- 关注索引构建时间 "Curious about the index build time on larger datasets" (shreyssh) "the bottleneck...isn't query speed, it's the initial indexing" (shreyssh)
- 高频率更新问题 "How does the BM25 implementation handle high-frequency updates" (Unical-A)
- 应用场景讨论
- 对RAG系统的价值 "huge benefit to all folks building RAG/rerank systems" (gmassman) "been doing some RAG prototypes with hybrid search" (maweaver)
- 与现有方案比较 "Goodbye manticore if this works" (bradfox2) "used to use paradedb...but paradedb was extremely unstable" (diwank)
- 许可问题
- 对AGPL许可的看法 "wonderful ad for ParadeDB, and clear signal that 'TigerData' is a pernicious entity" (gplprotects)
- 部署需求
- 云平台集成需求 "When will this be available on Supabase?" (jackyliang) "Please oh please let GCP add this" (mattbessey) "When is this available on AWS in Aurora?" (timedude)
- 其他技术吐槽
- PostgreSQL的不足 "amazing how hard it is to have a good case-insensitive search in Postgres" (piskov) "don't get me started with storing datetime with timezone" (piskov)
- 正面评价
- 对项目的期待 "VERY excited about this" (jackyliang) "This is really cool to see" (devmor) "Happy to see a 1.0 release" (maweaver)