跳到主要内容

CreateIndex()

This method creates an index for a specific field in a collection.

func (c *GrpcClient) CreateIndex(ctx context.Context, collName string, fieldName string, idx entity.Index, async bool, opts ...IndexOption) error

Parameter

Description

Type

ctx

Context for the current call to work.

context.Context

collName

Name of a collection

string

fieldName

Name of the field to index.

string

idx

Index settings.

entity.Index

async

Whether this operation is asynchronous.

bool

opts

Extra index settings

You can include multiple entity.IndexOption in this request.

client.IndexOption

entity.Index

This interface type defines a set of method signatures as follows.

type Index interface {
Name() string
IndexType() IndexType
Params() map[string]string
}

Method Signature

Return Type

Description

Name()

string

Return the index name.

IndexType()

entity.IndexType

Return the index type.

Params()

map[string]string

Return the index parameters.

For details on the struct types that implement the above method signatures and applicable index types, refer to Indexes.

client.IndexOption

This type provides methods to modify index settings when you create, describe, or drop indexes. The following methods return client.IndexOption.

Method

Description

WithIndexName(name string)

Name of the index to create.

The value defaults to the target field name.

WithMmap(enabled bool)

Whether to enable MMap for the index.

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 field with the same name does not exist.

  • The call to this API fails.

Example

// create index
idxHnsw, errIdx := entity.NewIndexHNSW(entity.COSINE, 8, 200)
if errIdx != nil {
log.Fatal("failed to new index:", errIdx.Error())
}
errIndex := mc.CreateIndex(context.Background(), collectionName, "vector", idxHnsw, false, client.WithIndexName("myIdx"))
if errIndex != nil {
log.Fatal("failed to create index:", errIndex.Error())
}