pg_bestmatch
Generate BM25 sparse vector inside PostgreSQL
Repository
tensorchord/pg_bestmatch.rs
https://github.com/tensorchord/pg_bestmatch.rs
Source
pg_bestmatch-0.0.2.tar.gz
pg_bestmatch-0.0.2.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_bestmatch | 0.0.2 | FTS | Apache-2.0 | Rust |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 2140 | pg_bestmatch | No | Yes | Yes | Yes | No | No | bm_catalog |
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.0.2 | 1817161514 | pg_bestmatch | - |
| RPM | PIGSTY | 0.0.2 | 1817161514 | pg_bestmatch_$v | - |
| DEB | PIGSTY | 0.0.2 | 1817161514 | postgresql-$v-pg-bestmatch | - |
Build
You can build the RPM / DEB packages for pg_bestmatch using pig build:
Install
You can install pg_bestmatch directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
Install the extension using pig or apt/yum/dnf:
Install
pig
dnf
apt
Preload:
Create Extension:
Usage
- repo: https://github.com/tensorchord/pg_bestmatch.rs
- benchmark: https://hazyresearch.stanford.edu/blog/2024-05-20-m2-bert-retrieval
How does it work?
- Create an BM25 statistics based on your document set by
bm25_create(table_name, column_name, statistic_name);. It will create a materilized view to record the stats. - Generate document sparse vector by
bm25_document_to_svector(statistic_name, passage) - For query, generate query sparse vector
bm25_query_to_svector(statistic_name, query) - Calculate the score by dot product between the query sparse vector and the document sparse vector
- Currently we use huggingface tokenizer with
bert-base-uncasedvocabulary set to tokenize words. Might support more configuration on tokenizer in the future.
Install
Example
Here is an example workflow demonstrating the usage of this extension with the example of Stanford LoCo benchmark.
- Load the dataset. Here is a script for you if you want to experience
pg_bestmatchwith the dataset.
- Create BM25 statistics for the
documentstable.
- Add an embedding column to the
documentsandqueriestables and update the embeddings for documents and queries.
- (Optional) Create a vector index on the sparse vector column.
- Perform a vector search to find the most relevant documents for each query.
This workflow showcases how to leverage BM25 text queries and vector search in PostgreSQL using this extension. The Top 1 recall of BM25 on this dataset is 0.77. If you reproduce the result, your operations are correct.
Comparison with pg_search
pg_bestmatch.rsonly provides methods for generating sparse vectors and does not support index-based search (which can be achieved by pgvecto.rs or pgvector).pg_searchperforms BM25 retrieval via the externaltantivyengine, which may have limitations when combined with transactions, filters, or JOIN operations. Sincepg_bestmatch.rsis entirely native to Postgres, it offers full compatibility with these operations inside postgres.
Reference
tokenize- Description: Tokenizes an input string into individual tokens.
- Example:
bm25_create- Description: Creates BM25 statistics for a specified table and column.
- Usage:
- Parameters:
table_name: Name of the table.column_name: Name of the column.stat_name: Name of the BM25 statistics.b: BM25 parameter (default 0.75).k: BM25 parameter (default 1.2).
bm25_refresh- Description: Updates the BM25 statistics to reflect any changes in the underlying data.
- Usage:
- Parameters:
stat_name: Name of the BM25 statistics to update.
bm25_drop- Description: Deletes the BM25 statistics for a specified table and column.
- Usage:
- Parameters:
stat_name: Name of the BM25 statistics to delete.
bm25_document_to_svector- Description: Converts document text into a sparse vector representation.
- Usage:
- Parameters:
stat_name: Name of the BM25 statistics.document_text: The text of the document.style: Emitspgvecto.rs-style sparse vector orpgvector-style sparse vector.
bm25_query_to_svector- Description: Converts query text into a sparse vector representation.
- Usage:
- Parameters:
stat_name: Name of the BM25 statistics.query_text: The text of the query.style: Emitspgvecto.rs-style sparse vector orpgvector-style sparse vector.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)