Create a Fusion mode vector index (Go SDK V2)

Updated at:

Use the Go SDK V2 to call the PutVectorIndexFusion operation to create a Fusion mode vector index in a vector bucket.

Note

Fusion Mode is currently in invitational preview and is available in the China (Shenzhen) and Indonesia (Jakarta) regions. For creating a vector index in Standard Mode, see Create a vector index (Go SDK V2).

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. The Alibaba Cloud account owner or an administrator must grant the required permissions by using a RAM policy or a bucket policy.

API

Action

Description

PutVectorIndexFusion

oss:PutVectorIndex

Creates a Fusion-mode vector index.

Method definition

A Fusion mode index is created by calling the PutVectorIndexFusion method, which uses typed request and response structures.

func (c *VectorsClient) PutVectorIndexFusion(ctx context.Context, request *vectors.PutVectorIndexFusionRequest, optFns ...func(*oss.Options)) (*vectors.PutVectorIndexFusionResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The request context.

request

*vectors.PutVectorIndexFusionRequest

The PutVectorIndexFusion request struct, which contains Bucket, IndexName, Mode, SchemaConfiguration, and other fields. For the meaning of each field, see the PutVectorIndexFusion operation.

optFns

...func(*Options)

(Optional) The operation-level configuration parameters.

Return values

Parameter

Type

Description

result

*vectors.PutVectorIndexFusionResult

The return value of PutVectorIndexFusion, which contains StatusCode, RequestId, and other fields. This parameter is valid only when err is nil.

err

error

The status of the request. If the request fails, err is not nil.

Sample code

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)

var (
	region     string
	bucketName string
	accountId  string
	indexName  string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the vector bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
	flag.StringVar(&accountId, "account-id", "", "The id of vector account.")
	flag.StringVar(&indexName, "index", "", "The name of vector index.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}
	if len(accountId) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, accountId required")
	}
	if len(indexName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, index required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region).WithAccountId(accountId)

	client := vectors.NewVectorsClient(cfg)

	// Build the schema field list using FieldSchema structs
	request := &vectors.PutVectorIndexFusionRequest{
		Bucket:    oss.Ptr(bucketName),
		IndexName: oss.Ptr(indexName),
		Mode:      oss.Ptr("fusion"),
		SchemaConfiguration: &vectors.SchemaConfiguration{
			Fields: []map[string]any{
				vectors.FieldSchema{
					Name:           oss.Ptr("vector_1"),
					Type:           oss.Ptr("vector"),
					DataType:       oss.Ptr("float32"),
					Dimension:      oss.Ptr(1024),
					DistanceMetric: oss.Ptr("cosine"),
				}.ToMap(),
				vectors.FieldSchema{
					Name:           oss.Ptr("vector_2"),
					Type:           oss.Ptr("vector"),
					DataType:       oss.Ptr("float32"),
					Dimension:      oss.Ptr(512),
					DistanceMetric: oss.Ptr("cosine"),
				}.ToMap(),
				vectors.FieldSchema{
					Name:    oss.Ptr("timestamps"),
					Type:    oss.Ptr("long"),
					IsArray: oss.Ptr(true),
				}.ToMap(),
				vectors.FieldSchema{
					Name: oss.Ptr("price"),
					Type: oss.Ptr("double"),
				}.ToMap(),
				vectors.FieldSchema{
					Name: oss.Ptr("ip"),
					Type: oss.Ptr("ip"),
				}.ToMap(),
				vectors.FieldSchema{
					Name: oss.Ptr("location"),
					Type: oss.Ptr("geoPoint"),
				}.ToMap(),
				vectors.FieldSchema{
					Name: oss.Ptr("tag"),
					Type: oss.Ptr("string"),
				}.ToMap(),
				vectors.FieldSchema{
					Name:           oss.Ptr("user_id"),
					Type:           oss.Ptr("string"),
					IsPartitionKey: oss.Ptr(true),
				}.ToMap(),
				vectors.FieldSchema{
					Name:    oss.Ptr("tags"),
					Type:    oss.Ptr("string"),
					IsArray: oss.Ptr(true),
				}.ToMap(),
				vectors.FieldSchema{
					Name:       oss.Ptr("title_1"),
					Type:       oss.Ptr("string"),
					ExactMatch: oss.Ptr(true),
					Text: &vectors.TextSchema{
						Enabled:  oss.Ptr(true),
						Analyzer: oss.Ptr("standard"),
						AnalyzerParameters: &vectors.AnalyzerParameters{
							CaseSensitive: oss.Ptr(true),
							DelimitWord:   oss.Ptr(false),
						},
					},
				}.ToMap(),
				vectors.FieldSchema{
					Name:       oss.Ptr("title_2"),
					Type:       oss.Ptr("string"),
					ExactMatch: oss.Ptr(false),
					Text: &vectors.TextSchema{
						Enabled:  oss.Ptr(true),
						Analyzer: oss.Ptr("split"),
						AnalyzerParameters: &vectors.AnalyzerParameters{
							CaseSensitive: oss.Ptr(true),
							Delimiter:     oss.Ptr(" "),
						},
					},
				}.ToMap(),
			},
		},
	}

	result, err := client.PutVectorIndexFusion(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put vector index fusion: %v", err)
	}
	log.Printf("put vector index fusion result: %#v\n", result)
}