This page shows how to query hints using the OpenSearch C# SDK. The example submits a GET request to the hint endpoint with sort_type, hit, and user_id parameters.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application with the hint feature enabled
A RAM user with the required permissions. For details, see AliyunServiceRoleForOpenSearch and Access authorization rules
An AccessKey pair for the RAM user. To create one, see Create an AccessKey pair
Use a Resource Access Management (RAM) user's AccessKey pair rather than your Alibaba Cloud account's 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. Keep your AccessKey pair out of source code and shared files to prevent unauthorized access.
Step 1: Set environment variables
Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to your RAM user's AccessKey ID and AccessKey secret.
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 the
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETvariables with their corresponding values.Restart Windows for the changes to take effect.
Step 2: Add dependencies
Download packages from NuGet and add them to your project:
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.0Step 3: Initialize the client
Create a Client instance configured with your endpoint and credentials:
var config = new Config
{
// The endpoint of the OpenSearch API. Get this from the OpenSearch console.
Endpoint = "opensearch-cn-hangzhou.aliyuncs.com",
// Request protocol. Valid values: HTTPS, HTTP.
Protocol = "HTTPS",
// Authentication method. Default: access_key.
// Use sts for RAM + Security Token Service (STS) authentication.
Type = "access_key",
// AccessKey ID and secret from environment variables.
AccessKeyId = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
AccessKeySecret = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
// Required only when Type is "sts". Leave blank for access_key authentication.
SecurityToken = "",
};
var openSearch = new Client(config);Step 4: Query hints
The following complete example sends a hint query and prints any errors to the console:
using System;
using System.Collections.Generic;
using AlibabaCloud.TeaUtil.Models;
using Tea;
namespace ConsoleApp2
{
internal class Program
{
public static Dictionary<string, object> appAlgoSearh(Client opensearchClient, string appName, string algoName,
string algoType,
Dictionary<string, object> queryParams, Dictionary<string, string> header, RuntimeOptions runTime)
{
string pathName;
if (algoType != "suggest")
{
pathName = "/v3/openapi/apps/" + appName + "/actions/" + algoType;
}
else
{
pathName = "/v3/openapi/apps/" + appName + "/suggest/" + algoName + "/search";
}
try
{
return opensearchClient._request("GET", pathName, queryParams, header, null, runTime);
}
catch (Exception 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
{
Endpoint = "opensearch-cn-hangzhou.aliyuncs.com",
Protocol = "HTTPS",
Type = "access_key",
AccessKeyId = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
AccessKeySecret = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
SecurityToken = "",
};
var openSearch = new Client(config);
// The name of your OpenSearch application.
var appName = "appName";
// Optional custom HTTP headers.
var header = new Dictionary<string, string>();
// Query parameters for the hint request.
var hotHintQuery = new Dictionary<string, object>
{
{"sort_type", "default"}, // Sorting strategy for hints.
{"hit", 10}, // Maximum number of hints to return.
{"user_id", "a7a0d37c824b659f36a5b9e3b819fcdd"} // User identifier for personalized hints. Replace with the actual user ID.
};
try
{
Dictionary<string, object> ret = appAlgoSearh(openSearch, appName, null, "hint", hotHintQuery, header,
runtime);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}Query parameters
| Parameter | Type | Description |
|---|---|---|
sort_type | String | Sorting strategy for returned hints. |
hit | Integer | Maximum number of hints to return. |
user_id | String | The ID of the current user. Pass the actual user ID to enable personalized hint ranking. |
What's next
Query top searches and hints — learn about the hint query API and available parameters
Create a RAM user — set up a RAM user for secure access