Skip to content

Milvus

Since v0.29.0

Introduction

The Testcontainers module for Milvus.

Adding this module to your project dependencies

Please run the following command to add the Milvus module to your Go dependencies:

go get github.com/testcontainers/testcontainers-go/modules/milvus

Usage example

ctx := context.Background()

milvusContainer, err := milvus.Run(ctx, "milvusdb/milvus:v2.3.9")
defer func() {
    if err := testcontainers.TerminateContainer(milvusContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

Module Reference

Run function

Info

The RunContainer(ctx, opts...) function is deprecated and will be removed in the next major release of Testcontainers for Go.

The Milvus module exposes one entrypoint function to create the Milvus container, and this function receives three parameters:

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*MilvusContainer, error)
  • context.Context, the Go context.
  • string, the Docker image to use.
  • testcontainers.ContainerCustomizer, a variadic argument for passing options.

Image

Use the second argument in the Run function to set a valid Docker image. In example: Run(context.Background(), "milvusdb/milvus:v2.3.9").

Container Options

When starting the Milvus container, you can pass options in a variadic way to configure it.

The following options are exposed by the testcontainers package.

Basic Options

Lifecycle Options

Files & Mounts Options

Build Options

Logging Options

Image Options

Networking Options

Advanced Options

Experimental Options

Container Methods

The Milvus container exposes the following methods:

ConnectionString

This method returns the connection string to connect to the Milvus container, using the default 19530 port.

connectionStr, err := ctr.ConnectionString(ctx)

Examples

Creating collections

This example shows the usage of the Milvus module to create and retrieve collections.

ctx := context.Background()

milvusContainer, err := milvus.Run(ctx, "milvusdb/milvus:v2.3.9")
defer func() {
    if err := testcontainers.TerminateContainer(milvusContainer); err != nil {
        log.Printf("failed to terminate container: %s", err)
    }
}()
if err != nil {
    log.Printf("failed to start container: %s", err)
    return
}

connectionStr, err := milvusContainer.ConnectionString(ctx)
if err != nil {
    log.Printf("failed to get connection string: %s", err)
    return
}

// Create a client to interact with the Milvus container
milvusClient, err := client.NewGrpcClient(context.Background(), connectionStr)
if err != nil {
    log.Print("failed to connect to Milvus:", err.Error())
    return
}
defer milvusClient.Close()

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,
}

err = milvusClient.CreateCollection(
    context.Background(), // ctx
    schema,
    2, // shardNum
)
if err != nil {
    log.Printf("failed to create collection: %s", err)
    return
}

list, err := milvusClient.ListCollections(context.Background())
if err != nil {
    log.Printf("failed to list collections: %s", err)
    return
}