Azure Cosmos DB Vector Store Auto-Configuration
Spring Boot auto-configuration for the Azure Cosmos DB Vector Store. This module provides zero-config setup when using Spring Boot.
Dependency
<dependency>
<groupId>com.azure.spring.ai</groupId>
<artifactId>spring-ai-autoconfigure-vector-store-azure-cosmos-db</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
This transitively brings in the spring-ai-azure-cosmos-db-store module.
Configuration Properties
Add these to your application.properties or application.yml:
spring.ai.vectorstore.cosmosdb.endpoint=https://your-account.documents.azure.com:443/
spring.ai.vectorstore.cosmosdb.databaseName=my-database
spring.ai.vectorstore.cosmosdb.containerName=my-vectors
spring.ai.vectorstore.cosmosdb.metadataFields=country,year,city
spring.ai.vectorstore.cosmosdb.vectorStoreThroughput=1000
spring.ai.vectorstore.cosmosdb.vectorDimensions=1536
spring.ai.vectorstore.cosmosdb.partitionKeyPath=/id
Authentication
Key-based (set the key property):
spring.ai.vectorstore.cosmosdb.key=your-cosmos-db-key
Azure Identity (recommended for production — omit the key property):
When no key is provided, the auto-configuration uses DefaultAzureCredential for authentication via managed identity, service principal, or other Azure credential sources.
Usage
Once configured, simply inject VectorStore:
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private VectorStore vectorStore;
💡 Tip: The auto-configuration will create the database and container if they don't exist.
Example Application
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);
@Lazy
@Autowired
private VectorStore vectorStore;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Document document1 = new Document(UUID.randomUUID().toString(),
"Sample content1", Map.of("key1", "value1"));
Document document2 = new Document(UUID.randomUUID().toString(),
"Sample content2", Map.of("key2", "value2"));
this.vectorStore.add(List.of(document1, document2));
List<Document> results = this.vectorStore.similaritySearch(
SearchRequest.builder().query("Sample content").topK(1).build());
log.info("Search results: {}", results);
this.vectorStore.delete(List.of(document1.getId(), document2.getId()));
}
}
Full Property Reference
| Property | Description | Default |
|---|---|---|
spring.ai.vectorstore.cosmosdb.endpoint | Cosmos DB endpoint URI | (required) |
spring.ai.vectorstore.cosmosdb.key | Primary or secondary key | (uses DefaultAzureCredential if absent) |
spring.ai.vectorstore.cosmosdb.databaseName | Database name | (required) |
spring.ai.vectorstore.cosmosdb.containerName | Container name | (required) |
spring.ai.vectorstore.cosmosdb.partitionKeyPath | Partition key path | /id |
spring.ai.vectorstore.cosmosdb.metadataFields | Metadata fields for filtering | (empty) |
spring.ai.vectorstore.cosmosdb.vectorStoreThroughput | Container throughput (RU/s) | 400 |
spring.ai.vectorstore.cosmosdb.vectorDimensions | Vector dimensions | 1536 |
Manual Configuration
If you prefer to configure the vector store manually (without auto-configuration), see the Vector Store page.