Retrieve vectors in Fusion mode (Go SDK V2)
Use the Go SDK V2 to call the QueryVectorsFusion operation to search a Fusion mode vector index. This operation supports vector retrieval, scalar filtering, full-text search, and multi-way recall fusion.
Fusion Mode is currently in invitational preview and is available only in the Indonesia (Jakarta) region. For retrieving vectors from a Standard Mode index, see Retrieve vectors (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 |
QueryVectorsFusion | oss:QueryVectors and oss:GetVectors | Queries a Fusion-mode vector index. |
Method definition
A Fusion mode index is searched by calling the QueryVectorsFusion method, which uses typed request and response structures. This operation supports vector retrieval, scalar filtering, full-text search, and multi-way recall fusion.
func (c *VectorsClient) QueryVectorsFusion(ctx context.Context, request *vectors.QueryVectorsFusionRequest, optFns ...func(*oss.Options)) (*vectors.QueryVectorsFusionResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The request context. |
request | *vectors.QueryVectorsFusionRequest | The QueryVectorsFusion request struct, which contains Bucket, IndexName, Knn, Query, Retriever, and other fields. For the meaning of each field, see the |
optFns | ...func(*Options) | (Optional) The operation-level configuration parameters. |
Return values
Parameter | Type | Description |
result | *vectors.QueryVectorsFusionResult | The return value of QueryVectorsFusion, which contains StatusCode, RequestId, Vectors, NextToken, OutputFields, 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(®ion, "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)
// QueryVectorsFusion supports multiple query modes. Build the request
// using Knn, Retriever, and other typed structs.
request := &vectors.QueryVectorsFusionRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
Knn: []map[string]any{
vectors.Knn{
Field: oss.Ptr("demo"),
QueryVector: map[string]any{"float32": []float32{float32(32)}},
TopK: oss.Ptr(10),
NumCandidates: oss.Ptr(9),
Filter: map[string]any{
"meta_field_1": map[string]any{
"$eq": "abc",
},
},
Boost: oss.Ptr(float32(1)),
}.ToMap(),
},
Retriever: map[string]any{
"simple": vectors.SimpleRetriever{
Query: map[string]any{
"$and": []map[string]any{
{"type": map[string]any{"$in": []string{"a", "b"}}},
{"year": map[string]any{"$gte": 2020}},
},
},
}.ToMap(),
},
ReturnMetadata: oss.Ptr(true),
ReturnMetadataFields: []string{"key1", "key2"},
PartitionKeys: []string{"key1", "key2"},
Limit: oss.Ptr(10),
NextToken: oss.Ptr("nextToken"),
Sort: []vectors.Sort{
{"field_a": vectors.SortOptions{Order: oss.Ptr("asc")}},
{"_score": vectors.SortOptions{Order: oss.Ptr("desc")}},
{"_primaryKey": vectors.SortOptions{Order: oss.Ptr("asc")}},
},
}
result, err := client.QueryVectorsFusion(context.TODO(), request)
if err != nil {
log.Fatalf("failed to query vectors fusion: %v", err)
}
log.Printf("query vectors fusion result: %#v\n", result)
}