Skip to content

Embeddings and vector search

How text becomes vectors, what similarity actually means, and when a vector index is the right tool.

6 MIN READUPDATED 2026-11-18Models & foundations
ORIGINAL DIAGRAM · 4 STEPS1Chunk content into mean…2Embed chunks with a3Store vectors with stab…4Embed the query and

Original flow diagram from note steps — not a copied image

§ 01Key concepts
EmbeddingA numeric vector that places semantically similar text near each other.
Cosine similarityAngle-based distance between vectors; the default for most text embeddings.
Vector indexA structure (e.g. HNSW) that finds nearest neighbours without scanning everything.
DimensionalityVector length; higher is not automatically better and costs more to store.
§ 02When to use
  • Finding semantically related documents where keywords fail.
  • Powering retrieval for RAG systems.
  • Deduplication, clustering or recommendation over text.
§ 03How it works
  1. 01Chunk content into meaningful passages.
  2. 02Embed chunks with a model whose behaviour suits your domain and language mix.
  3. 03Store vectors with stable IDs and metadata filters in an index.
  4. 04Embed the query and retrieve nearest neighbours, then filter/rerank.
§ 04Use cases & scenarios

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.
§ 05Examples

Index support articles with pgvector

sql
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

python
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.

§ 06Trade-offs

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

§ 07Implementation checklist
  • 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.
§ 08Configuration reference
KeyTypical valueNote
similaritycosineConfirm your index and metric agree.
top_k5–20Retrieve more, then rerank to a small final set.
ef_search / probesindex-specificTune recall against latency.
§ 09Troubleshooting
SymptomCauseFix
Irrelevant neighboursQuery/chunk mismatch or wrong modelAlign chunk granularity and embedding model.
Slow search at scaleIndex parameters too conservativeTune HNSW/probe settings or shard the index.
§ 10Frequently asked questions

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