Embeddings and Semantic Search Explained for Beginners

AI BASICS TO ADVANCED · LESSON 09
Different words.
Related ideas.
How numerical representations help a system find relevant text.

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.

TEXT BECOMES A VECTOR
01
Input
Ginger chai with milk
02
Embedding model
Encodes features of the text
03
Vector
An ordered list of numbers
Embeddings represent learned features. They do not contain a perfect or complete understanding of the text.

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.

COMPARE REPRESENTATIONS
01
Query
Hot drink with a spicy kick
02
Candidate
Ginger chai with milk
03
Comparison
Calculate a relevance-related score
A related menu entry is a candidate to inspect. Retrieval does not confirm that it satisfies every customer requirement.

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.

TWO USEFUL SEARCH SIGNALS
01
Lexical
Match words, identifiers, and phrases
02
Semantic
Compare learned representations
03
Hybrid
Combine both kinds of signal
Modern keyword search can include stemming and synonyms. Semantic search is not automatically better for every query.

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.

A MAP METAPHOR
01
Tea descriptions
Often related to one another
02
Fruit drinks
Another set of related descriptions
03
Bicycle repair
Usually less relevant to tea requests
There is no universal meaning map shared by every model. Similarity depends on training, task, and the chosen metric.

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.

READ THE STRUCTURE
01
Vector
[0.2, −0.4, 0.7]
02
Dimensions
Three numerical positions here
03
Interpretation
Not three named ingredients
This vector is invented only to show the format. It was not produced by an embedding model.

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.

PREPARATION
01
Source units
Menu entries or document passages
02
Encode
Create document embeddings
03
Store
Vectors + text references + metadata
Chunk size is a design choice. Very small chunks can lose context; very large ones can mix unrelated topics.

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.

AT SEARCH TIME
01
Encode query
Create the query vector
02
Compare
Score stored candidates
03
Retrieve
Return matching source text
Do not mix unrelated models just because their vectors have the same length. Follow the model’s query and document encoding guidance.

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.

TOY NUMBERS · EXACT CALCULATION
01
Query Q
[1, 0]
02
Candidate A
[0.8, 0.6] → score 0.8
03
Candidate B
[0, 1] → score 0
These are hand-chosen vectors, not actual tea embeddings. A score of 0.8 does not mean 80% correct or 80% likely to satisfy the query.
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.

RANKING IS RELATIVE
01
Top-k
Keep k highest-ranked candidates
02
Relevance check
Are they actually useful?
03
No good match
Return no result or ask for detail
Approximate search indexes can trade some retrieval accuracy for speed. Reranking can improve ordering but is another stage to evaluate.

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.

CHECK HARD REQUIREMENTS
01
Candidate
A semantically related drink
02
Constraints
Ingredients, price, availability
03
Verified choice
Inspect source data before offering
For allergy or other safety-sensitive requirements, do not infer suitability from a similarity score.

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.

KEEP THE RESPONSIBILITIES CLEAR
01
Embedding model
Represent text numerically
02
Search system
Retrieve relevant candidates
03
Generative model
Optionally compose an answer
A dedicated vector database is not required for a small demonstration. You can compare vectors in memory; larger systems often use indexes.

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.

MAINTAIN THE COLLECTION
01
Content changed
Refresh affected embeddings
02
Model changed
Rebuild compatible representations
03
Access changed
Enforce current permissions
Embedding is not encryption or anonymization. Treat representations of sensitive content with appropriate access controls.

Practice: choose a relevant result

Query: “How can I change my delivery address?”

THREE CANDIDATE HELP ARTICLES
01
A
Update the destination for an order
02
B
How to make ginger tea
03
C
Our shop’s opening hours
Which article is the strongest semantic candidate, even though its wording differs?
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.

YOUR ONE-MINUTE RECAP

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.


Comments

Popular posts from this blog

Beyond Standard RAG: The Architecture of Agentic Retrieval and Self-Correction Loops

The Architecture of Vision-Language Models: How Multimodal AI Sees and Understands the World

Java Collection Framework: A Practical Guide with Examples