Manage schemas

Updated at:
Copy as MD

In Milvus, a schema defines how data is organized in a vector database, including field names and types. You define schemas to manage and query data for efficient search and analysis. This topic describes collection and field schemas and how to create schemas in Milvus.

Prerequisites

  • The PyMilvus library is installed on your local client and is updated to the latest version.

    To install or update the PyMilvus library, run the following command.

    pip install --upgrade pymilvus
  • A Milvus instance has been created. For more information, see Create a Milvus instance.

Overview of schemas

Before you create a collection, you typically define field schemas so that you can incorporate and apply them along with the collection schema settings when you create the collection.

Field schema

When you define a field schema in Milvus, the system currently allows only one type of field to be specified as the primary key.

Property

Description

name

The name of the field.

dtype

The data type of the field.

description

The description of the field.

is_primary

Specifies whether to use the field as the primary key. Valid values: True and False.

auto_id (primary key field)

Specifies whether to enable auto-incrementing IDs for the primary key field. Valid values: True and False.

max_length (Varchar property)

The maximum length of a Varchar field. Valid values: [1, 65535].

dim

The dimension of the vector field. The value is an integer in the range [1, 32768].

is_partition_key

Specifies whether to use the field as a partition key. Valid values: True and False.

Collection schema

A collection schema defines the structure and properties of a collection in Milvus in detail.

Property

Description

field

The fields defined in the collection.

description

The description of the collection. This parameter is optional.

partition_key_field

The name of the partition field. This parameter is optional.

enable_dynamic_field

Specifies whether to enable dynamic schema. Default value: False.

If enabled, you can insert data into fields that are not predefined in the schema.

Create a collection schema

The following code example shows how to use FieldSchema to define the properties of each field, build a CollectionSchema from these properties, and then create a Collection object. After you create the collection, you can insert data, run queries, and perform other operations.

from pymilvus import FieldSchema, CollectionSchema, connections, DataType, Collection

conn = connections.connect(
    host="c-xxx.milvus.aliyuncs.com", 
    port=19530, user="<yourUsername>", 
    password="<yourPassword>"
)

# Define the collection fields (FieldSchema).

# Primary key field: id, of type INT64, uniquely identifies each record.
id_field = FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, description="primary id")

# Age field: age, of type INT64, stores age information.
age_field = FieldSchema(name="age", dtype=DataType.INT64, description="age")

# Vector field: embedding, of type FLOAT_VECTOR with 128 dimensions, stores vector data.
embedding_field = FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=128, description="vector")

# Partition key field: position, of type VARCHAR with a maximum length of 256, partitions the data.
position_field = FieldSchema(name="position", dtype=DataType.VARCHAR, max_length=256, is_partition_key=True)

# Build the CollectionSchema from the defined fields.
schema = CollectionSchema(fields=[id_field, age_field, embedding_field, position_field],auto_id=False, enable_dynamic_field=True, description="desc of a collection")

# Define the name of the collection to create.
collection_name = "demo_1"
# Create a Collection object from the defined schema.
collection = Collection(
    name=collection_name,
    schema=schema,
    using='default',  # Specify the connection name. The default is 'default'.
    shards_num=2  # Set the number of shards for the collection. Adjust it as needed.
)

Add fields to an existing collection

Milvus Manager does not support adding fields to an existing collection.

For API version requirements and instructions, see Add fields to an existing collection.