メインコンテンツまでスキップ

Upsert()

Addedv2.6.x

This operation inserts new entities or updates existing ones based on primary key values.

func (c *Client) Upsert(ctx context.Context, option UpsertOption, callOptions ...grpc.CallOption) (UpsertResult, error)

Request Syntax

option := milvusclient.NewColumnBasedInsertOption(collName).
WithInt64Column(colName, data).
WithVarcharColumn(colName, data).
WithFloatVectorColumn(colName, dim, data).
WithBinaryVectorColumn(colName, dim, data).
WithBoolColumn(colName, data).
WithPartition(partitionName).
WithPartialUpdate(partialUpdate)

// Alternative (row-based):
// option := milvusclient.NewRowBasedInsertOption(collName, rows...)

result, err := client.Upsert(ctx, option)

PARAMETERS:

  • collName (string)

The name of the target collection.

OPTION METHODS:

  • WithColumns(columns ...column.Column)

Inserts arbitrary typed columns.

  • WithBoolColumn(colName string, data []bool)

Inserts a column of boolean values.

  • WithInt8Column(colName string, data []int8)

Inserts a column of int8 values.

  • WithInt16Column(colName string, data []int16)

Inserts a column of int16 values.

  • WithInt32Column(colName string, data []int32)

Inserts a column of int32 values.

  • WithInt64Column(colName string, data []int64)

Inserts a column of int64 values.

  • WithVarcharColumn(colName string, data []string)

Inserts a column of string values.

  • WithFloatVectorColumn(colName string, dim int, data [][]float32)

Inserts a column of float32 dense vectors.

  • WithFloat16VectorColumn(colName string, dim int, data [][]float32)

Inserts a column of float16 vectors (converted from float32).

  • WithBFloat16VectorColumn(colName string, dim int, data [][]float32)

Inserts a column of bfloat16 vectors (converted from float32).

  • WithBinaryVectorColumn(colName string, dim int, data [][]byte)

Inserts a column of binary vectors.

  • WithInt8VectorColumn(colName string, dim int, data [][]int8)

Inserts a column of int8 vectors.

  • WithPartition(partitionName string)

Targets a specific partition for the upsert operation.

  • WithPartialUpdate(partialUpdate bool)

Enables partial update mode so only provided fields are updated (existing fields not in the payload are preserved).

RETURN TYPE:

UpsertResult, error

RETURNS:

The upsert result containing the IDs of the affected entities. Returns an error if the operation fails.

EXCEPTIONS:

  • error

    Check err != nil for failure details.

Example

import (
"context"
"fmt"

"github.com/milvus-io/milvus/client/v2/milvusclient"
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}

defer cli.Close(ctx)

resp, err := cli.Upsert(ctx, milvusclient.NewColumnBasedInsertOption("quick_setup").
WithInt64Column("id", []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}).
WithVarcharColumn("color", []string{"pink_8682", "red_7025", "orange_6781", "pink_9298", "red_4794", "yellow_4222", "red_9392", "grey_8510", "white_9381", "purple_4976"}).
WithFloatVectorColumn("vector", 5, [][]float32{
{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592},
{0.19886812562848388, 0.06023560599112088, 0.6976963061752597, 0.2614474506242501, 0.838729485096104},
{0.43742130801983836, -0.5597502546264526, 0.6457887650909682, 0.7894058910881185, 0.20785793220625592},
{0.3172005263489739, 0.9719044792798428, -0.36981146090600725, -0.4860894583077995, 0.95791889146345},
{0.4452349528804562, -0.8757026943054742, 0.8220779437047674, 0.46406290649483184, 0.30337481143159106},
{0.985825131989184, -0.8144651566660419, 0.6299267002202009, 0.1206906911183383, -0.1446277761879955},
{0.8371977790571115, -0.015764369584852833, -0.31062937026679327, -0.562666951622192, -0.8984947637863987},
{-0.33445148015177995, -0.2567135004164067, 0.8987539745369246, 0.9402995886420709, 0.5378064918413052},
{0.39524717779832685, 0.4000257286739164, -0.5890507376891594, -0.8650502298996872, -0.6140360785406336},
{0.5718280481994695, 0.24070317428066512, -0.3737913482606834, -0.06726932177492717, -0.6980531615588608},
}),
)
if err != nil {
// handle err
}
fmt.Println(resp)