Recipe 1 • Hybrid Search
Tri-Modal Reciprocal Rank Fusion (RRF)
The Problem
Pure vector search fails when users search for exact product serial numbers, names, or codes. Pure lexical (BM25) search fails on semantic, conceptual queries.The Solution
Execute lexical BM25 matching and SIMD vector nearest-neighbor search concurrently, fusing candidate rankings via Reciprocal Rank Fusion:RRF(d) = Σ 1/(60 + rank(d)).
import tapirus
conn = tapirus.connect("production.tapir")
# 1. Ingest documents with title, text, and vector embedding
conn.execute("""
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY,
title TEXT,
content TEXT,
embedding VECTOR(4)
);
""")
# 2. Hybrid Query: Vector near query + SQL lexical filter
query_vec = [0.85, 0.12, 0.05, 0.40]
results = conn.query(f"""
SELECT id, title, content
FROM articles
VECTOR NEAR embedding = {query_vec} TOP 10
WHERE content LIKE '%quantum%'
ORDER BY id ASC;
""")
print("Hybrid Matches:", results)
use tapirus::{Connection, Result};
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute("
CREATE TABLE kb (id INTEGER PRIMARY KEY, doc TEXT, emb VECTOR(4));
INSERT INTO kb VALUES (1, 'Quantum Encryption Guide', [0.9, 0.1, 0.0, 0.0]);
")?;
// Direct fused hybrid retrieval in-process
let rows = conn.query("
SELECT id, doc
FROM kb
VECTOR NEAR emb = [0.88, 0.12, 0.0, 0.0] TOP 5
WHERE doc LIKE '%Encryption%';
")?;
for r in rows {
println!("Match: {:?}", r.get::<String>("doc")?);
}
Ok(())
}