Manage schema
In Milvus, a schema defines the data structure of a vector database, including field names and data types. You manage and query data by defining a schema to support efficient search and analysis operations. This topic describes the schema definitions for collections and fields, and how to create a schema 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 pymilvusA Milvus instance has been created. For more information, see Create a Milvus instance.
Schema overview
Before you create a collection, you typically define the field schemas in advance so that these field schemas and the collection schema settings can be combined and applied when you build the collection.
Field schema
When you define a field schema in Milvus, the system currently allows only one field to be specified as the primary key.
Property | Description |
name | The name of the field. |
dtype | The data type of the field. |
description | A description of the field. |
is_primary | Whether to set the field as the primary key. Valid values: True, False. |
auto_id (for primary key field) | Whether to enable auto-incrementing IDs for the primary key field. Valid values: True, False. |
max_length (for VARCHAR fields) | The maximum length of a |
dim | The dimensions of a vector field. This is an integer value, with valid values ranging from [1, 32768]. |
is_partition_key | Whether to use the field as a partition key. Valid values: True, False. |
Collection schema
A collection schema is a detailed definition of the structure and characteristics of a collection in Milvus.
Property | Description |
field | The fields defined in the collection. |
description | A description of the collection. This parameter is optional. |
partition_key_field | The name of the partition field. This parameter is optional. |
enable_dynamic_field | Whether to enable the dynamic schema property. The default value is False. If enabled, you can insert data into fields that are not predefined in the schema. |
Create a collection schema
The following code sample shows how to use FieldSchema to define the properties of each field, then build a CollectionSchema based on these properties, and finally create a Collection object. After the collection is created, you can perform subsequent operations such as data insertion and queries.
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, used to uniquely identify each record.
id_field = FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, description="primary id")
# Age field: age, of type INT64, used to store age information.
age_field = FieldSchema(name="age", dtype=DataType.INT64, description="age")
# Vector field: embedding, of type FLOAT_VECTOR, with a dimension of 128, used to store 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, used for data partitioning.
position_field = FieldSchema(name="position", dtype=DataType.VARCHAR, max_length=256, is_partition_key=True)
# Build a CollectionSchema using 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 using the defined schema.
collection = Collection(
name=collection_name,
schema=schema,
using='default', # Specify the connection name to use. The default is 'default'.
shards_num=2 # Set the number of shards for the collection. Adjust it based on your actual requirements.
)