全部产品

底纹Demo

Package 依赖地址: https://www.nuget.org/packages

安装Package 依赖

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

示例代码

以下是C# SDK 底纹的示例代码:

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/HTTP
                Protocol = "HTTPS",

                // 用户识别信息
                AccessKeyId = "<access_key_id>",
                AccessKeySecret = "<access_key_secret>",

                // 支持 type 配置 sts/access_key 鉴权. 其中 type 默认为 access_key 鉴权. 使用 sts 可配置 RAM-STS 鉴权.
                // 备选参数为:  sts 或者 access_key
                Type = "access_key",

                // 如果使用 RAM-STS 鉴权, 请配置 security_token, 可使用阿里云 AssumeRole 获取 相关 STS 鉴权结构.
                SecurityToken = "",
            };

            // 创建 opensearch client 实例
            var openSearch = new Client(config);

            // 要搜索的appName.
            var appName = "appName";

            // 自定义HttpHeaders .
            var header = new Dictionary<string, string>();

            // query请求结构.
            var hotHintQuery = new Dictionary<string, object>
            {
                {"sort_type", "default"},
                {"hit", 10},
                {"user_id", "a7a0d37c824b659f36a5b9e3b819fcdd"}
            };


            // ---------------  热词底纹搜索 ---------------
            // 文档参考 https://help.aliyun.com/document_detail/172878.html
            try
            {
                Dictionary<string, object> ret = appAlgoSearh(openSearch, appName, null, "hint", hotHintQuery, header,
                    runtime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}