• Rust
    • Authentication
    • CDC
    • Client
    • Collections
    • DataImport
    • Database
    • FileResources
    • Management
    • Partitions
    • ResourceGroup
    • Snapshots
    • Vector

About Milvus Rust SDK

The Milvus Rust SDK is an open-source project and its source code is hosted on GitHub.

Installation

Add the SDK to your project with Cargo:

cargo add milvus-sdk-rust@3.0.2

Quick Start

The following example connects to Milvus, creates a collection, and runs a vector search using the V2 client API, which is the current API of this SDK.

use milvus::v2::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let client = ClientV2::new(
        &ConnectConfig::new()
            .uri("http://localhost:19530")
            .token("root:Milvus"),
    )
    .await?;

    let schema = CollectionSchema::new()
        .add_field(
            FieldSchema::new()
                .name("id")
                .data_type(DataType::Int64)
                .primary_key(true),
        )
        .add_field(
            FieldSchema::new()
                .name("embedding")
                .data_type(DataType::FloatVector)
                .dimension(3),
        );
    client
        .create_collection(
            CreateCollectionRequest::builder()
                .collection_name("hello_milvus")
                .schema(schema)
                .build()?,
        )
        .await?;

    let insert_req = InsertRequest::builder()
        .collection_name("hello_milvus")
        .columns(vec![
            FieldData::Int64 {
                name: "id".into(),
                values: vec![1],
            },
            FieldData::FloatVector {
                name: "embedding".into(),
                values: vec![vec![0.1f32, 0.2, 0.3]],
            },
        ])
        .build()?;
    client.insert(insert_req).await?;

    client
        .create_index(
            CreateIndexRequest::builder()
                .collection_name("hello_milvus")
                .index_params(vec![IndexParam::new()
                    .field_name("embedding")
                    .index_type(IndexType::AutoIndex)
                    .metric_type(MetricType::Cosine)])
                .build()?,
        )
        .await?;
    client
        .load_collection(
            LoadCollectionRequest::builder()
                .collection_name("hello_milvus")
                .build()?,
        )
        .await?;

    let search_resp = client
        .search(
            SearchRequest::builder()
                .collection_name("hello_milvus")
                .vector_field("embedding")
                .vectors(SearchVectors::Float(vec![vec![0.1f32, 0.2, 0.3]]))
                .limit(1)
                .build()?,
        )
        .await?;
    println!("hit count: {}", search_resp.results().len());

    client
        .drop_collection(
            DropCollectionRequest::builder()
                .collection_name("hello_milvus")
                .build()?,
        )
        .await?;
    Ok(())
}

Compatibility

Milvus proto is backward compatible, so a later SDK version can work with an earlier Milvus server. The table lists the recommended SDK version validated for each Milvus version.

Milvus versionRecommended SDK version
2.6.xv2.6.1
3.0.xv3.0.2

Breaking Changes

3.0.1

  • use_database() is now asynchronous and validates that the target database exists. Await the call and handle a validation error when the database does not exist.
  • AddCollectionFieldRequest requires FieldSchema.nullable(true) when adding a vector field to an existing collection.
  • RunAnalyzerRequest.analyzer_params accepts an optional serde_json::Value object instead of a pre-serialized JSON string.

3.0.0

  • CompactRequest rejects target_size = 0. Omit target_size to request the server default.

Contributing

We are committed to building a collaborative, exuberant open-source community for Milvus. Therefore, contributions to the Milvus Rust SDK are welcome from everyone. Refer to the Contributing Guideline before making contributions to this project. You can file an issue if you need any assistance or want to propose your ideas.

License

Apache License 2.0

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?