Process data

更新时间:
复制 MD 格式

You can call API operations to create, update, and delete one or more documents.

URL

/update/$table_name/actions/bulk

  • $table_name specifies the name of the data source.

  • The sample URL omits information such as request headers and the encoding method.

  • The sample URL also omits the endpoint that is used to connect to OpenSearch.

Supported format

JSON

HTTP request method

POST

Headers

Header

Type

Description

authorization

string

The authentication signature.

X-Opensearch-Swift-PK-Field

string

The primary key field of the index table. Example: id.

host

string

The endpoint of the host to which requests are sent. You can view the endpoint of the host in the API Endpoint section of the Instance Details page in the OpenSearch console. Example: ha-cn-**********.ha.aliyuncs.com.

Signature method

You can use the fields in the following table to calculate the request signature. The request signature is stored in the authorization header.

Field

Data type

Description

accessUserName

string

The username. You can view the username in the API Endpoint section of the Instance Details page.

accessPassWord

string

The password. You can change the password in the API Endpoint section of the Instance Details page.

import com.aliyun.darabonba.encode.Encoder;
import com.aliyun.darabonbastring.Client;

public class GenerateAuthorization {

    public static void main(String[] args) throws Exception {
        String accessUserName = "username";
        String accessPassWord = "password";
        String realmStr = "" + accessUserName + ":" + accessPassWord + "";
        String authorization = Encoder.base64EncodeToString(Client.toBytes(realmStr, "UTF-8"));
        System.out.println(authorization);
		}
}

Valid value format returned for the authorization header:

cm9vdDp******mdhbA==

Note: You must add the Basic prefix when you specify the authorization header in an HTTP request.

Sample code

authorization: Basic cm9vdDp******mdhbA==

Request body for document processing

[
    {
        "cmd": "add",
        "fields": {
            "id": "1",
            "title": "This is the title",
            "body": "This is the body"
        }
    },
    {
        "cmd": "delete",
        "fields": {
            "id": "3"
        }
    }
]

  • cmd: required. The operation to be performed on the document. Valid values: add and delete. A value of update is not supported for standard applications. We recommend that you send a request to perform multiple operations at a time. This improves the interaction efficiency over the network and the processing efficiency. A value of add specifies that a document is to be created. If the document corresponding to the primary key value already exists, the existing document is deleted and a new document is created. A value of delete specifies that a document is to be deleted. If the document corresponding to the primary key value no longer exists, the document is deleted.

  • fields: required. The fields on which the operation is to be performed in the document. You must specify the primary key field. OpenSearch identifies a document based on its primary key value. If you want to delete a document, you need to only specify the primary key field of the document.

  • To specify a field of the ARRAY type, you need to specify the field in the form of a JSON array. Example: [{"fields": { "id": "0","int_array": [14,85],"string_array": ["abc","xyz"]},"cmd": "add"}];

  • In the preceding sample code, the outermost layer is a JSON array that is used to manage multiple documents at a time.

Sample request

A sample API request for data processing, which omits information such as request headers and the encoding method:

http://ha-cn-**********.ha.aliyuncs.com/update/$table_name/actions/bulk

// The data to be uploaded, which needs to be placed in the request body.
[{
	"cmd": "add",
	"fields": {
		"id": 1,
		"name": "Test Data Push"
	}
}]

Sample success response

If no response parameters are returned, the request is successful.

Sample error response

[
    {
        "code": 3012,
        "message": "Resource not found."
    }
]

Usage notes

  • When you push data by calling API operations or using OpenSearch SDK, the field names of the application are not case-sensitive.

  • You can push data for a limited number of times and of a limited size by calling API operations or using OpenSearch SDK. Different limits are imposed on different applications. For more information, see the topics on limits.

  • After you send a request to upload data, you must check the return value. If an error code is returned, troubleshoot errors based on the error code to prevent data losses. Pay special attention to the error code 3007. OpenSearch processes data in an asynchronous manner. A return value of OK indicates that OpenSearch receives data. This does not indicate that the data is properly processed. If an error occurs during data processing, the relevant error message is displayed in the OpenSearch console. Check error messages at the earliest opportunity.

  • You can upload only a limited size of data when you send an HTTP POST request. If the size of data before encoding exceeds 2 MB, OpenSearch rejects the request and returns an error.