Principle:23blocks OS Ai maestro Hybrid RAG Search
| Knowledge Sources | |
|---|---|
| Domains | Memory, Search, RAG |
| Last Updated | 2026-10-26 10:00 GMT |
Overview
A hybrid retrieval pattern that fuses lexical term-frequency scoring (BM25-style) with semantic embedding similarity, using configurable weights to rank results.
Description
Hybrid search combines two retrieval signals:
- Lexical (searchByTerm): extracts terms from the query, searches each term in CozoDB, and collects results with frequency scoring. Exact term matches rank high for keyword queries.
- Semantic (semanticSearch): generates an embedding for the query and searches by embedding similarity. Semantically related results rank high even without exact term overlap.
The results are fused using configurable `lexicalWeight` and `semanticWeight`. `hybridSearch` runs both paths and merges. `searchMemories` searches long-term memories (not raw conversations) using semantic similarity with optional related-memory traversal.
Usage
Use this principle when designing search where:
- Both exact keyword matches and semantic similarity matter
- The balance between lexical and semantic can be tuned per query
- Results must be ranked by a fused score
Theoretical Basis
The hybrid score is a weighted fusion:
hybridSearch(agentDb, query, {lexicalWeight, semanticWeight}):
termResults = searchByTerm(agentDb, query) // term frequency
semResults = semanticSearch(agentDb, query) // embedding similarity
return fuse(termResults, semResults, lexicalWeight, semanticWeight)
The lexical path extracts terms from the query (not the full text) and matches against a term index in CozoDB. The semantic path generates a query embedding and compares against stored embeddings via cosine similarity.