Basic search

更新时间:
复制 MD 格式

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 appName in 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.

Important

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

    1. Create an environment variable file and add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET with their respective values.

    2. 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