Demo code for pushing documents

更新时间:
复制 MD 格式

This page shows how to push documents to an OpenSearch High-performance Search Edition application using the SDK for C#. You will configure credentials, add the required NuGet packages, and call the bulk push API to index documents.

Prerequisites

Before you begin, ensure that you have:

Important

An Alibaba Cloud account AccessKey pair has access to all API operations. Use a RAM user to call API operations and perform routine O&M instead. For more information, see Create a RAM user and Create an AccessKey pair.

Step 1: Set environment variables

Store your AccessKey credentials as environment variables rather than hardcoding them in your project.

  • Linux and macOS Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of your RAM user.

    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 and add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET variables with your AccessKey ID and AccessKey secret as their values.

    2. Restart Windows for the changes to take effect.

Step 2: Add dependencies

Install the required NuGet packages from 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

Step 3: Push documents

The following sample code shows how to push documents using the SDK for C#. The client reads credentials from the environment variables you set in Step 1, then calls the bulk push API (/v3/openapi/apps/{appName}/{tableName}/actions/bulk) to index documents.

The optional timestamp field controls the update order for documents that share the same primary key — OpenSearch applies updates in ascending timestamp order. Without a timestamp, OpenSearch processes documents in the order it receives them.

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
            {
                // Specify the endpoint of the OpenSearch API.
                // Obtain the endpoint from the OpenSearch console.
                Endpoint = "opensearch-cn-hangzhou.aliyuncs.com",

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

                // Read credentials from environment variables.
                // Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET before running this code.
                AccessKeyId = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                AccessKeySecret = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),

                // Specify the authentication method. Valid values: access_key and sts.
                // Use sts for authentication based on Resource Access Management (RAM) and Security Token Service (STS).
                // Default value: access_key.
                Type = "access_key",

                // If you use STS authentication, specify the SecurityToken parameter.
                // Obtain an STS token by calling the AssumeRole operation of Alibaba Cloud RAM.
                SecurityToken = "",
            };

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

            // Specify the name of the application to push documents to.
            var appName = "my-search-app";

            // Specify the name of the table to push documents to.
            var tableName = "products";

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

            // Build the document list.
            // document1 has no timestamp; document2 has a timestamp.
            // When both share the same primary key, OpenSearch applies document2's update last.
            var doc1Fields = new Dictionary<string, object>
            {
                { "id", "doc-001" },
                { "title", "Introduction to OpenSearch" },
                { "text", "OpenSearch is a managed search service." },
                { "cate", "documentation" }
            };
            var document1 = new Dictionary<string, object>
            {
                { "cmd", "ADD" },
                { "fields", doc1Fields }
            };

            var doc2Fields = new Dictionary<string, object>
            {
                { "id", "doc-002" },
                { "title", "Getting started with full-text search" },
                { "text", "This guide walks you through your first search query." },
                { "cate", "tutorial" }
            };
            var document2 = new Dictionary<string, object>
            {
                { "cmd", "ADD" },
                { "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds() },
                { "fields", doc2Fields }
            };

            var documents = new List<object> { document1, document2 };

            try
            {
                docBulk(openSearch, appName, tableName, documents, header, runtime);
                Console.WriteLine("Documents pushed successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}

What's next

For information on how to process data in OpenSearch after pushing documents, see Process data.