全部产品

搜索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 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
            {
                // 配置统一的请求入口, 此信息 可从 业务控制台获取.
                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请求结构.
            // config子句中的format参数目前暂时只支持配置为json/fulljson 
            var docQuery = new Dictionary<string, object>
            {
                {
                    "query",
                    "config=start:0,hit:10,format:fulljson&&query=(default:\"Search\" AND default:\"开放搜索\") OR (default:\"搜索引擎\" AND default:\"策略\")"
                },
                {"second_rank_name", "att"}, {"first_rank_name", "sdfrsdfs"}, {"fetch", "qp:profile"},
                {"fetch_fields", "title"}
            };


            // --------------- 文档搜索 ---------------
            // 文档参考 https://help.aliyun.com/document_detail/57155.html 
            try
            {
                Dictionary<string, object> ops = searchDoc(openSearch, appName, docQuery, header, runtime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}