Demo code for pushing documents

更新时间:
复制 MD 格式

This page shows a TypeScript example that pushes documents to an Open Search application using the bulk API.

Prerequisites

Before you begin, ensure that you have:

  • An Open Search application with at least one table

  • An AccessKey ID and AccessKey secret, or a Security Token Service (STS) token if you use Resource Access Management (RAM)-based authentication

  • Node.js installed with npm available

  • Your endpoint from the Open Search console

Install dependencies

Run the following commands to install the required packages. All packages are available from npmjs.com.

Runtime dependencies:

npm install @alicloud/credentials @alicloud/opensearch-util @alicloud/tea-typescript @alicloud/tea-util

Dev dependencies:

npm install --save-dev typescript ts-node

Push documents

The following example creates an Open Search client, configures authentication and runtime options, then pushes two documents to a table using a POST request to the bulk endpoint.

import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";

// Configure the client
let config = new Config();

// Endpoint from the Open Search console
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";

// Request protocol: HTTP (default) or HTTPS
config.protocol = "HTTP";

// Authentication method: access_key (default) or sts
// Use sts for Resource Access Management (RAM) and Security Token Service (STS) authentication.
config.type = "access_key";

// Required only when config.type is "sts".
// Get an STS token by calling the AssumeRole operation of Alibaba Cloud RAM.
config.securityToken = "";

// Runtime options (all time values are in milliseconds)
let runtime = new $Util.RuntimeOptions({
  connectTimeout: 5000,
  readTimeout: 10000,
  autoretry: false,
  ignoreSSL: false,
  maxIdleConns: 50,
});

// Create the client
let client = new Client(config);

// Replace with your application name or version
const appName = "<appName>";

// Replace with the destination table name in the application
const tableName = "<tableName>";

// Document payload
// cmd: "ADD" adds or updates a document.
// timestamp: controls update order for documents with the same primary key.
//   If omitted, Open Search processes documents in the order they are received.
let body = [
  {
    "fields": {
      "id": 1,
      "describe": "<describe>",
      "title": "<title>"
    },
    "cmd": "ADD"
  },
  {
    "fields": {
      "id": 2,
      "describe": "<describe>",
      "title": "<title>"
    },
    "timestamp": Date.now(),
    "cmd": "ADD"
  }
];

try {
  let pathname = `/v3/openapi/apps/${appName}/${tableName}/actions/bulk`;
  let result = client._request("POST", pathname, null, null, body, runtime);
  result.then(function (result) {
    console.log(result);
  });
} catch (e) {
  console.log(e);
}

Replace the following placeholders before running the code:

PlaceholderDescriptionExample
<appName>Name or version of the Open Search applicationmy-search-app
<tableName>Name of the destination table in the applicationproducts
<describe>Value for the describe fieldA red wool sweater
<title>Value for the title fieldWool Sweater
Note

The timestamp field is optional. When multiple documents share the same primary key, Open Search uses the timestamp value to determine which update to apply last. If timestamp is omitted, the order depends on when Open Search receives each document.

What's next

To process data after it is pushed, see Process data.