All Products
Search
Document Center

AnalyticDB:Go

Last Updated:Nov 26, 2025

OpenAPI encapsulates the Data Definition Language (DDL) and Data Manipulation Language (DML) for vector operations in AnalyticDB for PostgreSQL. You can use OpenAPI to manage vector data. This topic describes how to import and query vector data using the Go software development kit (SDK).

Prerequisites

Procedure

  1. Initialize the vector database

  2. Create a namespace

  3. Create a collection

  4. Upload vector data

  5. Retrieve vector data

Initialize the vector database

Before you use vector search, you must initialize the knowledgebase database and the full-text index feature.

Call the operation. The following sample code provides an example:

package main

import (
	"fmt"
	"os"
	
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	gpdb20160503 "github.com/alibabacloud-go/gpdb-20160503/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func CreateClient() (_result *gpdb20160503.Client, _err error) {
	config := &openapi.Config{
		AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
		AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
	}
	// For more information about endpoints, see https://api.aliyun.com/product/gpdb.
	config.Endpoint = tea.String("gpdb.aliyuncs.com")
	_result = &gpdb20160503.Client{}
	_result, _err = gpdb20160503.NewClient(config)
	return _result, _err
}

func main() {
	client, _err := CreateClient()
	if _err != nil {
		panic(_err)
	}

	initVectorDatabaseRequest := &gpdb20160503.InitVectorDatabaseRequest{
		RegionId:               tea.String("cn-beijing"),
		DBInstanceId:           tea.String("gp-bp1c62r3l489****"),
		ManagerAccount:         tea.String("myaccount"),
		ManagerAccountPassword: tea.String("myaccount_password"),
	}
	runtime := &util.RuntimeOptions{}
	response, _err := client.InitVectorDatabaseWithOptions(initVectorDatabaseRequest, runtime)
	if _err != nil {
		panic(_err)
	}
	fmt.Printf("response is %#v\n", response.Body)
}

For more information, see InitVectorDatabase.

Create a namespace

Namespaces are used for schema isolation. Before you use vectors, you must create at least one namespace or use the public namespace.

Call. The following is a code sample:

package main

import (
	"fmt"
	"os"
	
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	gpdb20160503 "github.com/alibabacloud-go/gpdb-20160503/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func CreateClient() (_result *gpdb20160503.Client, _err error) {
	config := &openapi.Config{
		AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
		AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
	}
	// For more information about endpoints, see https://api.aliyun.com/product/gpdb.
	config.Endpoint = tea.String("gpdb.aliyuncs.com")
	_result = &gpdb20160503.Client{}
	_result, _err = gpdb20160503.NewClient(config)
	return _result, _err
}

func main() {
	client, _err := CreateClient()
	if _err != nil {
		panic(_err)
	}

	createNamespaceRequest := &gpdb20160503.CreateNamespaceRequest{
		RegionId:               tea.String("cn-beijing"),
		DBInstanceId:           tea.String("gp-bp1c62r3l489****"),
		ManagerAccount:         tea.String("myaccount"),
		ManagerAccountPassword: tea.String("myaccount_password"),
		Namespace:              tea.String("vector_test"),
		NamespacePassword:      tea.String("vector_test_password"),
	}
	runtime := &util.RuntimeOptions{}
	response, _err := client.CreateNamespaceWithOptions(createNamespaceRequest, runtime)
	if _err != nil {
		panic(_err)
	}
	fmt.Printf("response is %#v\n", response.Body)
}

For more information, see CreateNamespace.

After you create the namespace, you can find the corresponding schema in the instance's knowledgebase database.

SELECT schema_name FROM information_schema.schemata;

Create a collection

Collections store vector data and are isolated by namespaces.

Call The following is an example:

package main

