Embeddings and Semantic Search Explained for Beginners
Related ideas.
A customer asks Raju, “Do you have a hot drink with a spicy kick?” His menu says “Ginger chai: freshly brewed tea with milk and ginger.” The words are different, but the ideas are related. How could a search system find this menu entry? One useful approach combines embeddings with semantic search.
01. What is an embedding?
An embedding is a numerical representation produced by a model. In this lesson, we focus on text embeddings used for search: a passage is represented by a vector, which is an ordered list of numbers. A suitable trained model can place related text in positions that compare favorably under a chosen similarity measure.
Input
Ginger chai with milk
Embedding model
Encodes features of the text
Vector
An ordered list of numbers
02. Why represent text as numbers?
Computers can calculate relationships between vectors. Instead of requiring the exact phrase “spicy kick” to appear in the menu, a search system can compare the customer’s query representation with menu representations. Good retrieval models are trained to make useful query–document matches score well.
Query
Hot drink with a spicy kick
Candidate
Ginger chai with milk
Comparison
Calculate a relevance-related score
03. Keyword search and semantic search
Keyword search uses matching terms and related lexical signals. Embedding-based semantic search uses learned representations to find relevant content even when wording differs. Both have strengths: exact order numbers and product codes often benefit from lexical matching, while paraphrased questions can benefit from semantic matching.
Lexical
Match words, identifiers, and phrases
Semantic
Compare learned representations
Hybrid
Combine both kinds of signal
04. Think of related menu cards grouped together
Imagine arranging tea descriptions near other tea descriptions and bicycle-repair notes elsewhere. This is a memory aid for a vector space. Actual embeddings often have hundreds or thousands of dimensions, depending on the model; a tabletop picture cannot show all of those relationships.
Tea descriptions
Often related to one another
Fruit drinks
Another set of related descriptions
Bicycle repair
Usually less relevant to tea requests
05. What do individual numbers mean?
A toy vector might look like [0.2, −0.4, 0.7]. It is tempting to label these numbers “heat,” “sweetness,” and “ginger.” Real dense embedding dimensions generally do not come with such simple human-readable labels. Meaning-related information is distributed across the representation.
Vector
[0.2, −0.4, 0.7]
Dimensions
Three numerical positions here
Interpretation
Not three named ingredients
06. Prepare the searchable collection
Before searching, divide the material into useful units. Raju might use one menu entry per unit. A long handbook might need coherent passages, often called chunks. Embed each unit and keep a reference to its original text and useful metadata, such as its title, ID, or update date.
Source units
Menu entries or document passages
Encode
Create document embeddings
Store
Vectors + text references + metadata
07. What happens when the customer searches?
Encode the customer’s query using the compatible query configuration for the same retrieval model or model system. Compare it with stored document vectors, rank candidates, and return the selected text. Some models require different prompts or encoder paths for queries and documents while keeping them comparable.
Encode query
Create the query vector
Compare
Score stored candidates
Retrieve
Return matching source text
08. Cosine similarity: a tiny worked example
Cosine similarity compares vector direction. For nonzero real vectors it ranges from −1 to 1. Consider invented vectors Q = [1, 0], A = [0.8, 0.6], and B = [0, 1]. All have length 1. Their cosine scores are Q·A = 0.8 and Q·B = 0, so A ranks above B under this metric.
Query Q
[1, 0]
Candidate A
[0.8, 0.6] → score 0.8
Candidate B
[0, 1] → score 0
Optional: the cosine formula
Cosine similarity = dot product ÷ (length of vector 1 × length of vector 2).
For Q and A: (1 × 0.8 + 0 × 0.6) ÷ (1 × 1) = 0.8. The dot product multiplies corresponding numbers and adds them. Other systems may use dot product or distance directly; use the metric recommended for the model.
09. Top-k does not mean “k good answers”
If the system requests the top three matches, it may return the three highest-ranked candidates even when none is useful. Search needs evaluation with real queries and known relevant results. Any score threshold must be validated for the particular model, data, and task. There is no universal “safe” cosine cutoff.
Top-k
Keep k highest-ranked candidates
Relevance check
Are they actually useful?
No good match
Return no result or ask for detail
10. Similarity does not verify ingredients
“Tea with sugar” and “tea without sugar” share many words and a topic, yet the difference matters. Numerical similarity can miss negation, exact quantities, or strict constraints. Raju must check the actual ingredients and availability. Applications can use structured fields and filters for constraints instead of relying only on vector similarity.
Candidate
A semantically related drink
Constraints
Ingredients, price, availability
Verified choice
Inspect source data before offering
11. Embeddings, vector search, and LLMs have separate jobs
The embedding model creates representations. A vector index or database helps retrieve candidates. A generative language model can then write an answer using the retrieved text. Search alone can simply return relevant passages without generating any prose. When retrieval supplies context for generation, that is a common RAG pattern.
Embedding model
Represent text numerically
Search system
Retrieve relevant candidates
Generative model
Optionally compose an answer
12. Keep your search collection current
When Raju changes a menu description, the stored text and its vector should be updated together. Switching embedding models generally means re-embedding the collection in the new compatible space. Also preserve source IDs and permissions: a highly similar private document must not become visible to someone who cannot access it.
Content changed
Refresh affected embeddings
Model changed
Rebuild compatible representations
Access changed
Enforce current permissions
Practice: choose a relevant result
Query: “How can I change my delivery address?”
A
Update the destination for an order
B
How to make ginger tea
C
Our shop’s opening hours
Reveal the answer
A is the strongest candidate by the meaning of its title. You still need to open it and check whether it applies to the user’s order status. This is a human-written exercise, not a measured ranking.
Quick questions
Are tokens and embeddings the same?
No. Tokens are units used to represent input text. Embeddings are numerical vectors. A model may tokenize a passage internally before producing its search embedding.
Does an embedding generate an answer?
The vector itself does not write an answer. A search system compares vectors; a separate generation step may produce a written response.
Can it match across languages?
Some multilingual embedding models support this, but quality varies by language and task. Test the languages your readers actually use.
Embeddings represent.
Search retrieves.
Generation writes.
Related wording can help find candidates. Source checks establish whether those candidates meet the need.
Next lesson: Vector databases
How do we store and search many embeddings efficiently? We will explore vector indexes, metadata filters, and the difference between exact and approximate search.
Join the conversation: Write two differently worded questions that should retrieve the same help article.
- Sentence Transformers: Semantic search
- Sentence Transformers: Usage and query/document encoding
- Sentence Transformers: Similarity metrics and model reference
All story examples and numerical exercises were created for this lesson.
Comments
Post a Comment