milvus-logo
Star
0
Forks
0
Get Started

CreateCollection()

This method creates a collection with the specified schema.

Invocation

client.CreateCollection(ctx, collSchema, shardNum)

Parameters

ParameterDescriptionType
ctxContext to control API invocation processcontext.Context
collSchemaSchema of the collection to createPointer of entity.Schema
shardNumShard number of the collection to create.
Default value 2 will be used if it is set as 0.
INT32

A schema specifies the properties of a collection and the fields within. See Schema for more information.

Collection schema

A collection schema is the logical definition of a collection.

ParameterDescriptionType
Collection NameName of the collection to createString
DescriptionDescription of the collection to createString
AutoIDSwitch value to automatically assigns IDs to entitiesBoolean
FieldsDefines the fields in the collectionSee Field Schema of Milvus for more information.

Field schema

A field schema is the logical definition of a field.

ParameterDescriptionType
IDField ID generated when collection is createdINT64
NameName of the fieldINT64
PrimaryKeySwitch value to enable primary keyBoolean
AutoIDSwitch value to automatically assigns IDs to entitiesBoolean
DescriptionDescription of the fieldString
DataTypeData type of the field.See FieldType for more information.
TypeParamsType parameters for the field.Map of key string value string
IndexParamsIndex parameters for the field.Map of key string value string

Return

A new collection object created with the specified schema.

Errors

err: error in the creation process (if any). Possible errors are listed below:

  • ErrClientNotReady: error that the client is not connected.

  • error that collection with same name already exists.

  • error that API invocation failed.

Example

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",
      },
    },
  },
}
err = milvusClient.CreateCollection(
    context.Background(),
    schema,
    2,
)
if err != nil {
    log.Fatal("failed to create collection:", err.Error())
}