Skip to content
Ameen Abozaid
MID-067

Embeddings and vector search

  • 6 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Embeddings
  • similarity metrics
  • pgvector
  • HNSW and IVFFlat indexes
  • hybrid keyword plus vector search

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Explain what an embedding is and why semantically similar texts end up close together.
  2. Choose a similarity measure (cosine, inner product, Euclidean distance) consistent with the embedding model.
  3. Store and query embeddings in PostgreSQL with pgvector, and choose between HNSW and IVFFlat indexes by recall, build time, and memory.
  4. Combine keyword (full-text) and vector search into hybrid search and explain when each wins.

Lessons

  1. 01What embeddings are
  2. 02Similarity measures
  3. 03pgvector: storing and querying vectors
  4. 04Approximate indexes: HNSW and IVFFlat
  5. 05Hybrid search: keywords plus vectors
  6. 06Demo: semantic search over Shop's help-center articles

Lab and exercises

Lab

MID-067-LAB1 · Searchable help center

Outcomes: 1–4 · Time: ~3 h Embed Shop's help-center and policy documents into pgvector, add an HNSW index, and expose a semantic search endpoint with a hybrid (full-text + vector) mode. Prepare a test set of 30 customer questions with the expected article for each. Record query latency and index size. Acceptance: the right article is in the top 3 for at least 25 of the 30 questions; query latency and index size recorded.

Exercises (assessment: design exercise)

IDOutcomeTask
MID-067-EX011Explain why "refund my order" and "money back for a purchase" are close as vectors, and name one case where embeddings fail.
MID-067-EX022A model documents cosine similarity and returns normalized vectors. Choose the operator and operator class and justify.
MID-067-EX033Write the table, an HNSW index, and a top-5 query with a language filter for a 768-dimension model.
MID-067-EX043Choose HNSW or IVFFlat for (a) 50 000 chunks updated daily, (b) 20 M chunks rebuilt monthly; compare recall, build time, memory.
MID-067-EX054Give three Shop queries and say whether keyword, vector, or hybrid wins for each.
MID-067-EX063Design exercise: for a new corpus (10 000 product manuals, 4 languages) choose model, dimensions, index, and refresh strategy and justify each.
Show solutions
  • EX01 The model maps similar meaning to nearby points. Failure: exact identifiers (SKUs, order numbers) or negation ("not refundable") may be missed.
  • EX02 Cosine distance <=> with vector_cosine_ops; for normalized vectors inner product gives the same ranking. Use the measure the model documents.
  • EX03 CREATE TABLE ... embedding vector(768); CREATE INDEX ... USING hnsw (embedding vector_cosine_ops); WHERE lang = 'ar' ORDER BY embedding <=> :q LIMIT 5. Verify syntax for your pgvector version.
  • EX04 (a) HNSW: handles inserts without rebuild and gives good recall; memory is small at this size. (b) Depends on the memory budget: IVFFlat builds faster and is smaller (build after load, tune lists and probes); HNSW usually has better recall. Measure recall for both.
  • EX05 "Where is my parcel": vector. "ORD-88231 status": keyword. "Return policy for electronics": hybrid.
  • EX06 Multilingual model if queries cross languages; dimensions as the model provides; HNSW with cosine; store model and content_hash; refresh only changed documents (hash compare) and re-embed everything only on a model change. Justify each with recall measured on a question set. Verify model choice and prices before deciding.

Outcomes 1 and 4 are also covered by the quiz and the lab (hybrid mode).

Quiz

Outcomes: O1 embeddings · O2 similarity measures · O3 pgvector and indexes · O4 hybrid search.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1An embedding is:O1
  2. 2Mixing vectors from two models in one column is a mistake because:O1
  3. 3Embeddings are weakest for:O1
  4. 4Which distance measure should you choose?O2
  5. 5For normalized vectors, inner product ranks like:O2
  6. 6The pgvector column type for 768 dimensions:O3
  7. 7Compared with IVFFlat, HNSW usually has:O3
  8. 8Before trusting an approximate index you should:O3
  9. 9Reciprocal Rank Fusion merges results by:O4
  10. 10Which finds "ORD-88231" best?O4

Practical part

Design exercise (MID-067-EX06) plus lab MID-067-LAB1. Pass: quiz at least 8/10, justified model, index, and refresh choices, and a lab with at least 25 of 30 questions answered in the top 3 and latency and index size recorded.

Common mistakes

Mixing embeddings from different models in one column; using a distance measure the model was not trained for; approximate indexes without measuring recall; re-embedding everything on every change.

Assessment

Design exercise — choose model, dimensions, index, and refresh strategy for a new corpus and justify each choice.

Career relevance

The data layer of every RAG feature (MID-068) and of AI solution architectures (ARC-045).

Type to search levels, phases, modules and lessons.