Vector Similarity Search in Cosmos DB
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Vector Similarity Search in Azure Cosmos DB for NoSQL
Introduction: The New Frontier of Data Retrieval
In the traditional world of databases, we have spent decades perfecting the art of exact matching. We query by primary keys, filter by specific values, or perform range scans on structured data. However, the rise of artificial intelligence—specifically large language models and generative AI—has fundamentally changed how we interact with information. We no longer just want to find records that contain the word "cat"; we want to find records that are "conceptually similar" to a user's intent, image, or audio file.
This is where Vector Similarity Search comes into play. Instead of storing data as raw text or numbers, we transform that data into high-dimensional arrays of floating-point numbers called "embeddings." These vectors represent the semantic meaning of the data. When we perform a search, we convert the user's query into a similar vector and calculate the "distance" between the query vector and the data vectors. The closer the vectors, the more relevant the result.
Azure Cosmos DB for NoSQL has integrated native support for vector indexing and search, allowing you to store and query these embeddings directly within your database. This eliminates the need to maintain a separate specialized vector database, simplifying your architecture, reducing latency, and allowing you to leverage the existing global scale and security of the Cosmos DB platform. Understanding how to implement this is essential for any developer looking to build modern, AI-powered applications that go beyond simple keyword matching.
Understanding Embeddings and Vector Space
Before diving into the implementation, it is vital to understand what an embedding actually is. An embedding is a numerical representation of a piece of information, such as a sentence, an image, or a product description. These models, often provided by services like OpenAI or open-source libraries like Hugging Face, take input data and map it to a coordinate in a multi-dimensional space.
Imagine a simple two-dimensional space where one axis represents "food" and the other represents "technology." A picture of an apple might sit near the food axis, while a picture of a laptop sits near the technology axis. In reality, modern models use hundreds or thousands of dimensions to capture subtle nuances like tone, context, and relationships.
Callout: Vector vs. Scalar Search Scalar search, which we use in traditional SQL databases, relies on exact matches or range comparisons (e.g.,
WHERE price > 100). It is binary—a record either matches the criteria or it does not. Vector search is probabilistic and semantic. It calculates the proximity of data points in a high-dimensional space, allowing the system to return "best fit" results even when there is no exact keyword match.
Key Distance Metrics
When searching for similar vectors, we need a way to measure the "closeness" of two points. Cosmos DB supports several standard mathematical approaches:
- Cosine Distance: Measures the cosine of the angle between two vectors. It focuses on the orientation of the vectors rather than their magnitude. This is the most popular choice for natural language processing.
- Euclidean Distance (L2): Measures the straight-line distance between two points in space. It is highly sensitive to the magnitude of the vectors.
- Inner Product (IP): Calculates the dot product of two vectors. It is often used when the vectors are normalized, and it is generally the fastest to compute.
Configuring Cosmos DB for Vector Search
To use vector search in Cosmos DB, you must ensure your container is properly configured. Vector indexing is not enabled by default for all containers, and you must define the vector embedding policy when creating or updating the container.
Step-by-Step Container Setup
- Define the Vector Embedding Policy: This policy specifies how the database should handle your vectors. You must define the path where the vector is stored (e.g.,
/embedding), the data type (e.g.,float32), the distance function to use, and the dimensions of the vector. - Define the Vector Indexing Policy: This policy determines how the vectors are indexed for efficient searching. Cosmos DB currently supports
flatindexing (exact search) andquantized flatordiskann(approximate nearest neighbor search). - Create the Container: Use the Azure SDK or the Azure Portal to apply these policies.
Note: The dimensions of your vector must match the output of the embedding model you choose. For example, if you are using
text-embedding-3-smallfrom OpenAI, you must set your vector dimensions to 1536. If these values do not match, your queries will fail or return invalid results.
Example: Defining the Policy (JSON Format)
{
"vectorEmbeddingPolicy": {
"vectorEmbeddings": [
{
"path": "/vector",
"dataType": "float32",
"distanceFunction": "cosine",
"dimensions": 1536
}
]
},
"vectorIndexingPolicy": {
"vectorIndexes": [
{
"path": "/vector",
"type": "quantizedFlat"
}
]
}
}
Implementing Vector Search: Practical Walkthrough
Let’s look at how to perform a search operation. We will assume you have a collection of documents representing product descriptions, each containing an id, name, and an embedding array.
Step 1: Generating Embeddings
You cannot perform a search without first generating vectors for your data. You would typically use an Azure OpenAI endpoint to generate these embeddings.
import openai
def get_embedding(text):
response = openai.Embedding.create(
input=text,
model="text-embedding-3-small"
)
return response['data'][0]['embedding']
Step 2: Inserting Data
When you insert a document into Cosmos DB, you simply include the resulting vector as a property in your JSON document.
product = {
"id": "prod-001",
"name": "Wireless Noise-Canceling Headphones",
"vector": get_embedding("Wireless Noise-Canceling Headphones")
}
container.create_item(product)
Step 3: Executing a Vector Search
The query syntax uses the VectorDistance system function. This function takes the target vector (the one generated from the user's search query) and the property path containing the stored vectors.
SELECT TOP 5 c.name, VectorDistance(c.vector, [0.012, -0.045, ...]) AS similarity
FROM c
ORDER BY VectorDistance(c.vector, [0.012, -0.045, ...])
Warning: Never hardcode your query vectors in your application code. The example above shows the vector as a list for clarity, but in a real-world scenario, you should generate the query vector at runtime based on user input and pass it as a parameter to your query.
Indexing Strategies: Flat vs. Approximate
Choosing the right indexing strategy is a critical performance decision. Cosmos DB provides different ways to index vectors, each with trade-offs regarding speed, accuracy, and cost.
Flat Indexing
The flat index performs an exhaustive search. It compares the query vector against every single vector in your container.
- Pros: 100% accuracy (it will always find the exact nearest neighbor).
- Cons: Very slow on large datasets. It consumes significant CPU resources as the dataset grows.
- Use Case: Small datasets (e.g., a few thousand documents) where accuracy is more important than response time.
Quantized Flat Indexing
This approach compresses the vectors to reduce the memory footprint and speed up the distance calculation.
- Pros: Much faster than standard flat indexing and consumes less memory.
- Cons: Slight loss in precision due to quantization.
- Use Case: Medium-sized datasets where you need a balance between speed and accuracy.
DiskANN (Approximate Nearest Neighbor)
DiskANN is an advanced indexing algorithm designed for large-scale, high-performance vector search. It builds a graph-based structure that allows the database to navigate to the nearest neighbors without scanning the entire collection.
- Pros: Extremely fast and highly scalable. It provides the best performance for millions of records.
- Cons: More complex to configure and requires more careful monitoring of index build times.
- Use Case: Production-grade systems with large datasets (e.g., millions of products or documents).
Best Practices for Production
Building a system that relies on vector search requires more than just functional code. To ensure your application remains stable and performs well under load, consider the following industry standards.
1. Monitor Request Unit (RU) Consumption
Vector search is computationally intensive. Every time you perform a VectorDistance calculation, it consumes Request Units. An exhaustive scan over a large collection can quickly deplete your provisioned throughput. Always monitor your RU consumption in the Azure Portal and consider using Autoscale or increasing throughput before deploying to production.
2. Handle Vector Dimensionality Carefully
If you change your embedding model (e.g., upgrading from a 1536-dimension model to a 3072-dimension model), you cannot simply update the existing documents. You must re-index your data. Changing the dimensions requires a new container or a full migration of the data, as the index policy is immutable once set.
3. Combine Vector and Scalar Filtering
One of the most powerful features of Cosmos DB is the ability to perform "hybrid search." You do not have to rely solely on vectors. You can combine a vector search with a standard SQL filter.
SELECT TOP 5 c.name
FROM c
WHERE c.category = 'Electronics'
ORDER BY VectorDistance(c.vector, @queryVector)
By filtering by category before calculating the distance, you significantly reduce the search space, which improves performance and reduces RU consumption.
4. Normalize Your Vectors
If you are using Inner Product (IP) as your distance function, ensure your vectors are normalized. Inner Product is only mathematically equivalent to Cosine Similarity when the vectors have a magnitude of 1. Failing to normalize will lead to skewed results that do not reflect true semantic similarity.
Callout: The "Hybrid Search" Advantage While vector search is great for finding similar concepts, it can sometimes struggle with specific metadata. For example, a user might search for "cheap laptops." A vector model understands "laptop," but it might not perfectly interpret "cheap." By combining vector search with a scalar filter (
price < 500), you get the best of both worlds: semantic understanding plus strict business logic constraints.
Common Pitfalls and How to Avoid Them
Even experienced developers can run into issues when implementing vector search. Here are common mistakes to watch out for.
The "Stale Index" Problem
When you insert a new document, it may take a few moments for the vector index to update. If you perform a search immediately after insertion, you might not see the new document in your results. While Cosmos DB is designed to be consistent, indexing overhead is a reality. If immediate consistency is required for your use case, design your application to handle the slight delay or use a read-your-writes consistency model.
Ignoring Throughput Limits
If your application performs a high volume of vector searches, you will hit your throughput limit faster than you would with standard CRUD operations. Do not assume that the RU cost of a vector search is equivalent to a simple SELECT *. Always perform load testing to determine the average RU cost of your specific vector query profile.
Misunderstanding Data Types
Ensure your application code sends floating-point numbers in the format expected by the database. If your database expects float32 (32-bit floating point), but your application sends float64 (double precision), you may encounter serialization errors or, worse, silent truncation that degrades search quality. Always explicitly cast your vectors in your application code before sending them to the database.
Over-indexing
It is tempting to index every vector field in every document, but this increases the cost of every write operation. Only index the fields you actually need for searching. If you have metadata that doesn't need to be searched via vectors, keep it in a separate, non-indexed field to save on storage and compute costs.
Comparison of Indexing Methods
To help you decide which approach is right for your project, refer to the following table:
| Indexing Type | Performance | Accuracy | Memory Usage | Best For |
|---|---|---|---|---|
| Flat | Low | High (100%) | Low | Small datasets, high precision |
| Quantized Flat | Medium | High | Medium | Medium datasets, balanced needs |
| DiskANN | High | High (Approx) | High | Large scale, production apps |
Advanced Architecture: The RAG Pattern
Vector similarity search is the backbone of the Retrieval-Augmented Generation (RAG) pattern. In a RAG architecture, you don't just return the search results to the user; you pass them to a large language model to generate a natural language response.
- Ingestion: You break your documents into chunks, generate embeddings for each chunk, and store them in Cosmos DB.
- Retrieval: When a user asks a question, you generate an embedding for that question and perform a vector search in Cosmos DB to find the most relevant document chunks.
- Augmentation: You take those chunks and inject them into a prompt template, along with the user's original question.
- Generation: You send the combined prompt to an AI model (like GPT-4), which generates an answer based only on the retrieved context.
This pattern is the industry standard for building "chat with your data" applications. Because Cosmos DB scales globally, you can provide low-latency RAG experiences to users around the world, making it a preferred choice for enterprise-grade AI solutions.
Troubleshooting Checklist
If your vector search results are not what you expect, work through this checklist:
- Check the model: Did you use the same embedding model for the search query as you used for the stored documents? If you change models, you must re-generate all stored embeddings.
- Check the dimensions: Do the dimensions of your query vector match the
dimensionsproperty in yourvectorEmbeddingPolicy? - Verify normalization: If using Inner Product, are your vectors normalized to a unit length?
- Review indexing: Have you actually created the vector index? Without an index, the database may fall back to a full scan, which is slow and expensive.
- Test with known pairs: Create a simple test case with two documents you know are similar. If a search for one does not return the other at the top of the list, your distance function or embedding quality is likely the issue.
Summary and Key Takeaways
Vector similarity search represents a fundamental shift in how we build data-driven applications. By moving from keyword-based retrieval to semantic, proximity-based search, we allow our applications to understand the intent behind data. Azure Cosmos DB for NoSQL provides a robust, scalable, and integrated environment to perform these operations, making it an excellent choice for modern AI applications.
Key Takeaways:
- Embeddings are semantic representations: They capture the "meaning" of data in high-dimensional space, enabling similarity searches that traditional SQL queries cannot perform.
- Infrastructure matters: You must carefully define your
vectorEmbeddingPolicyandvectorIndexingPolicyduring container creation, as these settings are largely immutable. - Choose the right index: Use
flatfor small datasets andDiskANNfor large-scale, high-performance production environments. - Leverage hybrid search: Combine vector similarity with standard scalar filters to improve both the accuracy of your results and the efficiency of your queries.
- Manage RUs effectively: Vector distance calculations are resource-heavy. Monitor your throughput and optimize by filtering data before searching.
- Consistency is key: Always use the same embedding model for both ingestion and querying, and ensure your data types and vector dimensions are consistent across your entire pipeline.
- RAG is the goal: Understand that vector search is most powerful when used as a component of a larger RAG architecture, allowing you to provide context-aware, accurate AI responses to user queries.
By mastering these concepts, you are not just learning how to use a database feature; you are gaining the ability to build intelligent systems that can process and interpret the complex, unstructured data that defines the modern digital landscape. Start small, focus on the quality of your embeddings, and scale your infrastructure as your application's needs grow.
Reach the last section to complete this lesson and earn points — you're on section 1 of 9.
- Azure Container Registry Basics
- Azure Container Registry Basics Quiz5q
- Build and Store Container Images
- Build and Store Container Images Quiz5q
- ACR Tasks for Building Images
- ACR Tasks for Building Images Quiz5q
- Deploy to Azure App Service
- Deploy to Azure App Service Quiz5q
- Environment Variables and Secrets
- Environment Variables and Secrets Quiz5q
- Azure Container Apps Overview
- Azure Container Apps Overview Quiz5q
- Environment and Revision Management
- Environment and Revision Management Quiz5q
- KEDA Event-Driven Scaling
- KEDA Event-Driven Scaling Quiz5q
- Azure Kubernetes Service Basics
- Azure Kubernetes Service Basics Quiz5q
- AKS Manifest Files
- AKS Manifest Files Quiz5q
- Container Monitoring and Troubleshooting
- Container Monitoring and Troubleshooting Quiz5q
- Cosmos DB SDK Basics
- Cosmos DB SDK Basics Quiz5q
- Query Optimization
- Query Optimization Quiz5q
- Indexing Policies
- Indexing Policies Quiz5q
- Consistency Levels
- Consistency Levels Quiz5q
- Vector Similarity Search in Cosmos DB
- Vector Similarity Search in Cosmos DB Quiz5q
- Change Feed Processor
- Change Feed Processor Quiz5q
- PostgreSQL SDK Basics
- PostgreSQL SDK Basics Quiz5q
- Schema Design and Data Types
- Schema Design and Data Types Quiz5q
- PostgreSQL Indexing Strategies
- PostgreSQL Indexing Strategies Quiz5q
- pgvector for Vector Workloads
- pgvector for Vector Workloads Quiz5q
- Vector Similarity Search in PostgreSQL
- Vector Similarity Search in PostgreSQL Quiz5q
- RAG Patterns with PostgreSQL
- RAG Patterns with PostgreSQL Quiz5q
- OpenTelemetry SDK Basics
- OpenTelemetry SDK Basics Quiz5q
- Distributed Tracing
- Distributed Tracing Quiz5q
- KQL for Log Analytics
- KQL for Log Analytics Quiz5q
- Metrics Analysis
- Metrics Analysis Quiz5q
- Application Insights Integration
- Application Insights Integration Quiz5q
- Alerting and Diagnostics
- Alerting and Diagnostics Quiz5q
- Managed Identity Configuration
- Managed Identity Configuration Quiz5q
- Private Endpoints
- Private Endpoints Quiz5q
- Network Security Groups
- Network Security Groups Quiz5q
- Certificate Management
- Certificate Management Quiz5q
- RBAC for AI Services
- RBAC for AI Services Quiz5q
- Service Principal Authentication
- Service Principal Authentication Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons