Class MilvusStore
- All Implemented Interfaces:
VDBStoreBase,AutoCloseable
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 ClassesModifier and TypeClassDescriptionstatic classBuilder for creating MilvusStore instances with fluent configuration. -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>Adds multiple documents to the store.static MilvusStore.Builderbuilder()Creates a new builder for constructing MilvusStore instances.voidclose()Closes the Milvus client and releases all resources.static MilvusStoreDeprecated, for removal: This API element is subject to removal in a future version.reactor.core.publisher.Mono<Boolean>Deletes a document from the store (optional).io.milvus.v2.client.MilvusClientV2Gets the underlying Milvus client for advanced operations.Gets the collection name.Gets the database name.intGets the vector dimensions.io.milvus.v2.common.IndexParam.MetricTypeGets the metric type used for similarity search.getUri()Gets the Milvus server URI.booleanisClosed()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.
-
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.Usebuilder()insteadCreates a new MilvusStore with minimal configuration.- Parameters:
uri- the Milvus server URI (e.g., "http://localhost:19530")collectionName- the name of the collection to usedimensions- the dimension of vectors that will be stored- Returns:
- a new MilvusStore instance
- Throws:
VectorStoreException- if initialization fails
-
add
Description copied from interface:VDBStoreBaseAdds 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:
addin interfaceVDBStoreBase- 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:VDBStoreBaseSearches 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:
searchin interfaceVDBStoreBase- Parameters:
searchDocumentDto- the search document DTO- Returns:
- a Mono that emits a list of Document objects with scores set, sorted by similarity (descending)
-
delete
Description copied from interface:VDBStoreBaseDeletes a document from the store (optional).Default implementation throws UnsupportedOperationException. Vector stores that support deletion should override this method.
- Specified by:
deletein interfaceVDBStoreBase- Parameters:
docId- the document ID (UUID string) to delete- Returns:
- a Mono that emits true if the deletion was successful, false otherwise
-
getClient
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
Gets the Milvus server URI.- Returns:
- the URI
-
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
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:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed()Checks if this store has been closed.- Returns:
- true if the store has been closed, false otherwise
-
builder
Creates a new builder for constructing MilvusStore instances.- Returns:
- a new builder instance
-
builder()instead