Manage Collections

This topic describes how to manage collections in Milvus.

A collection consists of one or more partitions. While creating a new collection, Milvus creates a default partition _default. See Glossary - Collection for more information.

The following example is based on a two-shard collection named example_collection with an eight-dimension float vector field, and an INT64, auto_id enabled primary key field.

Create a collection

  • You can create collections only after connecting to Milvus server.
  • The collection to create must contain a primary key field. INT64 is the only supported data type for the primary key field in current release of Milvus.

First, prepare necessary parameters, including field schema, collection schema, and collection name.

from pymilvus import CollectionSchema, FieldSchema, DataType
pk = FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=True)
field = FieldSchema(name="example_field", dtype=DataType.FLOAT_VECTOR, dim=8)
schema = CollectionSchema(fields=[pk,field], description="example collection")
collection_name = "example_collection"
const params = {
  collection_name: "example_collection",
  description: "example collection",
  fields: [
    {
      name: "example_field",
      description: "",
      data_type: 101,     // DataType.FloatVector
      type_params: {
        dim: "8",
      },
    },
    {
      name: "pk",
      data_type: 5,       // DataType.Int64
      autoID: true,
      is_primary_key: true,
      description: "",
    },
  ],
};
Parameter Description
FieldSchema Schema of the fields within the collection to create. Refer to Field Schema for more information.
CollectionSchema Schema of the collection to create. Refer to Collection Schema for more information.
collection_name Name of the collection to create.
Parameter Description
collection_name Name of the collection to create.
description Description of the collection to create.
fields Schema of the filed and the collection to create. Refer to Field Schema and Collection Schema for more information.
data_type Data type of the filed to create. Refer to data type reference number for more information.

Then, create a collection with the parameters you created above.

from pymilvus import Collection
collection = Collection(name=collection_name, schema=schema, using='default', shards_num=2)
await milvusClient.collectionManager.createCollection(params);
Parameter Description
using (optional) By specifying the server alias here, you can choose in which Milvus server you create a collection.
shards_num (optional) Number of the shards for the collection to create.

Verify if a collection exists

from pymilvus import utility
utility.has_collection("example_collection")
await milvusClient.collectionManager.hasCollection({
  collection_name: "example_collection",
});

List all collections

from pymilvus import utility
utility.list_collections()
await milvusClient.collectionManager.showCollections();

View collection statistics

from pymilvus import Collection
collection = Collection("example_collection")      # Get an existing collection.
collection.num_entities
await milvusClient.collectionManager.getCollectionStatistics({  collection_name: "example_collection",});

Load a collection

All CRUD operations within Milvus are executed in memory. Load the collection to memory before searching or deleting data.

from pymilvus import Collection
collection = Collection("example_collection")      # Get an existing collection.
collection.load()
await milvusClient.collectionManager.loadCollection({
  collection_name: "example_collection",
});
Parameter Description
partition_name (optional) Name of the partition to load.
Parameter Description
collection_name Name of the collection to load.

Drop a collection

Remove a collection and the data within.

The drop operation is irreversible. Dropping a collection deletes all data within it.
from pymilvus import Collection
collection = Collection("example_collection")      # Get an existing collection.
collection.drop()
await milvusClient.collectionManager.dropCollection({  collection_name: "example_collection",});

What's next

该页面是否对你有帮助?
评价成功!