You can use the Go software development kit (SDK) V2 to call the QueryVectors operation and perform a similarity search in a vector index.
Permissions
An Alibaba Cloud account has all permissions by default. A Resource Access Management (RAM) user or RAM role under an Alibaba Cloud account has no permissions by default. The Alibaba Cloud account or an administrator must grant permissions for the operation using a RAM policy overview or a bucket policy.
|
API |
Action |
Description |
|
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 |
Description |
|
ctx |
context.Context |
The request context. |
|
request |
*QueryVectorsRequest |
Sets the request parameters, including the query vector, filter condition, and the number of results to return. For more information, see QueryVectorsRequest. |
|
optFns |
...func(*Options) |
(Optional) The interface-level configuration parameters. For more information, see Options. |
Return values
|
Parameter |
Type |
Description |
|
result |
*QueryVectorsResult |
The return value. This parameter is valid only when err is nil. For more information, see QueryVectorsResult. |
|
err |
error |
The error message. If the operation is successful, this parameter is 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 where the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The ID of the Alibaba Cloud 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, account-id required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access over the public internet, set this to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.QueryVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
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 code for querying vectors, see query_vectors.go.