Use the following Go code example to call the OpenSearch drop-down suggestion API.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application with a drop-down suggestion model configured
A Resource Access Management (RAM) user with the required permissions granted for the
AliyunServiceRoleForOpenSearchrole by using your Alibaba Cloud account. For details, see AliyunServiceRoleForOpenSearch and Access authorization rules. To create a RAM user, see Create a RAM userAn AccessKey pair for the RAM user. To create one, see Create an AccessKey pair
Do not embed your AccessKey pair directly in code. Store credentials in environment variables to keep them out of version control.
Query drop-down suggestions
Step 1: Set environment variables
Set your AccessKey pair as environment variables. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of your RAM user.
Linux and macOS
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows
Create an environment variable file and add
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETwith your credentials.Restart Windows for the changes to take effect.
Step 2: Initialize the client
Create an OpenSearch client using your endpoint and credentials from environment variables.
config := &opensearch.Config{
// Replace <Endpoint> with the OpenSearch API endpoint for your region.
Endpoint: tea.String("<Endpoint>"),
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
client, err := opensearch.NewClient(config)
if err != nil {
fmt.Println(err)
return
}Step 3: Configure runtime options
Set connection and timeout parameters for the request.
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}| Option | Value | Description |
|---|---|---|
ConnectTimeout | 5000 | Connection timeout in milliseconds |
ReadTimeout | 10000 | Read timeout in milliseconds |
Autoretry | false | Whether to retry on failure |
IgnoreSSL | false | Whether to skip SSL certificate verification |
MaxIdleConns | 50 | Maximum number of idle connections in the pool |
Step 4: Send the request
Specify your application name, model name, and query, then send the GET request.
requestParams := map[string]interface{}{
"hit": 10,
"query": "<words>",
}
// appName: the name of the OpenSearch application to query.
// modelName: the name of the drop-down suggestion model.
appName := "<appName>"
modelName := "<modelName>"
response, err := client.Request(
tea.String("GET"),
tea.String("/v3/openapi/apps/"+appName+"/suggest/"+modelName+"/search"),
requestParams,
nil,
nil,
runtime,
)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response)Complete example
The following is the full runnable code. All steps above are combined into a single main function.
// This file is auto-generated, don't edit it. Thanks.
package main
import (
"fmt"
"os"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
opensearch "main/client"
)
func main() {
config := &opensearch.Config{
Endpoint: tea.String("<Endpoint>"),
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
client, err := opensearch.NewClient(config)
if err != nil {
fmt.Println(err)
return
}
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}
requestParams := map[string]interface{}{
"hit": 10,
"query": "<words>",
}
appName := "<appName>"
modelName := "<modelName>"
response, err := client.Request(
tea.String("GET"),
tea.String("/v3/openapi/apps/"+appName+"/suggest/"+modelName+"/search"),
requestParams,
nil,
nil,
runtime,
)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(response)
}Replace the following placeholders before running the code:
| Placeholder | Description |
|---|---|
<Endpoint> | The OpenSearch API endpoint for your region |
<appName> | The name of your OpenSearch application |
<modelName> | The name of your drop-down suggestion model |
<words> | The query string to get suggestions for |
What's next
Query drop-down suggestions — full API reference for the drop-down suggestion endpoint