Push documents to an OpenSearch application using the bulk API. This page provides a TypeScript example that adds multiple documents in a single request, explains the cmd field options, and shows how to interpret the response.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch application with at least one table
An Alibaba Cloud AccessKey pair (
accessKeyIdandaccessKeySecret)(Optional) An STS token, if you use Resource Access Management (RAM) and Security Token Service (STS) authentication
Install dependencies
Download packages from npmjs.com.
Add the following to your package.json:
{
"dependencies": {
"@alicloud/credentials": "*",
"@alicloud/opensearch-util": "*",
"@alicloud/tea-typescript": "*",
"@alicloud/tea-util": "*"
},
"devDependencies": {
"typescript": "*",
"ts-node": "*"
}
}Then run:
npm installPush documents
The following example submits a bulk request to add two documents to an OpenSearch table.
connectTimeoutandreadTimeoutare in milliseconds. For large bulk requests, increase these values to avoid timeouts. The values in this example areconnectTimeout: 5000andreadTimeout: 10000.
import * as $Util from '@alicloud/tea-util';
import Client from "./Client";
import Config from "./Config";
// Create a Config instance.
let config = new Config();
// Specify the endpoint of the OpenSearch API. Obtain the endpoint from the OpenSearch console.
config.endpoint = "opensearch-cn-hangzhou.aliyuncs.com";
// Specify the request protocol. Default value: HTTP. Valid values: HTTPS and HTTP.
config.protocol = "HTTP";
// Specify the authentication method. Default value: access_key.
// Set to sts for RAM and STS authentication.
// Valid values: sts and access_key.
config.type = "access_key";
// Required only when config.type is sts.
// Call the AssumeRole operation of Alibaba Cloud RAM to obtain an STS token.
config.securityToken = "";
// Specify your AccessKey pair.
config.accessKeyId = "<accessKeyId>";
config.accessKeySecret = "<accessKeySecret>";
// Specify runtime parameters. Timeout values are in milliseconds.
let runtime = new $Util.RuntimeOptions({
connectTimeout: 5000,
readTimeout: 10000,
autoretry: false,
ignoreSSL: false,
maxIdleConns: 50,
});
// Create an OpenSearch client instance.
let client = new Client(config);
// Name or version of the application to push data to.
const appName = "<appName>";
// Name of the destination table in the application.
const tableName = "<tableName>";
// Build the request body.
// The optional timestamp field controls update order for documents with the same primary key.
// If omitted, OpenSearch 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 placeholders with your actual values:
| Placeholder | Description |
|---|---|
<accessKeyId> | Your AccessKeyId |
<accessKeySecret> | Your AccessKeySecret |
<appName> | The name or version of the OpenSearch application |
<tableName> | The name of the destination table in the application |
The timestamp field
The timestamp field is optional. When two documents share the same primary key, OpenSearch applies updates in timestamp order. If timestamp is omitted, OpenSearch applies updates in the order they are received.
What's next
For information on processing data after pushing, see Process data.