Introduction
Search for "automobile repair" in a list of shop names and an exact-word search finds nothing if the right shop calls itself "car service". Semantic search fixes this by comparing meanings instead of strings: both texts become embeddings, and close embeddings mean related texts. It is the search behind modern help centres and shopping sites, and the "retrieval" in retrieval-augmented generation (next lesson).
How it works
A semantic search system has two phases.
Indexing (once, ahead of time):
- Split each document into chunks of a few hundred words, because one embedding cannot capture a whole book. Neighbouring chunks share some words (the overlap), so a sentence cut at a boundary still appears whole in one chunk.
- Embed each chunk and store the vectors in a vector index.
Searching (for every query):
- Embed the query with the same model.
- Compute the cosine similarity between the query vector and every chunk vector (or ask the index for the nearest ones).
- Return the top-ranked chunks.
Real systems use an embedding model trained for sentences. A simple stand-in is to average the embeddings of a text's words, which is what the example does; it ignores word order but already captures topic.
Keyword search (counting shared words, often weighted by how rare each word is) is fast and precise for names, codes and exact phrases, but blind to synonyms. Semantic search handles synonyms and paraphrases, but can miss exact identifiers. Many production systems run both and merge the results (hybrid search).
Example
import math
# Tiny hand-made word embeddings: (vehicles, food, money)
vectors = {
"car": [0.9, 0.0, 0.1], "automobile": [0.95, 0.0, 0.05], "repair": [0.6, 0.0, 0.4],
"service": [0.5, 0.1, 0.4], "pizza": [0.0, 1.0, 0.1], "recipe": [0.0, 0.9, 0.0],
"bank": [0.0, 0.0, 1.0], "loan": [0.1, 0.0, 0.9],
}
docs = ["car service", "pizza recipe", "bank loan"]
def embed(text):
known = [w for w in text.split() if w in vectors]
return [sum(vectors[w][i] for w in known) / len(known) for i in range(3)]
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
return dot / round(math.sqrt(sum(x * x for x in a) * sum(y * y for y in b)), 12)
query = "automobile repair"
print("keyword matches:", [d for d in docs if set(d.split()) & set(query.split())])
q = embed(query)
ranked = sorted(docs, key=lambda d: -cosine(q, embed(d)))
for d in ranked:
print(f"{cosine(q, embed(d)):.3f} {d}")Line by line
- Each word has a three-number embedding; car and automobile point almost the same way.
embedaverages the vectors of the known words in a text, giving one vector per document.- The keyword check finds no document sharing a word with "automobile repair".
- The documents are ranked by the cosine similarity between their vector and the query's.
- "car service" comes first, though it shares no word with the query.
Expected result
keyword matches: []
0.996 car service
0.329 bank loan
0.015 pizza recipe
In the real world
Help centres let users describe a problem in their own words and return the right article; shops match "comfy shoes for running" to products named "cushioned trainers"; code tools search a repository by what functions do. Vector databases and search engines provide indexes that return the nearest of millions of embeddings in milliseconds, using approximate methods instead of comparing every vector.
Common mistakes
- Embedding whole long documents, so one vector blurs many topics.
- Using different embedding models for the documents and the query.
- Dropping keyword search entirely, and then failing on product codes and names.
- Chunks with no overlap, which cut important sentences in half.
Best practices
- Choose chunk sizes that hold one idea, typically a few hundred words, with some overlap.
- Store each chunk's source and position, so results can be cited.
- Evaluate search on real questions with known right answers.
Summary
Semantic search compares meanings by embedding the query and the document chunks and ranking chunks by cosine similarity. It finds related text without shared words, unlike keyword search, which excels at exact terms. Documents are split into overlapping chunks before embedding, and many systems combine both kinds of search.
Quick check
Key takeaways
- Semantic search embeds the query and every chunk, then returns the chunks with the highest cosine similarity.
- It finds matches with no words in common, such as "automobile repair" and "car service".
- Long documents are split into overlapping chunks so each embedding covers one topic.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.