Skip to content
AI-200 Learn

Cosmos DB for AI & Vector Search

Store embeddings and metadata in Cosmos DB for NoSQL with vector similarity search.

What this covers

Azure Cosmos DB for NoSQL is the main document and vector store in AI-200. It holds chunked content, metadata and embeddings in the same item, answers semantic queries with VectorDistance, and streams changes so embeddings stay current. This guide summarizes the four Cosmos DB objectives and links to the detailed pages.

Exam weight: Develop AI solutions by using Azure data management services (30–35%). The skills-at-a-glance list and change log give 30–35%; the section heading in the study guide still says 25–30%.

Skills measured

  • "Connect to Azure Cosmos DB for NoSQL by using the SDK and run queries"
  • "Optimize query performance and Request Units (RUs) consumption by using indexing policies and consistency levels"
  • "Store and retrieve embeddings and execute vector similarity search for semantic retrieval"
  • "Implement a change feed processor to detect and handle new or updated items"

Key concepts

SDK and queries

  • One singleton CosmosClient, authenticated with DefaultAzureCredential and a Cosmos DB data-plane role.
  • Point reads (id + partition key) cost about 1 RU per KB; queries cost more, especially cross-partition.
  • Parameterized queries, SELECT VALUE, ARRAY_CONTAINS, JOIN within an item, paging with continuation tokens.

Details: SDK, queries.

Vector embeddings

Numerical representations of text or images used for similarity search. A container needs a vector embedding policy (path, float32/float16/int8/uint8, dimensions, cosine/dotproduct/euclidean) and a vector index:

IndexMax dimensionsUse
flat505Exact, small or heavily filtered
quantizedFlat4,096Up to ~50,000 vectors per search scope
diskANN4,096Large scale, lowest RU and latency

quantizedFlat and diskANN need 1,000+ vectors. Queries use SELECT TOP N ... ORDER BY VectorDistance(c.embedding, @v); hybrid search uses ORDER BY RANK RRF(VectorDistance(...), FullTextScore(...)). Details: store embeddings, similarity queries, filters and hybrid, tuning.

Indexing policies and RU cost

Indexing policies affect query cost and RU consumption. The default range-indexes every path; an opt-in policy indexes only filtered paths (plus the partition key) and excludes embeddings. Multi-property ORDER BY needs a composite index. Details: indexes, composite, RU reduction.

Consistency levels

Session is the default and the common choice for app reads. Know Strong versus Eventual, and that Strong and Bounded staleness reads cost about 2× RUs. Details: consistency.

Change feed

React to new or updated documents (for example, re-embed them). The change feed processor pattern (Functions trigger or .NET/Java library with a lease container) powers async embedding pipelines; Python uses the trigger or the pull model. Details: embedding refresh, change feed processor.

How it works

Diagram source: flowchart LR I[Ingest chunks] --> C[(Cosmos DB: text + metadata + vector)] C --> CF[Change feed] CF --> F[Functions trigger + leases] F --> E[Embedding model] E --> C U[User query] --> Q[VectorDistance + filters] Q --> C C --> R[Top N context for RAG]

Hands-on

Azure portal

  1. Create an Azure Cosmos DB for NoSQL account; enable Vector Search for NoSQL API under Features.
  2. In Data Explorer, create a container with partition key /tenantId and a vector policy on /embedding.

Azure CLI

Azure CLI / Bash
# Create Cosmos DB account (NoSQL API)
az cosmosdb create \
  --name cosmos-ai200 \
  --resource-group rg-ai200 \
  --locations regionName=eastus \
  --capabilities EnableNoSQLVectorSearch

az cosmosdb sql database create \
  --account-name cosmos-ai200 \
  --resource-group rg-ai200 \
  --name vectordb

az cosmosdb sql container create \
  --account-name cosmos-ai200 \
  --resource-group rg-ai200 \
  --database-name vectordb \
  --name documents \
  --partition-key-path "/tenantId"

Python

Python
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential

url = "https://cosmos-ai200.documents.azure.com:443/"
client = CosmosClient(url, credential=DefaultAzureCredential())  # or credential=key in a sandbox
container = client.get_database_client("vectordb").get_container_client("documents")

# Insert document with embedding vector
doc = {
    "id": "doc-1",
    "tenantId": "tenant-a",
    "content": "Azure AI services overview",
    "embedding": [0.12, 0.45, 0.78],  # simplified; real vectors match the policy's dimensions
}
container.upsert_item(doc)

# Vector similarity query
query = """
SELECT TOP 5 c.id, c.content, VectorDistance(c.embedding, @queryVector) AS score
FROM c
WHERE c.tenantId = @tenant
ORDER BY VectorDistance(c.embedding, @queryVector)
"""
items = list(container.query_items(
    query=query,
    parameters=[
        {"name": "@queryVector", "value": query_vector},
        {"name": "@tenant", "value": "tenant-a"},
    ],
    partition_key="tenant-a",
))

This query only works if the container was created with a vector embedding policy for /embedding; the plain CLI container above needs one added at creation (portal or SDK).

Exam traps

NeedService or option
Global distribution, elastic scale, vectors beside JSON documentsCosmos DB for NoSQL
Relational joins plus vectors in SQLAzure Database for PostgreSQL with pgvector
Sub-millisecond cache and short-lived vectorsAzure Managed Redis
Large-scale vector searchdiskANN index
Read your own write cheaplySession consistency
Re-embed on updateChange feed with a lease container

Quick review

  • Partition key choice drives query cost and RU consumption.
  • Exclude embeddings from range indexes; add a vector index.
  • ORDER BY VectorDistance returns most similar first.
  • Hybrid search fuses vector and BM25 with RRF.
  • Session is the default consistency; overrides only weaken.
  • The change feed processor checkpoints in a lease container and delivers at least once.

Practice

Practice: Cosmos DB SDK and query questions

Practice: Cosmos DB RU and indexing questions

Practice: Cosmos DB vector search questions

Practice: Cosmos DB change feed questions

Official references