Skip to main content

Hybrid Search

Agentium supports three search modes in KnowledgeBase:
Hybrid search is built into KnowledgeBase and works with all vector store backends (InMemory, PgVector, Qdrant, MongoDB). No additional dependencies needed.

Why Hybrid?

Pure vector search understands meaning but can miss exact terms. Pure keyword search matches terms exactly but misses semantics. Hybrid combines both:

Vector Search

Query: “time off vacation days” Finds: PTO policy doc (no doc says “vacation” but meaning matches)

Keyword Search

Query: “401k matching” Finds: 401(k) plan doc (exact term match scores high)
Hybrid search runs both searches in parallel, then merges the results using Reciprocal Rank Fusion — so you get the best of both worlds in a single query.

Quick Start


KnowledgeBase Config

"vector" | "keyword" | "hybrid"
default:"vector"
Default search mode for all search() and asTool() calls. Can be overridden per call.
HybridSearchConfig
Fine-tune hybrid search behavior. Only used when searchMode is "hybrid".

HybridSearchConfig

number
default:"1.0"
Weight for vector (semantic) results. Increase to favor semantic matches.
number
default:"1.0"
Weight for keyword (BM25) results. Increase to favor exact term matches.
number
default:"60"
RRF smoothing constant. Higher values dampen the effect of rank differences across the two result lists. Lower values make top-ranked results more dominant.

Per-Query Override

You can override the search mode on individual search() calls, regardless of the default:

With asTool()

Pass searchMode to asTool() to control how the agent searches:
If searchMode is not passed to asTool(), it inherits the KB’s default.

How It Works Under the Hood

1

Parallel retrieval

search() runs vector search (via the vector store) and keyword search (via the built-in BM25 index) in parallel. Each fetches topK × 2 candidates for better fusion quality.
2

BM25 scoring

The in-memory BM25Index tokenizes the query, computes term frequency / inverse document frequency scores, and ranks documents. Stop words are filtered, and scores are length-normalized.
3

Reciprocal Rank Fusion

Both ranked lists are merged using RRF. For each document, its fused score is:score = Σ weight_i / (k + rank_i)Documents appearing in both lists get scores from both, naturally ranking higher.
4

Final ranking

Results are sorted by fused score and trimmed to topK.

BM25 Index

The BM25Index is built-in and maintained automatically:
  • Auto-populated: When you call add() or addDocuments(), documents are indexed in both the vector store and the BM25 index.
  • Auto-cleaned: When you call delete() or clear(), documents are removed from both.
  • In-memory: The BM25 index lives in process memory. It’s rebuilt from the vector store’s documents on startup if needed.
  • Configurable: BM25 uses standard Okapi BM25 parameters (k1 = 1.5, b = 0.75) which work well for most use cases.

Tuning Tips


Full Example

See examples/knowledge/28-hybrid-search.ts for a complete comparison of all three search modes: