Schema Definition
Relevant source files
The following files were used as context for generating this wiki page:
Schema Definition in the Milvus Rust SDK provides the structural foundation for data organization within a Milvus cluster. It defines how collections are composed of fields, specifying data types, constraints, and vector dimensions required for efficient similarity searches and attribute filtering. The schema acts as a contract between the client application and the Milvus server, ensuring data integrity during insertion and retrieval operations.
The architecture relies on a builder pattern to construct complex collection structures, which are then serialized into Protocol Buffer formats for communication with the Milvus service. Key components include CollectionSchema, which aggregates metadata about the collection, and FieldSchema, which defines individual attributes like primary keys, vector embeddings, and scalar values.
Core Components
Field Schema
Individual fields are defined using the FieldSchema struct. Each field requires a name and a DataType. Fields can be designated as primary keys, which must be of type Int64 or VarChar. For vector fields, specific parameters like dimensions (dim) are mandatory to define the size of the embedding.
Collection Schema
The CollectionSchema aggregates a set of FieldSchema objects. It defines the collection's identity, including its name and description. It also controls global collection behaviors such as enable_dynamic_field, which allows the storage of fields not explicitly defined in the schema.
| Property | Type | Description |
|---|---|---|
name | String | Unique identifier for the collection. |
description | String | Human-readable details about the collection. |
fields | Vec<FieldSchema> | The set of defined attributes for each entity. |
enable_dynamic_field | bool | Whether to allow undefined fields in data ingestion. |
Sources: src/schema.rs:356-361, src/proto/milvus.proto.schema.rs:77-90
Schema Construction via Builder
The SDK utilizes a CollectionSchemaBuilder to facilitate the programmatic creation of schemas. This builder manages the validation of schema constraints, such as ensuring exactly one primary key is defined and verifying that auto_id is only enabled for supported data types.
Construction Flow
- Initialize the builder with a collection name.
- Add scalar and vector fields using specific constructor methods (e.g.,
new_float_vector). - Define the primary key and optional
auto_id. - Call
build()to perform final validation and generate theCollectionSchema.
Data Types and Mapping
The SDK supports a variety of scalar and vector types, mapped from internal Rust representations to Milvus Protocol Buffer types.
| Rust Helper Method | Milvus DataType | Constraints |
|---|---|---|
new_int64 | Int64 | 8-byte integer. |
new_varchar | VarChar | Requires max_length > 0. |
new_float_vector | FloatVector | Requires dim > 0. |
new_binary_vector | BinaryVector | Requires dim to be multiple of 8. |
Sources: src/schema.rs:271-318, src/proto/milvus.proto.schema.rs:370-394
Protocol Buffer Serialization
When a collection is created via the Client, the SDK converts the internal CollectionSchema into a serialized byte array using the prost library. This is sent to the Milvus server within a CreateCollectionRequest.
impl From<CollectionSchema> for schema::CollectionSchema {
fn from(col: CollectionSchema) -> Self {
schema::CollectionSchema {
name: col.name.to_string(),
auto_id: col.auto_id(),
description: col.description,
fields: col.fields.into_iter().map(Into::into).collect(),
enable_dynamic_field: col.enable_dynamic_field,
// ... other fields
}
}
}
Sources: src/schema.rs:410-423, src/proto/milvus.proto.milvus.rs:96-103
Schema Validation Rules
The SDK enforces several rules during the building and usage of schemas:
- Primary Key Requirement: Every collection MUST have a primary key field. src/schema.rs:518-520
- Type Constraints: Primary keys must be either
Int64orVarChar. src/schema.rs:467-471 - Auto-ID Compatibility:
auto_idis only supported forInt64primary keys. src/schema.rs:487-493 - Vector Field Check: Operations like
create_indexverify that the target field is aFloatVectororBinaryVector. src/schema.rs:392-404
Summary
Schema Definition is the structural blueprint for Milvus collections in the Rust SDK. By using FieldSchema and CollectionSchemaBuilder, developers can define rigorous data models that support both structured metadata and high-dimensional vector embeddings. The SDK ensures these definitions are validated locally before being transmitted to the Milvus server, reducing runtime errors and ensuring consistency across the database.