Create a Collection or Partition

This page will show you how to create a collection or a partition in Milvus.

Create a collection

Collections can only be created after successfully connecting to the Milvus server.

The created collection must contain a primary key field. Int64 is the only supported data type for the primary key field for now.

Parameters marked with * are specific to Python SDK, and those marked with ** are specific to Node.js SDK.

  1. Prepare collection parameters, including collection name and field parameters. Refer to API documents for respective languages for a detailed description of these parameters.
collection_name = "example_collection"
field_name = "example_field"
from pymilvus import Collection, CollectionSchema, FieldSchema, DataType
pk = FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=True)
field = FieldSchema(name=field_name, dtype=DataType.FLOAT_VECTOR, dim=8)
schema = CollectionSchema(fields=[pk,field], description="example collection")
const COLLECTION_NAME = "example_collection";
const FIELD_NAME = "example_field";

const params = {
  collection_name: COLLECTION_NAME,
  fields: [
    {
      name: FIELD_NAME,
      description: "vector field",
      data_type: DataType.FloatVector,

      type_params: {
        dim: "8",
      },
    },
    {
      name: "age",
      data_type: DataType.Int64,
      autoID: true,
      is_primary_key: true,
      description: "",
    },
  ],
};
Detailed Description
Parameter Description Note
collection_name Name of the collection to create Data type: String
field_name Name of the field in the collection Data type: String
Schema Schema used to create a collection and the fields within. Refer to field schema and collection schema for detailed description.  
description Description of the collection Data type: String
  1. Create a collection:
>>> collection = Collection(name=collection_name, schema=schema, using='default', shards_num=2)

# Get an existing collection by its name.
collection=Collection(name=collection_name)
await milvusClient.collectionManager.createCollection(params);
Detailed Description
Parameter Description Note
using* By specifying the srever alias here, you can decide in which Milvus server you create a collection. Optional
shards_num* Number of the shards for the collection to create Optional
  1. Check if the collection is created successfully:
>>> import pymilvus
>>> pymilvus.utility.get_connection().has_collection(collection_name)
True
await milvusClient.collectionManager.hasCollection({
  collection_name: COLLECTION_NAME,
});
  1. List all created collections:
>>> pymilvus.utility.get_connection().list_collections()
['example_collection']
await milvusClient.collectionManager.showCollections();
  1. View collection statistics, such as row count:
>>> collection.num_entities
0
await milvusClient.collectionManager.getCollectionStatistics({
  collection_name: COLLECTION_NAME,
});

Create a partition

Search performance worsens as more vectors are inserted into the collection. To help mitigate declining search performance, consider creating collection partitions. Partitioning is a way to separate data. Partition names narrow a search to a specific number of vectors, improving query performance. To improve search efficiency, divide a collection into several partitions by name.

>>> partition_name = "example_partition"
>>> partition = collection.create_partition(partition_name)
await milvusClient.partitionManager.createPartition({
  collection_name: COLLECTION_NAME,
  partition_name: "example_partition",
});
Detailed Description
Parameter Description Note
partition_name Name of the partition to create Data type: String

Milvus creates a default partition name, _default, for new collections. After creating a partition, you have two partition names, example_partition and _default.

List all partitions in a collection:

>>> collection.partitions
[{"name": "_default", "description": "", "num_entities": 0}, {"name": "example_partition", "description": "", "num_entities": 0}]
await milvusClient.partitionManager.showPartitions({
  collection_name: COLLECTION_NAME,
});

Check if a partition is successfully created:

>>> collection.has_partition(partition_name)
True
await milvusClient.partitionManager.hasPartition({
  collection_name: COLLECTION_NAME,
  partition_name: "example_partition",
});
Is this page helpful?
Scored Successfully!