Demo code for implementing the search feature

更新时间:
复制 MD 格式

This topic describes the demo code for implementing basic search features by using an SDK for Go.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you 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.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    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, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Sample code

// This file is auto-generated, don't edit it. Thanks.
package main

import (
    "fmt"
    util "github.com/alibabacloud-go/tea-utils/service"
    "github.com/alibabacloud-go/tea/tea"
    opensearch "main/client"
)

func main() {

    // Create a client instance for sending requests.
    // Endpoint: the endpoint of the OpenSearch API in your region.
    // AccessKeyId and AccessKeySecret: the AccessKey pair used for authentication.
    config := &opensearch.Config{
        Endpoint:         tea.String("<Endpoint>"),
      
        // Specify your AccessKey pair.
        // Obtain the AccessKey ID and AccessKey secret from environment variables. 
        // You must configure environment variables before you run this code. For more information, see the "Configure environment variables" section of this topic.
        // Specify the AccessKey ID and AccessKey secret.
        AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
        AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
    }

    // Create a client for sending requests.
    client, _clientErr := opensearch.NewClient(config)

    // If an error occurs when the system creates the client, _clientErr and an error message are returned.
    if _clientErr != nil {
        fmt.Println(_clientErr)
        return
    }

    // Specify request parameters. 
    requestParams := map[string]interface{}{
        "query":"config=start:0,hit:50,format:fulljson&&query=default:'<words>'",
        "second_rank_name":"<rank_name>",
        "first_rank_name": "<rank_name>",
      	// Specify the highlighted summary in search results. The following sample code provides an example:
				"summary": "summary_field:description,summary_ellipsis:...,summary_snipped:1,summary_len:50,summary_element_prefix:<abc>,summary_element_postfix:</abc>",
		
    }

    // Define configuration parameters that are used to configure the request and connection pool.
    runtime := &util.RuntimeOptions{
        ConnectTimeout: tea.Int(5000),
        ReadTimeout:    tea.Int(10000),
        Autoretry:      tea.Bool(false),
        IgnoreSSL:      tea.Bool(false),
        MaxIdleConns:   tea.Int(50),
    }

    // To query data, you must specify the appName parameter.
    // appName: the name or version of the application to which data is to be pushed.
    appName := "<appName>"

    // Call the method for sending a request.
    response, _requestErr := client.Request(
        tea.String("GET"),
        tea.String("/v3/openapi/apps/"+appName+"/search"),
        requestParams,
        nil,
        nil,
        runtime)

    // If an error occurs when the system sends the request, _requestErr and an error message are returned.
    if _requestErr != nil {
        fmt.Println(_requestErr)
        return
    }

    // Display the response if no error occurs.
    fmt.Println(response)
}

For information about the demo code for querying drop-down suggestions, see Demo code for querying drop-down suggestions.