Semantic Search and Embeddings
Understand how embeddings let search match meaning instead of exact words, with a worked example and clear limits.
Key takeaways
- An embedding represents a piece of text as a list of numbers, arranged so that similar meanings land near each other.
- Search compares the query's embedding with stored ones, commonly using cosine similarity.
- Large collections use approximate nearest neighbor indexes that trade a little accuracy for much more speed.
- Keyword search is still better for exact names, codes and rare terms, so many systems combine both.
- A high similarity score means related, which isn't the same as correct or answering the question.
Semantic search finds results that mean something close to your query, even when they use different words. It works by turning text into embeddings, which are lists of numbers produced by a trained model, and then looking for the lists that sit closest together. A search for "laptop won't turn on" can surface an article titled "Notebook fails to power up".
This explainer shows what an embedding is, how closeness is measured, why large collections need shortcuts and when plain keyword matching still wins.
What keyword search misses
Traditional search matches terms. It uses an inverted index, a table listing which documents contain each word, and scores matches by factors such as how often a term appears and how rare it is. Scoring methods like BM25 do this well and fast.
The weakness is vocabulary. People describe the same thing in many ways: "cheap flights" and "low-cost airfare", "remove" and "uninstall", "my screen is black" and "display not working". Engines soften this with stemming, which reduces words to a root form, and with synonym lists. Those help, but someone has to anticipate each variation.
The opposite problem also happens. The word "jaguar" matches pages about an animal and a car. Keywords alone can't tell which you mean, though the rest of your query often gives it away. Semantic methods use the whole phrase to form a picture of intent.
What an embedding is
An embedding model reads a word, sentence or passage and outputs a fixed-length list of numbers, called a vector. Typical vectors have hundreds or thousands of positions. No single position has a tidy label like "is about animals". Meaning is spread across the whole pattern.
The model is trained on large amounts of text, often with pairs that should be close, such as a question and its answer, and pairs that should be far apart. Over time it learns an arrangement in which distance reflects relatedness. The same idea works for images and audio, and some models place text and pictures in a shared space, so a typed description can find a photo.
Two rules follow from this. Vectors from different models aren't comparable, so if you change models you must re-embed your collection. And the model's training shapes what it considers similar, including any skews in the data. Bias in AI systems discusses that concern.
Measuring closeness: a small example
The usual measure is cosine similarity, which looks at the angle between two vectors. A value of 1 means they point the same way; 0 means they're at right angles. It equals the dot product divided by the product of the two lengths.
Real embeddings have far more dimensions, but two are enough to show the arithmetic. These numbers are invented for the example.
| Item | Vector | Dot product with query | Cosine similarity |
|---|---|---|---|
| Query: "laptop won't start" | (3, 4) | ||
| Doc A: "notebook fails to power up" | (4, 3) | 3×4 + 4×3 = 24 | 24 / (5×5) = 0.96 |
| Doc B: "how to bake sourdough" | (4, -3) | 3×4 + 4×(-3) = 0 | 0 / (5×5) = 0.00 |
Each vector here has length 5, because the square root of 3² plus 4² is 5. Doc A shares no important words with the query, yet scores high. Doc B scores zero and drops to the bottom.
Illustrative values from a two-dimensional worked example and invented extra cases. Hypothetical example: the numbers are made up to show the method, not a quote or an average.
Building a semantic search
A basic system has two phases.
Indexing, done ahead of time:
- Split documents into passages of a sensible size.
- Run each passage through the embedding model.
- Store the vector with a pointer to the original text and metadata such as date and category.
Querying, done live:
- Embed the query with the same model.
- Find the stored vectors nearest to it.
- Apply filters, such as language or product.
- Return the matching passages, optionally reranked.
Passage size matters. A vector for an entire long document blurs many topics into one average, while a vector for a single sentence may lack context. Paragraph-sized or section-sized pieces are a common starting point, tested against real queries.
Why large collections need shortcuts
Comparing a query with every stored vector is exact and simple, and fine for thousands of items. With many millions, it becomes slow. Systems then use approximate nearest neighbor search, which builds a structure that lets it examine only a fraction of the vectors.
One widely used method builds a layered graph of links between vectors, known as HNSW (hierarchical navigable small world). Search starts in a sparse upper layer and moves downward toward closer neighbors. Other methods group vectors into clusters and search only the nearest groups, or compress vectors to save memory.
The shared trade-off: faster and smaller usually means occasionally missing a true neighbor. Most tools expose settings that let you spend more time for better recall. Filtering can complicate this, because a strict filter applied after the neighbor search may leave too few results.
Where keywords still win
Embeddings capture gist, and gist is the wrong tool for some jobs.
- Exact identifiers. Part numbers, error codes and order IDs differ by one character yet mean completely different things.
- Rare names. A new product name or unusual surname may be poorly represented in the model.
- Negation. "Phones with a headphone jack" and "phones without a headphone jack" can land close together.
- Precise phrases. Legal or quoted wording needs literal matching.
- Explainability. You can see why a keyword matched; a vector score is harder to explain.
That's why hybrid search is common: run both methods and merge. One simple merging technique, reciprocal rank fusion, gives each result points based on its position in each list, so items ranked well by both rise. It avoids having to compare two unrelated score scales.
Testing whether it actually helps
A demonstration with a few clever queries proves little. Build a test set instead. Take fifty or so real queries and, for each, write down which documents a knowledgeable person would expect to see. Then run keyword search, vector search and a hybrid against the same set.
Two simple measures go a long way. Recall at a cutoff asks what share of the expected documents appear in, say, the top ten. Precision at that cutoff asks what share of the top ten are relevant. Read the failures one by one. You may find that a missing synonym explains many keyword misses, while vector misses cluster around product codes or negated phrases.
Also test queries that should return nothing. Nearest neighbor search always finds a nearest item, even when the whole collection is irrelevant. If someone asks a shoe store's search about tax forms, the closest vector is still some shoe. A minimum score, a reranker judgment or a clear "no good matches" message keeps weak results from posing as answers.
Uses beyond a search box
Once text is a vector, "what is similar?" becomes a general tool. Sites use embeddings to suggest related articles, detect near-duplicate support tickets, group feedback by theme and route messages to the right team. They also supply the retrieval step in retrieval-augmented generation, where a language model writes an answer from the passages found.
Embeddings of sensitive text should be protected like the text itself. They aren't readable at a glance, but research has shown that information about the original can be recovered from them in some conditions. Apply access controls and deletion rules to the vectors too.
Practical advice if you're adding it to a site
Start by collecting real queries, especially ones that returned nothing. Check whether better titles or synonyms solve them before adding new infrastructure. If you do add vectors, keep your keyword search and compare results side by side on the same queries.
Decide how you'll update: new content needs embedding, edited content needs re-embedding and deleted content needs removal. Budget for the model calls or the hardware to run a model yourself. Then measure whether people find what they came for, which matters more than any similarity score.
Common questions
What does a similarity score of 0.8 mean?
Only that two vectors are fairly close for that model. Scales differ between models, so set any cutoff by testing on your own data.
Do I need a special database?
Not always. Several general-purpose databases and search engines support vectors. A dedicated vector database may help at large scale or with demanding filtering.
Does semantic search understand my question?
It models patterns of usage well enough to match related wording. It doesn't verify facts, and the top result can be closely related while answering something else.