跳到主要内容

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

ctx

Context for the current call to work.

context.Context

collSchema

Schema of the collection to create.

*entity.Schema

shardNum

Shard number of the collection to create.

The value defaults to 1. If it is left unspecified, the default value applies.

int32

opts

Extra options for the current request.

This parameter is optional. You can add multiple CreateCollectionOption in the request.

...entity.CreateCollectionOption

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

WithName(name string)

Name of collection to create.

WithDescription(description string)

Description of the collection to create.

WithAutoID(autoID bool)

Whether the primary field is automatically generated upon data insertions.

WithField(f *entity.Field)

A field in the collection to create.

Call the method multiple types to add more fields.

WithDynamicFieldEnabled(dynamicEnabled bool)

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 entity.WithEnableDynamicSchema() to create an entity.CreateCollectionOption instead.

If you assign different values to this method and entity.WithEnableDynamicSchema(), the one set to true takes precedence.

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

WithName(name string)

The field name

WithDescription(description string)

The description of the field.

WithIsPrimaryKey(isPrimaryKey bool)

Whether the field is the primary field.

WithIsAutoID(isAutoID bool)

Whether the primary field value is automatically generated upon data insertions.

If this value is different from the one specified in entity.Schema, this value takes precedence.

WithDataType(dataType entity.FieldType)

The data type of the field.

WithDim(dim int64)

The dimensionality of a vector field.

This only applies when you set dataType to a vector field type in WithDataType().

WithMaxLength(maxLen int64)

The maximum length of a VarChar field.

This only applies when you set dataType to FieldTypeVarChar in WithDataType().

WithMaxCapacity(maxCap int64)

The maximum number of elements of an ARRAY field.

This only applies when you set dataType to FieldTypeArray in WithDataType().

WithElementType(eleType entity.FieldType)

The maximum number of elements of an ARRAY field.

This only applies when you set dataType to FieldTypeArray in WithDataType().

WithIsPartitionKey(isPartitionKey bool)

Whether this field is the partition key.

WithIsDynamic(isDynamic bool)

Whether this field serves as the dynamic field.

WithTypeParams(key string, value string)

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

WithCollectionProperty(key, value string)

Collection properties, such as TTL and MMap-related settings.

WithConsistencyLevel(cl entity.ConsistencyLevel)

The consistency level of the collection. Possible options are:

  • ClStrong

  • ClBounded(default)

  • ClSession

  • ClEventually

  • ClCustomized

WithEnableDynamicSchema(enable bool)

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 WithDynamicFieldEnable(true) to the entity.NewSchema() request instead.

If you assign different values to this method and entity.WithDynamicFieldEnable(), the one set to true takes precedence.

WithPartitionNum(partitionNums int64)

The number of partitions to create along with the collection.

This is required if a field has isPartitionKey set to true in WithIsPartitionKey().

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())
}