import (
	"fmt"
	"os"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	gpdb20160503 "github.com/alibabacloud-go/gpdb-20160503/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)



func CreateClient() (_result *gpdb20160503.Client, _err error) {
	config := &openapi.Config{
		AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
		AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
	}
	// For more information about endpoints, see https://api.aliyun.com/product/gpdb.
	config.Endpoint = tea.String("gpdb.aliyuncs.com")
	_result = &gpdb20160503.Client{}
	_result, _err = gpdb20160503.NewClient(config)
	return _result, _err
}

func main() {
	client, _err := CreateClient()
	if _err != nil {
		panic(_err)
	}

	createCollectionRequest := &gpdb20160503.CreateCollectionRequest{
		RegionId: tea.String("cn-beijing"),
		DBInstanceId: tea.String("gp-bp1c62r3l489****"),
		ManagerAccount: tea.String("myaccount"),
		ManagerAccountPassword: tea.String("myaccount_password"),
		Namespace: tea.String("vector_test"),
		Collection: tea.String("document"),
		Dimension: tea.Int64(3),
		Parser: tea.String("zh_cn"),
		FullTextRetrievalFields: tea.String("title,content"),
		Metadata: tea.String("{\"title\":\"text\",\"content\":\"text\",\"response\":\"int\"}"),
	}
	runtime := &util.RuntimeOptions{}
	response, _err := client.CreateCollectionWithOptions(createCollectionRequest, runtime)
	if _err != nil {
		panic(_err)
	}
	fmt.Printf("response is %#v\n", response.Body)
}

For more information about the parameters, see CreateCollection.

After the collection is created, you can view the corresponding table in the instance's knowledgebase database.

SELECT tablename FROM pg_tables WHERE schemaname='vector_test';

Upload vector data

Upload the prepared embedding vector data to the corresponding collection.

Call the operation. The following sample code provides an example:

package main

import (
	"fmt"
	"os"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	gpdb20160503 "github.com/alibabacloud-go/gpdb-20160503/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func CreateClient() (_result *gpdb20160503.Client, _err error) {
	config := &openapi.Config{
		AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
		AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
	}
	// For more information about endpoints, see https://api.aliyun.com/product/gpdb.
	config.Endpoint = tea.String("gpdb.aliyuncs.com")
	_result = &gpdb20160503.Client{}
	_result, _err = gpdb20160503.NewClient(config)
	return _result, _err
}

func main() {
	client, _err := CreateClient()
	if _err != nil {
		panic(_err)
	}

	rows0Metadata := map[string]*string{
		"title":    tea.String("Test document"),
		"content":  tea.String("Test content"),
		"response": tea.String("1"),
	}
	rows0 := &gpdb20160503.UpsertCollectionDataRequestRows{
		Metadata: rows0Metadata,
		Id:       tea.String("0CB55798-ECF5-4064-B81E-FE35B19E01A6"),
		Vector:   []*float64{tea.Float64(0.2894745251078251), tea.Float64(0.5364747050266715), tea.Float64(0.1276845661831275)},
	}
	upsertCollectionDataRequest := &gpdb20160503.UpsertCollectionDataRequest{
		RegionId:          tea.String("cn-beijing"),
		Rows:              []*gpdb20160503.UpsertCollectionDataRequestRows{rows0},
		DBInstanceId:      tea.String("gp-bp1c62r3l489****"),
		Collection:        tea.String("document"),
		Namespace:         tea.String("vector_test"),
		NamespacePassword: tea.String("vector_test_password"),
	}
	runtime := &util.RuntimeOptions{}
	response, _err := client.UpsertCollectionDataWithOptions(upsertCollectionDataRequest, runtime)
	if _err != nil {
		panic(_err)
	}
	fmt.Printf("response is %#v\n", response.Body)
}

For more information, see UpsertCollectionData.

After the upload is complete, you can view the data in the knowledgebase database of the instance.

SELECT * FROM vector_test.document;

Retrieve vector data

Prepare the query vectors or full-text index fields for retrieval, and then call the query operation.

Call the operation. The following sample code provides an example:

package main

import (
	"fmt"
	"os"

	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	gpdb20160503 "github.com/alibabacloud-go/gpdb-20160503/v4/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func CreateClient() (_result *gpdb20160503.Client, _err error) {
	config := &openapi.Config{
		AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
		AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
	}
	// For more information about endpoints, see https://api.aliyun.com/product/gpdb.
	config.Endpoint = tea.String("gpdb.aliyuncs.com")
	_result = &gpdb20160503.Client{}
	_result, _err = gpdb20160503.NewClient(config)
	return _result, _err
}

func main() {
	client, _err := CreateClient()
	if _err != nil {
		panic(_err)
	}

	queryCollectionDataRequest := &gpdb20160503.QueryCollectionDataRequest{
		RegionId:          tea.String("cn-beijing"),
		DBInstanceId:      tea.String("gp-bp1c62r3l489****"),
		Collection:        tea.String("document"),
		Namespace:         tea.String("vector_test"),
		NamespacePassword: tea.String("vector_test_password"),
		Content:           tea.String("Test"),
		Filter:            tea.String("response > 0"),
		TopK:              tea.Int64(10),
		Vector:            []*float64{tea.Float64(0.7152607422256894), tea.Float64(0.5524872066437732), tea.Float64(0.1168505269851303)},
	}
	runtime := &util.RuntimeOptions{}
	response, _err := client.QueryCollectionDataWithOptions(queryCollectionDataRequest, runtime)
	if _err != nil {
		panic(_err)
	}
	fmt.Printf("response is %#v\n", response.Body)
}

The following result is returned:

{
   "Matches": {
      "match": [
         {
            "Id": "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
            "Metadata": {
               "content": "Test content",
               "response": "1",
               "source": "3",
               "title": "Test document"
            },
            "MetadataV2": {
               "content": "Test content",
               "response": 1,
               "source": 3,
               "title": "Test document"
            },
            "Score": 0.9132830731723668,
            "Values": {
               "value": [
                  0.28947452,
                  0.5364747,
                  0.12768456
               ]
            }
         }
      ]
   },
   "RequestId": "707D2202-61A6-53DF-AAD2-E8DE276CE292",
   "Status": "success"
}