Sub topic 3
Combining Vector Search with Metadata Filters
Assume you know basic Python or SQL, dot products, embeddings, and database indexes. A vector database usually stores an embedding with metadata such as tenant, language, date, or access level.
Filtering and hybrid search
A vector query finds items near a query embedding. A metadata filter restricts which items are eligible:
SELECT id, content
FROM documents
WHERE tenant_id = 42 AND language = 'en'
ORDER BY embedding <=> '[0.2, 0.1, 0.9]'
LIMIT 10;The filter can run before or during approximate nearest-neighbor search. Pre-filtering gives precise access control but may leave too few candidates for an index. Post-filtering is often faster, but it can return fewer than k results. Test filtered recall separately from unfiltered recall.
Hybrid search combines lexical matching, such as BM25 keyword search, with vector similarity. Keyword search handles exact names and identifiers. Vector search handles paraphrases. A common approach retrieves candidates from both systems, then combines normalized scores or uses reciprocal rank fusion.
Retrieval-augmented generation
In retrieval-augmented generation (RAG), an application retrieves relevant chunks and gives them to a language model as context.
Chunk size, metadata quality, and the number of retrieved results often matter as much as the index. Measure answer quality separately from retrieval quality.
Evaluation and tuning
Build a labeled query set with known relevant documents. Compare an approximate index with exact search and calculate recall@k:
\[ \text{recall@k} = \frac{\text{relevant exact results found by approximate search}}{k} \]
Also record median and p95 latency, memory use, and results under realistic filters. For HNSW, increasing efSearch usually improves recall and latency cost. For IVF, increasing nprobe searches more clusters. Quantization reduces memory but can reduce ranking accuracy.
Operations and system choice
Updates and deletes require index maintenance. Track stale entries, rebuild thresholds, and consistency requirements. Sharding can increase capacity, but every shard must search and the coordinator must merge results. Keep tenant boundaries and replication in the design.
| Option | Good fit |
|---|---|
| Vector library | Local or embedded search with application-managed storage |
| Relational extension | Transactions, joins, and moderate vector workloads |
| Dedicated vector database | Large scale, distributed search, and managed operations |
Choose based on filtering, hybrid search, update rate, scale, and operational ownership, not benchmark speed alone.
Exercise
Which parameter would you increase first when HNSW recall is too low but memory usage is acceptable?
Show answer
Increase efSearch, then measure recall and p95 latency again.