Creating a collection
This topic describes how to create a collection in Milvus.
A collection consists of one or more partitions. When creating a new collection, Milvus automatically creates a default partition named _default
.
The following example creates a collection called book
with two shards. It includes a primary key field named book_id
, an INT64
scalar field named word_count
, and a two-dimensional floating point vector field named book_intro
. Actual applications may use vector dimensions higher than those in the example.
Definition of Collection Schema
The collection to be created must include a primary key field and a vector field. The primary key field supports INT64 and VarChar data types.
Tip: The schema definition of the collection is similar to defining a table structure in MYSQL.
First, prepare the necessary parameters, including field schemas, collection schemas, and collection names.
Before defining the collection schema, create a schema for each field in the collection. To simplify the complexity of data insertion, Milvus allows specifying a default value for each scalar field (except for the primary key field). This means that if a field is left empty during data insertion, the default value configured for this field during the field schema creation will be used.
var (
collectionName = "book"
)
schema := &entity.Schema{
CollectionName: collectionName,
Description: "Test book search",
Fields: []*entity.Field{
{
Name: "book_id",
DataType: entity.FieldTypeInt64,
PrimaryKey: true,
AutoID: false,
},
{
Name: "word_count",
DataType: entity.FieldTypeInt64,
PrimaryKey: false,
AutoID: false,
},
{
Name: "book_intro",
DataType: entity.FieldTypeFloatVector,
TypeParams: map[string]string{
"dim": "2",
},
},
},
EnableDynamicField: true
}
Type | Parameter | Description | Options |
---|---|---|---|
entity.Schema |
CollectionName |
Name of the collection to be created | N/A |
Description |
Description of the collection to be created | N/A | |
AutoID |
Switch used to enable or disable automatic ID (primary key) allocation | True or False |
|
Fields |
Schemas of the fields in the collection to be created. For more information, see schema | N/A | |
EnableDynamicField |
Whether to enable dynamic schema. For more information on dynamic schema, see the Dynamic Schema and Collection Management User Guide | N/A | |
entity.Field |
Name |
Name of the field to be created | N/A |
PrimaryKey |
Whether this field is a primary key. Required for primary key fields | N/A | |
AutoID |
Whether the field value is auto-incremented. Required for primary key fields | N/A | |
Description |
Description of the field | N/A | |
DataType |
Data type of the field to be created | For primary key fields: - entity.FieldTypeInt64(numpy.int64) - entity.FieldTypeVarChar(VARCHAR) For scalar fields: - entity.FieldTypeBool(Boolean) - entity.FieldTypeInt8(numpy.int8) - entity.FieldTypeInt16(numpy.int16) - entity.FieldTypeInt32(numpy.int32) - entity.FieldTypeInt64(numpy.int64) - entity.FieldTypeFloat(numpy.float32) - entity.FieldTypeDouble(numpy.double) - entity.FieldTypeVarChar(VARCHAR) For vector fields: - entity.FieldTypeBinaryVector (Binary vector) - entity.FieldTypeFloatVector (Float vector) |
|
TypeParams |
Mapping to the string that sets parameters for specific data types | N/A | |
IndexParams |
Mapping to the string that sets collection index parameters | N/A | |
IsDynamic |
Whether this field enables dynamic schema | N/A | |
IsPartitionKey |
Whether this field acts as a partition key | N/A |
Create a collection based on the defined structure
Next, create a collection using the specified schema.
err = milvusClient.CreateCollection(
context.Background(), // ctx
schema,
2, // shardNum
)
if err != nil {
log.Fatal("Failed to create collection: ", err.Error())
}
Parameter | Description | Options |
---|---|---|
ctx |
Context to control the API call process | N/A |
shardNum |
Number of shards in the collection to be created | [1,16] |
Collection Restrictions
Resource Configuration
Feature | Maximum Limit |
---|---|
Length of collection name | 255 characters |
Number of partitions in collection | 4,096 |
Number of fields in collection | 64 |
Number of shards in collection | 16 |
Parameter default_value
-
default_value
applies only to non-array and non-JSON scalar fields. -
default_value
does not apply to primary keys. - The data type of
default_value
must be the same as the data type specified indtype
, otherwise errors may occur. - If
auto_id
is used, it is not allowed to set all remaining fields to use default values. In other words, when performing insert or update operations, you need to specify a value for at least one field, otherwise errors may occur.
Check if a collection exists
collExists, err := milvusClient.HasCollection(ctx, collectionName)
if err != nil {
log.Fatal("Failed to check if the collection exists: ", err.Error())
}
Drop a collection
_ = milvusClient.DropCollection(ctx, collectionName)
Query all collections
collections, err := milvusClient.ListCollections(ctx)