This sample demonstrates how to compare all three vector search index algorithms (IVF, HNSW, DiskANN) with Azure DocumentDB using the MongoDB Java driver.
- Java 17 or later
- Maven 3.8+
- Azure DocumentDB cluster with vector search enabled
- Azure OpenAI resource with an embedding model deployed
- Azure CLI logged in (
az login) for passwordless authentication
-
After deploying with
azd up, use the provisioned output values to set the required environment variables in your terminal:export DOCUMENTDB_CLUSTER_NAME=your-cluster-name export AZURE_OPENAI_EMBEDDING_ENDPOINT=https://your-resource.openai.azure.com export AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-3-small export AZURE_DOCUMENTDB_DATABASENAME=Hotels export DATA_FILE_WITH_VECTORS=data/Hotels_Vector.json export EMBEDDED_FIELD=DescriptionVector export EMBEDDING_DIMENSIONS=1536
-
Copy the shared data file into the local
data/directory:mkdir -p data && cp ../data/Hotels_Vector.json data/
mvn clean compileCompare all 9 algorithm × similarity combinations:
mvn exec:java -PcompareOn Windows (PowerShell):
mvn exec:java -Pcompare| Algorithm | Description | Best For |
|---|---|---|
| IVF | Inverted File index — partitions vectors into clusters | Large datasets with batch queries |
| HNSW | Hierarchical Navigable Small World graph | Low-latency, high-recall searches |
| DiskANN | Disk-based Approximate Nearest Neighbor | Very large datasets that exceed memory |
| Variable | Default | Description |
|---|---|---|
DOCUMENTDB_CLUSTER_NAME |
(required) | DocumentDB cluster name |
AZURE_OPENAI_EMBEDDING_ENDPOINT |
(required) | Azure OpenAI endpoint |
AZURE_OPENAI_EMBEDDING_MODEL |
text-embedding-3-small |
Embedding model deployment name |
DATA_FILE_WITH_VECTORS |
data/Hotels_Vector.json |
Path to vectors JSON file |
EMBEDDED_FIELD |
DescriptionVector |
Field name containing embeddings |
EMBEDDING_DIMENSIONS |
1536 |
Vector dimensions |
AZURE_DOCUMENTDB_DATABASENAME |
Hotels |
Target database name |
QUERY_TEXT |
luxury hotel near the beach |
Search query text |
TOP_K |
5 |
Number of results per search |
CompareAll.java always runs all 9 algorithm/metric combinations. It does not read ALGORITHM or SIMILARITY environment variables.
This sample uses passwordless authentication via DefaultAzureCredential:
- DocumentDB: OIDC mechanism with Azure identity
- Azure OpenAI: Entra ID token-based auth
Ensure your identity has the appropriate RBAC roles assigned on both resources.
- Connects to DocumentDB and loads hotel data into a single
hotelscollection - Generates one embedding for the query text (reused for all searches)
- For each of the 9 algorithm/metric combinations: creates the index → searches → drops the index
- DocumentDB only allows one vector index per kind per field, so indexes are created sequentially
- Prints a formatted comparison table with scores, top results, and key insights
| Algorithm | Kind | Parameters |
|---|---|---|
| IVF | vector-ivf |
numLists=1 |
| HNSW | vector-hnsw |
m=16, efConstruction=64 |
| DiskANN | vector-diskann |
maxDegree=32, lBuild=50 |
src/main/java/com/azure/documentdb/selectalgorithm/
├── Main.java — Entry point, runs CompareAll
├── Utils.java — Shared helpers (connection, embedding, data loading)
└── CompareAll.java — Unified comparison runner (all 9 combinations)