The Case for Native BM25

Traditional Postgres full-text search (using GIN or RUM) often struggles at scale because it lacks sophisticated algorithmic scoring. It is susceptible to "keyword stuffing" and fails to account for the rarity of specific terms. BM25 (Best Matching 25) addresses these limitations by evaluating term frequency and inverse document frequency while normalizing for document length. This ensures that short, highly relevant documents are not penalized or outranked by long, rambling text that happens to repeat a keyword.

By integrating BM25 directly into AlloyDB and Cloud SQL via the pg_search extension, developers can eliminate the need for a separate search engine database, simplifying architecture while maintaining high-performance relevance ranking for GenAI and RAG (Retrieval-Augmented Generation) applications.

Tuning and Index Selection

When implementing BM25, developers gain granular control over search behavior through two primary parameters:

  • K1 (Term Frequency Saturation): Controls how much weight is given to repeated words.
  • B (Document Length Normalization): Determines how heavily the length of a document penalizes its relevance score.

Choosing the right index depends on the specific use case:

  • GIN: The general-purpose workhorse for semi-structured JSON or simple tag filtering.
  • RUM: The positional specialist, ideal for exact phrase matching (e.g., legal documents), though it requires more disk space.
  • BM25: The modern standard for algorithmic relevance, specifically recommended for RAG pipelines where semantic vector search needs to be combined with keyword-based filtering.

Hybrid Search Architecture

Hybrid search combines vector similarity (using models like text-embedding-005) with BM25 full-text search. The implementation typically uses a Common Table Expression (CTE) approach:

  1. Vector Search CTE: Performs a cosine distance search to retrieve semantically similar items.
  2. Text Search CTE: Executes the BM25 index query to rank items based on keyword relevance.
  3. Fusion: A final SELECT statement joins these results using Reciprocal Rank Fusion (RRF) to produce a unified, highly relevant list.

Google Cloud is also developing a hybrid search User Defined Function (UDF) that will automate this RRF process, further simplifying the integration of vector and full-text search components.