Use the Go SDK V2 to call QueryVectors and run a similarity search against a vector index.
Permissions
By default, an Alibaba Cloud account has full permissions. Resource Access Management (RAM) users and RAM roles have no permissions by default — grant access using a RAM policy or a bucket policy.
| API | Action | Description |
|---|---|---|
| QueryVectors | oss:QueryVectors | Queries vector data. |
Method definition
func (c *VectorsClient) QueryVectors(ctx context.Context, request *QueryVectorsRequest, optFns ...func(*oss.Options)) (*QueryVectorsResult, error)Request parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The request context. |
request | *QueryVectorsRequest | Yes | The query parameters, including the query vector, filter condition, and the number of results to return. See QueryVectorsRequest. |
optFns | ...func(*Options) | No | Interface-level configuration options. See Options. |
Return values
| Parameter | Type | Description |
|---|---|---|
result | *QueryVectorsResult | The query result. Valid only when err is nil. See QueryVectorsResult. |
err | error | The error. nil if the operation succeeds. |
Sample code
The example below submits a similarity search with a metadata filter, returning the top 10 matches with their metadata and distances.
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 the vector account.")
flag.StringVar(&indexName, "index", "", "The name of the 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")
}
// Load credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId)
client := vectors.NewVectorsClient(cfg)
request := &vectors.QueryVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
// Filter using $and + $in: match vectors where "type" is "comedy" or "documentary"
Filter: map[string]any{
"$and": []map[string]any{
{
"type": map[string]any{
"$in": []string{"comedy", "documentary"},
},
},
},
},
QueryVector: map[string]any{
"float32": []float32{float32(32)},
},
ReturnMetadata: oss.Ptr(true),
ReturnDistance: oss.Ptr(true),
TopK: oss.Ptr(10),
}
result, err := client.QueryVectors(context.TODO(), request)
if err != nil {
log.Fatalf("failed to query vectors %v", err)
}
log.Printf("query vectors result:%#v\n", result)
}References
For the complete sample, see query_vectors.go.