Code Examples
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Code Examples within the Milvus Rust SDK provide developers with practical implementation patterns for interacting with a Milvus vector database. These examples cover the full lifecycle of data management, including client initialization, authentication, database and collection administration, indexing, and high-performance vector searching.
The examples serve as both a functional verification tool and a reference architecture for building Rust applications that require low-latency vector retrieval. They demonstrate the use of asynchronous programming via tokio and the SDK's type-safe builders for schemas and options. Sources: README.md, examples/collection.rs, examples/query_search.rs
Client Connection and Authentication
The foundational step for any operation is establishing a secure connection to the Milvus server. The SDK supports basic authentication and provides a ClientBuilder for configuring credentials and connection parameters.
Connection Workflow
The following diagram illustrates the sequence for establishing an authenticated client session.
Authentication Management
Developers can programmatically manage users, roles, and privilege groups. User passwords are Base64 encoded before transmission to the server. Sources: src/authentication.rs:69-70, examples/authentication.rs:24-30
| Operation | Function | Description |
|---|---|---|
| Create User | create_user | Registers a new username and password. |
| Role Assignment | grant_role | Links a specific role to a user account. |
| Privilege Control | grant_privilege_v2 | Assigns specific permissions (e.g., "Load") to a role. |
| Group Management | create_privilege_group | Groups related privileges for easier administration. |
Sources: src/authentication.rs:68, src/authentication.rs:222, src/authentication.rs:596, examples/authentication.rs:88
Collection and Schema Management
Collections in Milvus require a defined CollectionSchema, which is constructed using the CollectionSchemaBuilder. This includes defining primary keys, vector dimensions, and optional dynamic fields.
Data Insertion and Synchronization
Entities are inserted using FieldColumn structures. To ensure data visibility for queries, the flush operation is used to synchronize the write-ahead log with the data storage. Sources: examples/collection.rs:54-58, examples/query_search.rs:136-138
Indexing and Vector Search
Indexing is critical for performance. The SDK allows defining IndexParams such as IndexType (e.g., IvfFlat) and MetricType (e.g., L2).
Indexing Flow
- Creation: Call
create_indexon a specific vector field. - Loading: Use
load_collectionto move the indexed data into Query Nodes for searching. - Execution: Perform
searchorqueryoperations.
Searching vs. Querying
The SDK distinguishes between similarity-based searching and expression-based querying.
- Search: Uses a query vector to find the nearest neighbors based on a metric. Sources: examples/query_search.rs:65-81
- Query: Uses a boolean expression (e.g.,
"age > 20") to filter entities. Sources: examples/query_search.rs:37-43 - Get: Retrieves specific entities by their primary IDs. Sources: examples/query_search.rs:94-106
Database Administration
The SDK supports multi-tenancy through database management. A client can switch its active context using using_database.
| Feature | Method | Key Options |
|---|---|---|
| Database Creation | create_database | replica_number, diskquota_mb, max_collections |
| Context Switching | using_database | Automatically clears schema cache and updates interceptors. |
| Resource Allocation | alter_database_properties | Modifies existing DB constraints. |
Sources: src/database.rs:77-130, src/database.rs:414-437
Conclusion
These code examples demonstrate the SDK's capability to handle complex vector database workflows with Rust's safety and concurrency features. By following the patterns for schema definition, authenticated connection, and indexed searching, developers can build robust applications integrated with Milvus. Sources: examples/collection.rs, examples/authentication.rs, tests/aggressivehpctesting/aggressivetesting.md