pg_bestmatch
Generate BM25 sparse vector inside PostgreSQL
pg_bestmatch : Generate BM25 sparse vector inside PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 2140 | pg_bestmatch | pg_bestmatch | 0.0.2 |
FTS | Apache-2.0 | Rust |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --sLd-- | No | Yes | Yes | Yes | no | no |
| Relationships | |
|---|---|
| Schemas | bm_catalog |
| See Also | pg_search pg_textsearch vchord_bm25 pg_fts pg_rrf pgroonga psql_bm25s vectorize pgcontext |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.0.2 |
18 17 16 15 14 | pg_bestmatch |
- |
| RPM | PIGSTY | 0.0.2 |
18 17 16 15 14 | pg_bestmatch_$v |
- |
| DEB | PIGSTY | 0.0.2 |
18 17 16 15 14 | postgresql-$v-pg-bestmatch |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| el8.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| el9.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| el9.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| el10.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| el10.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| d12.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| d12.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| d13.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| d13.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u22.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u22.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u24.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u24.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u26.x86_64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
| u26.aarch64 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 | PIGSTY 0.0.2 |
Source
github.com/tensorchord/pg_bestmatch.rs
pg_bestmatch-0.0.2.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Config this extension to shared_preload_libraries:
Create this extension with:
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.