Share via

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

Setup

  1. Clone the repository:
git clone https://github.com/documentdb-samples
cd ai/vector-search-dotnet
  1. Login to Azure:
az login
  1. Update appsettings.json with 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"
  }
}
  1. Run the application:
dotnet restore
dotnet build
dotnet run

Application Menu

The application provides an interactive menu with the following options:

  1. Create embeddings for data (required first step)
  2. Show all database indexes
  3. Run IVF vector search (fast approximate search)
  4. Run HNSW vector search (high accuracy, memory-intensive)
  5. 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 models
  • Services/ - Core business logic services
  • Utilities/ - Azure authentication helpers
  • data/ - Sample hotel data files
  • Program.cs - Application entry point
  • appsettings.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 endpoint
  • MongoDB.TenantId: Your Azure tenant ID
  • MongoDB.ClusterName: Your DocumentDB cluster name

Troubleshooting

  • Authentication errors: Run az login to 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

Additional Resources