Understanding Embeddings and Vector Similarity
This lesson explains what embeddings are, how they are generated, how similarity is measured and why approximate indexes exist.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what an embedding and its dimensions represent.
- Describe how to generate embeddings with keyless authentication.
- Distinguish cosine, dot product and Euclidean distance.
- Compare exact search with approximate index families.
What an embedding is
An embedding is a list of numbers, a vector, that an embedding model produces from a piece of content such as a sentence, a paragraph, an image or a product record. The model is trained so that content with similar meaning produces vectors that point in similar directions. The question "How do I reset my password?" and the passage "To change a forgotten password, select Forgot password on the sign-in page" share few words, but their embeddings are close, which is what allows semantic search to find the passage.
The length of the vector is its number of dimensions, fixed by the model. Azure OpenAI's text-embedding-3-small produces 1,536 dimensions by default and text-embedding-3-large 3,072, and both accept a dimensions parameter that returns a shorter vector at a small cost in quality. Every vector stored for a given field must have the same number of dimensions, and that number is declared in the database's schema or vector policy.
Two rules follow from how embeddings work. First, the query and the stored content must be embedded by the same model (and the same dimension setting): vectors from different models live in different spaces and cannot be compared. Second, changing the embedding model means re-embedding every stored item, which is a significant migration for large collections.
Embeddings
Generating embeddings
Embeddings are generated by calling an embedding model, usually a deployment in Microsoft Foundry (Azure OpenAI). The following Python code uses keyless authentication: DefaultAzureCredential obtains a Microsoft Entra token, which requires the identity to hold a role such as Cognitive Services OpenAI User on the resource.
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import OpenAI
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
client = OpenAI(base_url="https://RESOURCE.openai.azure.com/openai/v1/", api_key=token_provider)
response = client.embeddings.create(
model="text-embedding-3-small", # the deployment name
input=["How do I reset my password?", "Shipping takes three to five days."])
vectors = [item.embedding for item in response.data]
print(len(vectors), len(vectors[0])) # 2 1536A single request can embed many inputs, which is much faster than one request per item during bulk ingestion. Embedding calls are subject to the deployment's tokens-per-minute quota, so ingestion code batches inputs and retries on HTTP 429 responses with backoff. Embedding generation is usually done by the application or an ingestion worker, then stored next to the data in the vector store.
The following diagram shows why the searches fail and the two ways to fix them.

Generating embeddings in bulk
Measuring similarity
A vector store ranks items by a distance function between the query vector and each stored vector. Three functions are common, and the database must be told which one to use, because the same pair of vectors can rank differently under each.
- Cosine similarity measures the angle between vectors, ignoring their length. It ranges from -1 to 1, and higher means more similar; the corresponding cosine distance is 1 minus the similarity, so lower means more similar.
- Dot product (inner product) multiplies matching components and adds them up. For vectors normalized to length 1, it gives the same ranking as cosine similarity and is cheaper to compute.
- Euclidean distance (L2) measures the straight-line distance between vector end points. Lower means more similar.
Azure OpenAI embeddings are normalized to length 1, so cosine and dot product give the same order for them, and cosine is the usual default. The choice matters for models whose documentation recommends a specific metric, and for the operator and index type in each database, which must agree with the chosen metric. The following table maps the metrics to their names in the three services in this exam.
| Metric | Cosmos DB for NoSQL | PostgreSQL pgvector | Azure Managed Redis |
|---|---|---|---|
| Cosine | cosine | <=> with vector_cosine_ops | COSINE |
| Dot product | dotproduct | <#> (negative inner product) with vector_ip_ops | IP |
| Euclidean | euclidean | <-> with vector_l2_ops | L2 |
Metric names across services
Exact and approximate search
The simplest search, exact k-nearest neighbours (kNN), compares the query with every stored vector and returns the closest k. Its results are perfect, which is called 100% recall, but its cost grows linearly with the number of vectors: a million vectors of 1,536 dimensions means about 1.5 billion multiplications per query. Exact search is fine for small collections or when a filter has already narrowed the candidates to a few thousand items.
Approximate nearest neighbour (ANN) indexes avoid comparing everything. They organize vectors so that a search visits only a promising fraction, returning most of the true nearest neighbours in a fraction of the time. Recall becomes a tunable trade-off against latency and cost. Three families appear in this exam.
Quantization compresses vectors, for example from 32-bit floats to 16-bit floats or to compact codes, which reduces memory and speeds comparisons with a small loss of accuracy. Cosmos DB's quantizedFlat index and pgvector's halfvec type are examples.
Exact or approximate?
A search that misses obvious matches
A team stores support articles embedded with text-embedding-3-small. Later, they switch the query side of their search to text-embedding-3-large to improve quality. Searches now return seemingly random articles, although every query succeeds.
The queries and the stored articles are now embedded by different models, so their vectors live in different spaces and distances between them are meaningless; a dimension mismatch might even be rejected by the database. The team must either return the query side to text-embedding-3-small, or re-embed all articles with text-embedding-3-large and update the vector field's declared dimensions before switching queries.
Changing the distance function from cosine to Euclidean would not help, because the problem is the model, not the metric. Raising the number of results returned would only return more random articles.
Why do cosine similarity and dot product return the same ranking for Azure OpenAI embeddings?
Show answer
Azure OpenAI embeddings are normalized to length 1, and for unit vectors the dot product equals the cosine of the angle between them.
Exam Tips
Remember for the exam
- Embed queries and stored content with the same model and dimension setting.
- Normalized vectors rank the same under cosine and dot product.
- Exact kNN gives 100% recall but scans everything; ANN indexes trade recall for speed.
- Changing embedding models requires re-embedding stored items.