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.
- 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
Note
Vector indexes can be created or dropped on existing collections at any time — no special configuration is needed at resource creation.
- Azure DocumentDB account
- Azure OpenAI Service with text-embedding-3-small model deployed
- .NET 8.0 SDK
- Visual Studio Code or Visual Studio 2022
- Azure CLI
- Azure DocumentDB for VS Code
- Clone the repository:
git clone https://github.com/documentdb-samples
cd ai/vector-search-dotnet- Login to Azure:
az login- Configure environment variables:
The .NET sample reads configuration from appsettings.json and environment variables. After deploying with azd up, you can view your provisioned resource values:
azd env get-valuesUse these values to update appsettings.json or set them as environment variables.
- 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 runThe 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)
- 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
Models/- Configuration and data modelsServices/- Core business logic servicesUtilities/- Azure authentication helpersdata/- Sample hotel data filesProgram.cs- Application entry pointappsettings.json- Configuration file
This project includes a dev container with all required tools. Open in VS Code and click "Reopen in Container".
Install .NET 8.0 SDK and Azure CLI, then run:
dotnet restore
dotnet buildUpdate the following in appsettings.json:
AzureOpenAI.Endpoint: Your Azure OpenAI service endpointMongoDB.TenantId: Your Azure tenant IDMongoDB.ClusterName: Your DocumentDB cluster name
- 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