Vector search (Go SDK v2)

更新时间:
复制 MD 格式

OSS vector search helps you quickly find specific objects in large datasets by querying their semantic content, OSS metadata, multimedia metadata, object ETags, tags, and custom metadata. This topic shows how to use the Go SDK v2 to perform vector search.

Usage notes

  • The sample code in this topic is for the China (Hangzhou) region (region ID: cn-hangzhou) and uses the public endpoint by default. If you want to access OSS from another Alibaba Cloud product in the same region, use the internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • This topic provides an example of reading access credentials from environment variables. For information about how to configure access credentials, see Configure access credentials.

Examples

Enable vector search

The following code shows how to enable vector search for a specified bucket.

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"
)

var (
	region     string
	bucketName string
)

func init() {
	// Set a command-line flag to specify the region.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set a command-line flag to specify the bucket name.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Create a client configuration and use credentials from environment variables.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create an OSS client.

	// Create a request to enable AISearch for the specified bucket.
	request := &oss.OpenMetaQueryRequest{
		Bucket: oss.Ptr(bucketName),
		Mode:   oss.Ptr("semantic"), // Set mode to "semantic" to enable semantic search capabilities.
	}
	result, err := client.OpenMetaQuery(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to open meta query %v", err)
	}

	log.Printf("open meta query result:%#v\n", result)
}

Get metadata index status

The following code shows how to get the status of the metadata index library for a specified bucket.

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"
)

var (
	region     string 
	bucketName string
)

func init() {
	// Set a command-line flag to specify the region.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set a command-line flag to specify the bucket name.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Create a client configuration and use credentials from environment variables and the specified region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create an OSS client.

	// Create a request to get the status of the metadata index library.
	request := &oss.GetMetaQueryStatusRequest{
		Bucket: oss.Ptr(bucketName), // Specify the bucket to query.
	}
	result, err := client.GetMetaQueryStatus(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get meta query status %v", err)
	}

	log.Printf("get meta query status result:%#v\n", result)
}

Query objects

The following code shows how to use vector search to query objects that match specific semantic criteria.

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"
)

var (
	region     string
	bucketName string
)

func init() {
	// Set a command-line flag to specify the region.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set a command-line flag to specify the bucket name.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Create a client configuration and use credentials from environment variables and the specified region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create an OSS client.

	// Perform an AISearch operation.
	request := &oss.DoMetaQueryRequest{
		Bucket: oss.Ptr(bucketName),
		Mode:   oss.Ptr("semantic"),
		MetaQuery: &oss.MetaQuery{
			MaxResults: oss.Ptr(int64(99)),
			Query:      oss.Ptr("Overlook the snow-covered forest"), // Provide the semantic query string. This is an example.
			MediaTypes: &oss.MetaQueryMediaTypes{
				MediaTypes: []string{"image"}, // Specify the media types to search. In this example, "image".
			},
			SimpleQuery: oss.Ptr(`{"Operation":"gt", "Field": "Size", "Value": "30"}`),
		},
	}
	result, err := client.DoMetaQuery(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to do meta query %v", err)
	}

	log.Printf("do meta query result:%#v\n", result)
}

Disable vector search

The following code shows how to disable vector search for a specified bucket.

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" 
)

var (
	region     string
	bucketName string
)

func init() {
	// Set a command-line flag to specify the region.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set a command-line flag to specify the bucket name.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}


func main() {
	flag.Parse()

	// Check if the bucket name is provided.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check if the region is provided.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Create a client configuration and use credentials from environment variables and the specified region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create an OSS client.

	// Create a request to disable AISearch for the specified bucket.
	request := &oss.CloseMetaQueryRequest{
		Bucket: oss.Ptr(bucketName), // Specify the target bucket.
	}
	result, err := client.CloseMetaQuery(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to close meta query %v", err)
	}

	log.Printf("close meta query result:%#v\n", result)
}

References

  • For details on vector search operations, see Vector search.

  • For the data indexing API reference, see Data indexing.

  • The complete sample code for vector search is available on GitHub.