GuidesUnderstanding AI

Hybrid search: keywords and meaning, together

By the Banzai Labs teamUpdated on 2 min read

Keywords12345Meaning12345FusionRRFTwo rankings, one more accurate final list
The keyword ranking and the meaning ranking are merged into a single list.

What each one misses

Two complementary searches
Keywords (BM25)
  • Finds “REF-2041” and proper names
  • Misses “couch” when you search “sofa”
  • Fast and explainable
Meaning (embeddings)
  • Finds synonyms and rephrasings
  • Fuzzy on codes and references
  • Needs an embedding model

Rank fusion

  1. 1Two listsThe top 50 of each search.
  2. 2A score per rank1 / (60 + rank) in each list.
  3. 3SumA document ranked well on both sides moves ahead.
function rrf(lists: string[][], k = 60) {
  const score = new Map<string, number>();
  for (const list of lists) list.forEach((id, i) => score.set(id, (score.get(id) ?? 0) + 1 / (k + i + 1)));
  return [...score.entries()].sort((a, b) => b[1] - a[1]).map(([id]) => id);
}

Where to use it

Case Why hybrid
RAG on documentation Free questions and exact function names
Store search Intents and product references
Customer support Varied wording and order numbers

Vectors: the vector database. The next step: reranking. An example: semantic search for online stores.

Frequently asked questions

How do you fuse two rankings with different scores?
The most common method, reciprocal rank fusion (RRF), ignores the scores and adds up 1 / (k + rank) for each document in each list, with k often set to 60.
Do I need a specialized engine?
No. PostgreSQL does both with its full-text search and the pgvector extension. Dedicated search engines also offer hybrid search.
When is hybrid search useless?
When queries are always exact codes (keywords alone are enough) or always free questions on a small corpus (meaning alone may be enough).

Read next

All guides