postgres-hybrid-text-search

Combine BM25 keyword search with semantic vector search in PostgreSQL.

1.8k|104|Updated Jul 23, 2025
One-click install
npx skills add https://github.com/timescale/pg-aiguide --skill postgres-hybrid-text-search
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: postgres-hybrid-text-search
Source: https://github.com/timescale/pg-aiguide/tree/main/skills/postgres-hybrid-text-search
Command: npx skills add https://github.com/timescale/pg-aiguide --skill postgres-hybrid-text-search

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill enables developers to implement hybrid search by combining BM25 keyword search with semantic vector search to deliver more relevant results in PostgreSQL-driven applications. It helps AI-assisted tooling retrieve both exact-match results and conceptually related documents in a single query flow.

Core Features & Use Cases

  • Hybrid search: merge BM25 keyword relevance with vector-based semantic similarity using Reciprocal Rank Fusion (RRF).
  • Setup guidance: demonstrates enabling pg_textsearch, pgvector, and optional vector indexing methods, plus client-side fusion logic.
  • Use Cases: document search, product catalogs, knowledge bases, code search, and QA systems that require both precise terms and semantic understanding.

Quick Start

Enable the extensions and create a sample documents table with both text and vector columns, then perform BM25 and semantic searches in parallel and fuse results client-side via RRF. Example steps:

  • Enable extensions: CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS pg_textsearch;
  • Create sample table with id, content, embedding;
  • Create BM25 and HNSW indexes;
  • Run parallel queries and fuse results on the client.

Frequently Asked Questions about postgres-hybrid-text-search

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I combine BM25 keyword search and semantic vector search in PostgreSQL?▼

Hybrid search in PostgreSQL combines BM25 and vector similarity by running both query types in parallel, then fusing results client-side using Reciprocal Rank Fusion (RRF) to merge exact keyword matches with semantic meaning.

What is Reciprocal Rank Fusion (RRF) and how does it work for hybrid search?▼

Reciprocal Rank Fusion (RRF) is a client-side technique that merges ranked result lists from BM25 and vector searches into a single output, weighting documents by their positions in each set to balance keyword and semantic relevance.

How do I set up pg_textsearch and pgvector for document search in PostgreSQL?▼

Setup requires enabling pgvector and pg_textsearch extensions via CREATE EXTENSION, creating a table with text and embedding columns, and building BM25 and HNSW indexes to support parallel keyword and semantic queries.

When should I use PostgreSQL hybrid search instead of just semantic or keyword search?▼

Use hybrid search for document search, product catalogs, and knowledge bases where both exact terms and meaning matter, ensuring precise keyword matches and conceptually related documents are retrieved in a single query flow.

Does PostgreSQL hybrid search require client-side logic to merge BM25 and vector results?▼

Yes, this approach requires client-side logic to execute parallel BM25 and vector queries from PostgreSQL, apply Reciprocal Rank Fusion (RRF) to the retrieved sets, and output the final merged document list.