This topic describes the syntax of the requests that are initiated to upload and delete documents.
URL
/update/$table_name/actions/bulk
Specify $table_name in the Instance ID_Table name format. For example, if the instance ID is ha-843qfng6ydhb and the table name is test, set $table_name to ha-843qfng6ydhb_test.
The sample URL omits information such as the request headers and the encoding method.
The sample URL also omits the endpoint that is used to connect to an OpenSearch application.
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 the 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. |
Request signature
You can use the following method to calculate the request signature. The request signature is stored in the authorization header.
Parameter | 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 modify 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 format for the value of the authorization header:
cm9vdDp******mdhbA==
Note: You must add the Basic prefix when you specify the authorization header in an HTTP request.
Example:
authorization: Basic cm9vdDp******mdhbA==
Request body for document update
[
{
"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. 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 the document is to be uploaded. If a document with the specified primary key value exists, the original document is deleted before the new document is uploaded. A value of delete specifies that the document is to be deleted. If no document with the specified primary key value 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 specify only the primary key field of the document.
To specify a field of the ARRAY type, you must specify the field in a JSON array. Example: [{"fields": { "id": "0","int_array": [14,85],"string_array": ["abc","xyz"]},"cmd": "add"}].
In the 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 update, 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 related topics of the limits.
After the data is uploaded, you must check the return values. If an error code is returned, especially 3007, troubleshoot the error based on the error code and try again. Otherwise, data loss may occur. OpenSearch processes data in an asynchronous way. A return value of OK indicates that OpenSearch receives the data. This does not indicate that 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 your 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.