All Products
Search
Document Center

OpenSearch:Basic search

Last Updated:Dec 08, 2025

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.

Add dependencies

Download dependency packages at https://www.nuget.org/packages.

dotnet add package AlibabaCloud.TeaUtil --version  0.1.5
dotnet add package AlibabaCloud.OpenSearchUtil --version 1.0.2
dotnet add package Aliyun.Credentials --version  1.2.1
dotnet add package Tea --version 0.4.0

Sample code

The following sample code shows how to search for documents using the SDK for C#:

using System;
using System.Collections.Generic;
using AlibabaCloud.TeaUtil.Models;
using Tea;

namespace ConsoleApp
{
    internal class Program
    {
        public static Dictionary<string, object> searchDoc(Client opensearchClient, string appName,
            Dictionary<string, object> queryParams, Dictionary<string, string> header, RuntimeOptions runTime)
        {
            string pathName = "/v3/openapi/apps/" + appName + "/search";

            try
            {
                return opensearchClient._request("GET", pathName, queryParams, header, null, runTime);
            }
            catch (TeaException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }


        private static void Main(string[] args)
        {
            var runtime = new RuntimeOptions
            {
                ConnectTimeout = 5000,
                ReadTimeout = 10000,
                MaxAttempts = 0,
                Autoretry = false,
                IgnoreSSL = false,
                MaxIdleConns = 50
            };

            var config = new Config
            {
                // Specify the endpoint of the OpenSearch API. You can obtain the endpoint from the OpenSearch console.
                Endpoint = "opensearch-cn-hangzhou.aliyuncs.com",

                // Specify the request protocol. Valid values: HTTPS and HTTP.
                Protocol = "HTTPS",

                /* Specify your user credentials.
                Obtain the AccessKey ID and AccessKey secret from environment variables.
                Before you run this code, you must configure the environment variables. For more information, see the "Configure environment variables" section in this topic. */
                AccessKeyId =  System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                AccessKeySecret = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),

                // Specify the authentication method. The default value is access_key. A value of sts indicates authentication based on RAM and Security Token Service (STS).
                // Valid values: sts and access_key.
                Type = "access_key",

                // If you use authentication based on RAM and STS, you must specify the security_token parameter. You can call the AssumeRole operation of Alibaba Cloud to obtain an STS token.
                SecurityToken = "",
            };

            // Create an OpenSearch client instance.
            var openSearch = new Client(config);

            // The name of the app to search.
            var appName = "appName";

            // Customize HTTP headers.
            var header = new Dictionary<string, string>();

            // The structure of the query request.
            // The format parameter in the config clause supports only json and fulljson. 
            var docQuery = new Dictionary<string, object>
            {
                {
                    "query",
                    "config=start:0,hit:10,format:fulljson&&query=(default:\"Search\" AND default:\"OpenSearch\") OR (default:\"search engine\" AND default:\"policy\")"
                },
                {"second_rank_name", "att"}, {"first_rank_name", "sdfrsdfs"}, {"fetch", "qp:profile"},
                {"fetch_fields", "title"}
            };


            
            try
            {
                Dictionary<string, object> ops = searchDoc(openSearch, appName, docQuery, header, runtime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}
Note

For more information, see Initiate search requests.