全部产品

文档推送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> docBulk(Client opensearchClient, string appName,
            string tableName, List<object> docContent, Dictionary<string, string> header, RuntimeOptions runTime)
        {
            string pathName = "/v3/openapi/apps/" + appName + "/" + tableName + "/actions/bulk";
            try
            {
                return opensearchClient._request("POST", pathName, null, header, docContent, 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";

            // 推送的目标表. 
            var tableName = "tableName";

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


            // 文档结构构造. 
            // timestamp 信息 用以增加对 文档操作的保序能力. 系统会用该时间戳来作为同一主键文档更新顺序的判断标准.
            // 在没有该timestamp项时,默认以文档发送到OpenSearch的时间作为文档更新时间进行操作。
            List<object> documents = new List<object>();
            Dictionary<string, object> document = new Dictionary<string, object>();
            Dictionary<string, object> document1 = new Dictionary<string, object>();
            Dictionary<string, object> fields = new Dictionary<string, object>();
            fields.Add("id", DateTime.Now.Millisecond);
            fields.Add("text", DateTime.Now.Millisecond);
            fields.Add("title", DateTime.Now.Millisecond);
            fields.Add("cate", DateTime.Now.Millisecond);
            document.Add("cmd", "ADD");
            document.Add("fields", fields);
            document1.Add("cmd", "ADD");
            document1.Add("timestamp", DateTime.Now.Second);
            document1.Add("fields", fields);
            documents.Add(document);
            documents.Add(document1);


            // ---------------  文档推送 ---------------
            // 文档参考 https://help.aliyun.com/document_detail/57154.html
            try
            {
                docBulk(openSearch, appName, tableName, documents, header, runtime);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}