CREATETABLEdocuments(idSERIALPRIMARYKEY,passageTEXTNOTNULL,embeddingbm25vector);INSERTINTOdocuments(passage)VALUES('PostgreSQL is a powerful, open-source object-relational database system. It has over 35 years of active development.'),('Full-text search is a technique for searching a single computer-stored document or a collection in a full-text database.'),('BM25 is a bag-of-words retrieval function that ranks a set of documents based on the query terms appearing in each document.'),('VectorChord-bm25 is a PostgreSQL extension that provides native BM25 ranking index using the Block-WeakAnd algorithm.');
4. 将文档分词为 bm25vector
tokenize(text, tokenizer_name) 返回一个由词元 ID 组成的 INT[] 数组,它可以隐式转换为 bm25vector(自动将重复的词元 ID 聚合为词频):
为什么 <&> 返回负数?
PostgreSQL 仅支持按升序(ASC)执行索引驱动的 ORDER BY 扫描。因此,<&> 操作符返回的是负的 BM25 得分(例如 -2.8415)。数值越负代表相关性越高,从而使 ORDER BY embedding <&> to_bm25query(...) ASC LIMIT K 能够直接利用 Block-WeakAnd 索引扫描。如果希望展示正数得分,只需在 SELECT 列表中对该表达式取反(-(embedding <&> ...))即可。
工作流 B:原生 tsvector API(main 分支)
在新一代 main 分支重构中,vchord_bm25 可直接为 PostgreSQL 原生的 tsvector 类型建立索引——对于标准语言无需安装额外的分词器扩展。
1. 创建扩展与数据表
CREATEEXTENSIONIFNOTEXISTSvchord_bm25CASCADE;CREATETABLEdocuments(idSERIALPRIMARYKEY,passageTEXTNOTNULL,vectortsvectorGENERATEDALWAYSAS(to_tsvector('english',passage))STORED);INSERTINTOdocuments(passage)VALUES('PostgreSQL is a powerful, open-source object-relational database system.'),('Full-text search is a technique for searching documents in a full-text database.'),('BM25 is a bag-of-words retrieval function that ranks documents based on query terms.'),('VectorChord-bm25 provides a native BM25 ranking index in PostgreSQL.');