| Embedding | A numeric vector that places semantically similar text near each other. |
| Cosine similarity | Angle-based distance between vectors; the default for most text embeddings. |
| Vector index | A structure (e.g. HNSW) that finds nearest neighbours without scanning everything. |
| Dimensionality | Vector length; higher is not automatically better and costs more to store. |
- Finding semantically related documents where keywords fail.
- Powering retrieval for RAG systems.
- Deduplication, clustering or recommendation over text.
- 01Chunk content into meaningful passages.
- 02Embed chunks with a model whose behaviour suits your domain and language mix.
- 03Store vectors with stable IDs and metadata filters in an index.
- 04Embed the query and retrieve nearest neighbours, then filter/rerank.
Finding a policy by meaning
- Context
- Support staff search for a return rule using different wording than the policy document.
- Problem
- Keyword search misses synonyms and paraphrases.
- Approach
- Embed versioned policy chunks with one model and retrieve by cosine similarity inside a tenant filter.
- Outcome
- The right passage surfaces with a citation, regardless of phrasing.
Deduplicating a merged catalogue
- Context
- Two vendor catalogues are merged into one product listing.
- Problem
- Duplicate products appear with different descriptions and codes.
- Approach
- Embed descriptions, cluster near-duplicate vectors and review the highest-similarity pairs.
- Outcome
- A cleaner catalogue with fewer duplicate SKUs.
Index support articles with pgvector
CREATE TABLE support_chunks (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
product TEXT NOT NULL,
section_path TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536)
);
CREATE INDEX ON support_chunks USING hnsw (embedding vector_cosine_ops);Store tenant and product as metadata columns so retrieval can filter before search. Use the same embedding model for documents and queries.
Filter before you search
q = embed("how do I return a leased device?")
rows = db.query("""
SELECT id, content, 1 - (embedding <=> :q) AS score
FROM support_chunks
WHERE tenant_id = :tenant AND product = :product
ORDER BY embedding <=> :q LIMIT 10
""", q=q, tenant=tenant, product=product)Apply permission and product filters in the index query, not after retrieval — otherwise one tenant can see another tenant's passages.
Vector-only retrieval
Up: Semantic matching beyond keywords
Down: Misses exact terms, names and numbers
High-dimensional embeddings
Up: Finer semantic resolution
Down: Larger index, slower search
Managed vector DB
Up: Scaling and filters handled for you
Down: Cost and data residency considerations
| Option | Upside | Cost |
|---|---|---|
| Vector-only retrieval | Semantic matching beyond keywords | Misses exact terms, names and numbers |
| High-dimensional embeddings | Finer semantic resolution | Larger index, slower search |
| Managed vector DB | Scaling and filters handled for you | Cost and data residency considerations |
- Use the same embedding model for documents and queries.
- Store source metadata alongside vectors for filtering and citations.
- Re-embed everything when you change the embedding model.
- Measure retrieval quality before tuning index parameters.
| Key | Typical value | Note |
|---|---|---|
| similarity | cosine | Confirm your index and metric agree. |
| top_k | 5–20 | Retrieve more, then rerank to a small final set. |
| ef_search / probes | index-specific | Tune recall against latency. |
| Symptom | Cause | Fix |
|---|---|---|
| Irrelevant neighbours | Query/chunk mismatch or wrong model | Align chunk granularity and embedding model. |
| Slow search at scale | Index parameters too conservative | Tune HNSW/probe settings or shard the index. |
Do I need a vector database?
Not always. Postgres with pgvector is enough for many workloads; dedicated indexes help at high scale or with complex filtering.
Can embeddings be updated in place?
Only with the same model and dimensionality. Otherwise rebuild the index.
More in Models & foundations
Language models: tokens, context and inference
The mental model behind an LLM call: tokenisation, context windows, sampling controls and why outputs are probabilistic.
Prompting vs fine-tuning vs RAG
A decision guide: which lever changes behaviour, which one adds knowledge, and what each one costs.
Model selection: capability, cost and latency
A practical framework for choosing a model per task instead of defaulting to the largest one.