This page provides a complete Go example for running a basic search against an OpenSearch Industry Algorithm Edition application.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch Industry Algorithm Edition application. The application name or version is used as the
appNamein the search request.A Resource Access Management (RAM) user with the necessary permissions. Grant permissions through the AliyunServiceRoleForOpenSearch role. For more information, see Access authorization rules.
An AccessKey pair (AccessKey ID and AccessKey secret) for the RAM user. To create one, see Create an AccessKey pair.
The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. Do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.
Run a basic search
Step 1: Set environment variables
Set your AccessKey credentials 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 their respective values.Restart Windows for the changes to take effect.
Step 2: Run the sample code
The following example creates a client, builds a search request with query, ranking, and summary parameters, and prints the response.
// 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() {
// Initialize the client.
// Endpoint: the region-specific service domain for your application.
// Find your endpoint on the application details page in the OpenSearch console.
// Credentials are read from environment variables set in Step 1.
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, _clientErr := opensearch.NewClient(config)
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
// Build the request parameters.
requestParams := map[string]interface{}{
// Query string: paginate from offset 0, return 50 results, full JSON format.
"query": "config=start:0,hit:50,format:fulljson&&query=default:'<words>'",
// First rank (coarse ranking) configuration name.
"first_rank_name": "<rank_name>",
// Second rank (fine ranking) configuration name.
"second_rank_name": "<rank_name>",
// Summary: highlight matched text in the description field.
"summary": "summary_field:description,summary_ellipsis:...,summary_snipped:1,summary_len:50,summary_element_prefix:<abc>,summary_element_postfix:</abc>",
}
// Runtime options: connection and read timeouts, connection pool size.
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}
// appName can be the application name or the application version.
appName := "<appName>"
// Send the search request.
response, _requestErr := client.Request(
tea.String("GET"),
tea.String("/v3/openapi/apps/"+appName+"/search"),
requestParams,
nil,
nil,
runtime)
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// Print the response.
fmt.Println(response)
}What's next
AliyunServiceRoleForOpenSearch — Review the service-linked role and permissions required for OpenSearch.
Access authorization rules — Learn about the access control rules for OpenSearch API operations.
Create a RAM user — Set up a RAM user to manage API access with least-privilege permissions.