Class MilvusVectorStore

java.lang.Object
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
org.springframework.ai.vectorstore.milvus.MilvusVectorStore
All Implemented Interfaces:
Consumer<List<Document>>, DocumentWriter, VectorStore, org.springframework.beans.factory.InitializingBean

public class MilvusVectorStore extends AbstractObservationVectorStore implements org.springframework.beans.factory.InitializingBean
Milvus implementation of the VectorStore interface. This implementation supports storing and searching document embeddings using Milvus, an open-source vector database optimized for similarity search and AI applications.

Key features include:

  • Support for different similarity metrics (Cosine, L2, Inner Product)
  • Configurable index types for performance optimization
  • Metadata filtering capabilities
  • Automatic schema initialization
  • Batching strategy support for efficient operations

Example usage:


 // Create a basic Milvus vector store
 MilvusVectorStore vectorStore = MilvusVectorStore.builder(milvusServiceClient, embeddingModel)
     .initializeSchema(true)
     .build();

 // Create a customized Milvus vector store
 MilvusVectorStore customVectorStore = MilvusVectorStore.builder(milvusServiceClient, embeddingModel)
     .databaseName("my_database")
     .collectionName("my_collection")
     .metricType(MetricType.COSINE)
     .indexType(IndexType.IVF_FLAT)
     .indexParameters("{\"nlist\":1024}")
     .embeddingDimension(1536)
     .batchingStrategy(new TokenCountBatchingStrategy())
     .initializeSchema(true)
     .build();

 // Add documents to the store
 List<Document> documents = List.of(
     new Document("content1", Map.of("meta1", "value1")),
     new Document("content2", Map.of("meta2", "value2"))
 );
 vectorStore.add(documents);

 // Perform similarity search
 List<Document> results = vectorStore.similaritySearch(
     SearchRequest.query("search text")
         .withTopK(5)
         .withSimilarityThreshold(0.7)
         .withFilterExpression("meta1 == 'value1'")
 );
 

The vector store supports various configuration options through its builder:

  • milvusClient: Required Milvus service client for database operations
  • embeddingModel: Required model for generating embeddings
  • metricType: Similarity metric (COSINE, L2, IP)
  • indexType: Type of index for search optimization
  • databaseName: Name of the Milvus database (default: "default")
  • collectionName: Name of the collection (default: "vector_store")
  • initializeSchema: Whether to automatically create the schema
Author:
Christian Tzolov, Soby Chacko, Thomas Vitale, Ilayaperumal Gopinathan
See Also: