CreateCollection()
This method creates a collection with the specified schema.
func (c *GrpcClient) CreateCollection(ctx context.Context, collSchema *entity.Schema, shardNum int32, opts ...CreateCollectionOption) error
Request Parameters
Parameter | Description | Type |
---|---|---|
| Context for the current call to work. |
|
| Schema of the collection to create. |
|
| Shard number of the collection to create. The value defaults to |
|
| Extra options for the current request. This parameter is optional. You can add multiple |
|
A schema specifies the properties of a collection and the fields within it. For details, refer to Schema for more information.
entity.Schema
You can create a schema using the entity.NewSchema()
method as follows:
schema := entity.NewSchema().
WithName(). // CollectionName
WithDescription(). // Description
WithAutoID(). // AutoID
WithField(). // Field
WithDynamicFieldEnabled() // EnableDynamicField
Method | Description |
---|---|
| Name of collection to create. |
| Description of the collection to create. |
| Whether the primary field is automatically generated upon data insertions. |
| A field in the collection to create. Call the method multiple types to add more fields. |
| Whether to enable the dynamic field for non-schema-defined fields. Once enabled, non-schema-defined fields and their values are saved in the reserved JSON field named $meta. As an alternative, you can use If you assign different values to this method and |
entity.Field
You can create a field using the entity.NewField()
method as follows:
field := entity.NewField().
WithName().
WithDescription().
WithIsPrimaryKey().
WithIsAutoID().
WithDataType().
WithDim().
WithMaxLength().
WithMaxCapacity().
WithElementType().
WithIsPartitionKey().
WithIsDynamic()
Method | Description |
---|---|
| The field name |
| The description of the field. |
| Whether the field is the primary field. |
| Whether the primary field value is automatically generated upon data insertions. If this value is different from the one specified in |
| The data type of the field. |
| The dimensionality of a vector field. This only applies when you set |
| The maximum length of a VarChar field. This only applies when you set |
| The maximum number of elements of an ARRAY field. This only applies when you set |
| The maximum number of elements of an ARRAY field. This only applies when you set |
| Whether this field is the partition key. |
| Whether this field serves as the dynamic field. |
| Additional properties of the specified data type. |
entity.FieldType
const (
// FieldTypeNone zero value place holder
FieldTypeNone FieldType = 0 // zero value place holder
// FieldTypeBool field type boolean
FieldTypeBool FieldType = 1
// FieldTypeInt8 field type int8
FieldTypeInt8 FieldType = 2
// FieldTypeInt16 field type int16
FieldTypeInt16 FieldType = 3
// FieldTypeInt32 field type int32
FieldTypeInt32 FieldType = 4
// FieldTypeInt64 field type int64
FieldTypeInt64 FieldType = 5
// FieldTypeFloat field type float
FieldTypeFloat FieldType = 10
// FieldTypeDouble field type double
FieldTypeDouble FieldType = 11
// FieldTypeString field type string
FieldTypeString FieldType = 20
// FieldTypeVarChar field type varchar
FieldTypeVarChar FieldType = 21 // variable-length strings with a specified maximum length
// FieldTypeArray field type Array
FieldTypeArray FieldType = 22
// FieldTypeJSON field type JSON
FieldTypeJSON FieldType = 23
// FieldTypeBinaryVector field type binary vector
FieldTypeBinaryVector FieldType = 100
// FieldTypeFloatVector field type float vector
FieldTypeFloatVector FieldType = 101
// FieldTypeBinaryVector field type float16 vector
FieldTypeFloat16Vector FieldType = 102
// FieldTypeBinaryVector field type bf16 vector
FieldTypeBFloat16Vector FieldType = 103
// FieldTypeBinaryVector field type sparse vector
FieldTypeSparseVector FieldType = 104
)
entity.CreateCollectionOption
You can add extra collection settings to the CreateCollection()
request using the following methods.
Method | Description |
---|---|
| Collection properties, such as TTL and MMap-related settings. |
| The consistency level of the collection. Possible options are:
|
| Whether to enable the dynamic field for non-schema-defined fields. Once enabled, non-schema-defined fields and their values are saved in the reserved JSON field named $meta. As an alternative, you can append If you assign different values to this method and |
| The number of partitions to create along with the collection. This is required if a field has |
Return
Null
Errors
Any error in the execution of the request. Possible errors are as follows:
-
ErrClientNotReady
: The client is not connected to Milvus. -
A collection with the same name already exists.
-
The call to this API fails.
Example
- default
var collectionName = "test_01"
pkField := entity.NewField().WithName("id").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)
varcharField := entity.NewField().WithName("varchar").WithDataType(entity.FieldTypeVarChar).WithTypeParams(entity.TypeParamMaxLength, "100")
vecField := entity.NewField().WithName("vector").WithDataType(entity.FieldTypeFloatVector).WithDim(768)
schema := entity.NewSchema().WithName(collectionName).WithField(pkField).WithField(varcharField).WithField(vecField)
errCreate := mc.CreateCollection(context.Background(), schema, 1, client.WithConsistencyLevel(entity.ClBounded))
if errCreate != nil {
log.Fatal("failed to create collection:", errCreate.Error())
}
- Other vectors
var collectionName = "test_02"
pkField := entity.NewField().WithName("id").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)
binaryField := entity.NewField().WithName("binary").WithDataType(entity.FieldTypeBinaryVector).WithDim(768)
fp16Field := entity.NewField().WithName("fp16").WithDataType(entity.FieldTypeFloat16Vector).WithDim(768)
bf16Field := entity.NewField().WithName("bf16").WithDataType(entity.FieldTypeBFloat16Vector).WithDim(768)
sparseField := entity.NewField().WithName("sparse").WithDataType(entity.FieldTypeSparseVector)
schema := entity.NewSchema().WithName(collectionName).WithField(pkField).WithField(binaryField).WithField(fp16Field).WithField(bf16Field).WithField(sparseField)
errCreate := mc.CreateCollection(context.Background(), schema, 1, client.WithConsistencyLevel(entity.ClBounded))
if errCreate != nil {
log.Fatal("failed to create collection:", errCreate.Error())
}