Class MilvusStore

java.lang.Object
io.agentscope.core.rag.store.MilvusStore
All Implemented Interfaces:
VDBStoreBase, AutoCloseable

public class MilvusStore extends Object implements VDBStoreBase, AutoCloseable
Milvus vector database store implementation.

This class provides an interface for storing and searching vectors using Milvus, a cloud-native vector database. It implements the VDBStoreBase interface to provide a unified API for vector storage operations.

In Milvus, we use dynamic fields to store the metadata, including the document ID, chunk ID, and original content.

This implementation uses the Milvus Java SDK v2 (io.milvus:milvus-sdk-java). To use this class, add the following dependency:


 <dependency>
     <groupId>io.milvus</groupId>
     <artifactId>milvus-sdk-java</artifactId>
     <version>2.6.16</version>
 </dependency>
 

Example usage:


 // Using builder with authentication and timeout options
 try (MilvusStore store = MilvusStore.builder()
         .uri("http://localhost:19530")
         .databaseName("my_database")
         .collectionName("my_collection")
         .dimensions(1024)
         .token("root:Milvus")
         .connectTimeoutMs(30000)
         .build()) {
     store.add(document).block();
     List<Document> results = store.search(queryEmbedding, 5).block();
     // Store is automatically closed here
 }
 

Authentication options:

  • Token-based: Use token("username:password") format
  • Username/Password: Use username() and password() methods
  • No auth: Leave token/username/password empty for local instances

Note: The uri parameter should be a full HTTP URL including protocol and port, for example: "http://localhost:19530" or "https://milvus.example.com:19530"

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for creating MilvusStore instances with fluent configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Mono<Void>
    add(List<io.agentscope.core.rag.model.Document> documents)
    Adds multiple documents to the store.
    Creates a new builder for constructing MilvusStore instances.
    void
    Closes the Milvus client and releases all resources.
    create(String uri, String collectionName, int dimensions)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use builder() instead
    reactor.core.publisher.Mono<Boolean>
    delete(String docId)
    Deletes a document from the store (optional).
    io.milvus.v2.client.MilvusClientV2
    Gets the underlying Milvus client for advanced operations.
    Gets the collection name.
    Gets the database name.
    int
    Gets the vector dimensions.
    io.milvus.v2.common.IndexParam.MetricType
    Gets the metric type used for similarity search.
    Gets the Milvus server URI.
    boolean
    Checks if this store has been closed.
    reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>>
    search(SearchDocumentDto searchDocumentDto)
    Searches for similar documents.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      @Deprecated(since="1.0.11", forRemoval=true) public static MilvusStore create(String uri, String collectionName, int dimensions) throws VectorStoreException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder() instead
      Creates a new MilvusStore with minimal configuration.
      Parameters:
      uri - the Milvus server URI (e.g., "http://localhost:19530")
      collectionName - the name of the collection to use
      dimensions - the dimension of vectors that will be stored
      Returns:
      a new MilvusStore instance
      Throws:
      VectorStoreException - if initialization fails
    • add

      public reactor.core.publisher.Mono<Void> add(List<io.agentscope.core.rag.model.Document> documents)
      Description copied from interface: VDBStoreBase
      Adds multiple documents to the store.

      All documents must have embedding vectors set. The store will persist both the vectors and the document metadata/payloads.

      This method supports batch operations for efficiency. Implementations should handle batch insertion optimally.

      Specified by:
      add in interface VDBStoreBase
      Parameters:
      documents - the list of documents to store (all must have embeddings set)
      Returns:
      a Mono that completes when all documents are stored
    • search

      public reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>> search(SearchDocumentDto searchDocumentDto)
      Description copied from interface: VDBStoreBase
      Searches for similar documents.

      This method performs vector similarity search and returns the most similar documents. Results include the document metadata and similarity scores. Optionally, embeddings may also be included in the results (implementation-dependent).

      Specified by:
      search in interface VDBStoreBase
      Parameters:
      searchDocumentDto - the search document DTO
      Returns:
      a Mono that emits a list of Document objects with scores set, sorted by similarity (descending)
    • delete

      public reactor.core.publisher.Mono<Boolean> delete(String docId)
      Description copied from interface: VDBStoreBase
      Deletes a document from the store (optional).

      Default implementation throws UnsupportedOperationException. Vector stores that support deletion should override this method.

      Specified by:
      delete in interface VDBStoreBase
      Parameters:
      docId - the document ID (UUID string) to delete
      Returns:
      a Mono that emits true if the deletion was successful, false otherwise
    • getClient

      public io.milvus.v2.client.MilvusClientV2 getClient() throws VectorStoreException
      Gets the underlying Milvus client for advanced operations.

      This method provides access to the Milvus client for users who need to perform operations beyond the standard VDBStoreBase interface, such as:

      • Custom index configuration
      • Advanced filtering and faceting
      • Collection management
      • Batch operations
      Returns:
      the MilvusClientV2 instance
      Throws:
      VectorStoreException - if the store has been closed
    • getUri

      public String getUri()
      Gets the Milvus server URI.
      Returns:
      the URI
    • getCollectionName

      public String getCollectionName()
      Gets the collection name.
      Returns:
      the collection name
    • getDimensions

      public int getDimensions()
      Gets the vector dimensions.
      Returns:
      the dimensions
    • getMetricType

      public io.milvus.v2.common.IndexParam.MetricType getMetricType()
      Gets the metric type used for similarity search.
      Returns:
      the metric type
    • getDatabaseName

      public String getDatabaseName()
      Gets the database name.
      Returns:
      the database name
    • close

      public void close()
      Closes the Milvus client and releases all resources.

      This method closes the MilvusClientV2, releasing all connections and associated resources. This method is idempotent and can be called multiple times safely.

      After closing, all operations on this store will fail with a VectorStoreException. It's recommended to use try-with-resources for automatic resource management:

      
       try (MilvusStore store = MilvusStore.builder()
               .uri("http://localhost:19530")
               .collectionName("collection")
               .dimensions(1024)
               .build()) {
           store.add(documents).block();
           // Store is automatically closed here
       }
       
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Checks if this store has been closed.
      Returns:
      true if the store has been closed, false otherwise
    • builder

      public static MilvusStore.Builder builder()
      Creates a new builder for constructing MilvusStore instances.
      Returns:
      a new builder instance