Demo code for querying drop-down suggestions

更新时间:
复制 MD 格式

Use the following Go code example to call the OpenSearch drop-down suggestion API.

Prerequisites

Before you begin, ensure that you have:

Important

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

    1. Create an environment variable file and add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET with your credentials.

    2. 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),
}
OptionValueDescription
ConnectTimeout5000Connection timeout in milliseconds
ReadTimeout10000Read timeout in milliseconds
AutoretryfalseWhether to retry on failure
IgnoreSSLfalseWhether to skip SSL certificate verification
MaxIdleConns50Maximum 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:

PlaceholderDescription
<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