DocumentDB Vector Search for .NET
This .NET 8.0 sample application demonstrates vector similarity searches using Azure DocumentDB with different vector search algorithms (IVF, HNSW, and DiskANN). The application uses hotel data and text embeddings generated by Azure OpenAI.
Features
- Generate vector embeddings for hotel descriptions using Azure OpenAI
- Compare performance across IVF, HNSW, and DiskANN vector search algorithms
- Seamless integration with DocumentDB and Azure OpenAI services
- Interactive menu-driven application
Prerequisites
Azure Services
- Azure DocumentDB account
- Azure OpenAI Service with text-embedding-3-small model deployed
Development Environment
- .NET 8.0 SDK
- Visual Studio Code or Visual Studio 2022
- Azure CLI
- Azure DocumentDB for VS Code
Setup
- Clone the repository:
git clone https://github.com/documentdb-samples
cd ai/vector-search-dotnet
- Login to Azure:
az login
- Update
appsettings.jsonwith your Azure service details:
{
"AzureOpenAI": {
"Endpoint": "https://your-openai-service-name.openai.azure.com/"
},
"MongoDB": {
"TenantId": "your-azure-tenant-id",
"ClusterName": "your-documentdb-mongodb-cluster-name"
}
}
- Run the application:
dotnet restore
dotnet build
dotnet run
Application Menu
The application provides an interactive menu with the following options:
- Create embeddings for data (required first step)
- Show all database indexes
- Run IVF vector search (fast approximate search)
- Run HNSW vector search (high accuracy, memory-intensive)
- Run DiskANN search (balanced performance)
Vector Search Algorithms
- IVF (Inverted File): Fast approximate search with low memory usage
- HNSW (Hierarchical Navigable Small World): High accuracy but memory-intensive
- DiskANN: Balanced performance and memory efficiency
Project Structure
Models/- Configuration and data modelsServices/- Core business logic servicesUtilities/- Azure authentication helpersdata/- Sample hotel data filesProgram.cs- Application entry pointappsettings.json- Configuration file
Development Setup
Option 1: Dev Container
This project includes a dev container with all required tools. Open in VS Code and click "Reopen in Container".
Option 2: Manual Setup
Install .NET 8.0 SDK and Azure CLI, then run:
dotnet restore
dotnet build
Configuration
Update the following in appsettings.json:
AzureOpenAI.Endpoint: Your Azure OpenAI service endpointMongoDB.TenantId: Your Azure tenant IDMongoDB.ClusterName: Your DocumentDB cluster name
Troubleshooting
- Authentication errors: Run
az loginto authenticate with Azure - Connection issues: Verify DocumentDB cluster is running and accessible
- Embedding errors: Ensure Azure OpenAI service is deployed with text-embedding-3-small model
- Index failures: Check vector dimensions match (1536) and sufficient storage is available