Part 6 · 1 chapters · ~8 min

Vector and Hybrid Search

Embeddings for semantic search, approximate nearest neighbour indexes (HNSW, IVF) and their recall and latency trade-offs, vector fields in OpenSearch, Elasticsearch and pgvector, where vectors fail (codes, names, numbers), hybrid search with reciprocal rank fusion, reranking with cross-encoders, and evaluation.

7

Meaning plus exact terms

code
-- pgvector: hybrid in Postgres with RRF
WITH semantic AS (SELECT id, row_number() OVER (ORDER BY embedding <=> $1) AS r FROM merchants ORDER BY embedding <=> $1 LIMIT 50),
     keyword  AS (SELECT id, row_number() OVER (ORDER BY ts_rank(tsv, q) DESC) AS r FROM merchants, plainto_tsquery('english', $2) q WHERE tsv @@ q LIMIT 50)
SELECT id, COALESCE(1.0 / (60 + s.r), 0) + COALESCE(1.0 / (60 + k.r), 0) AS score
FROM semantic s FULL OUTER JOIN keyword k USING (id) ORDER BY score DESC LIMIT 20;
CREATE INDEX ON merchants USING hnsw (embedding vector_cosine_ops);
VECTOR AND HYBRID SEARCH
meaning from embeddings, exact terms from BM25, combined
query"where can I buy drugs near yaba"embedding modelquery vectorANN index (HNSW)nearest vectorsBM25exact termsfusionreciprocal rank fusionrerankercross-encoder on top 50
swipe the figure sideways, or tap expand for full screen
1/5
embeddings
An embedding model maps text to a vector so that similar meanings are close (Linear Algebra course P1). "drugs" and "pharmacy" end up near each other even though they share no characters.
meaning as vectorssynonyms without synonym lists