Data push demo
Updated at:
Use the Retrieval Engine Edition SDK for Java to push documents to an instance in bulk. The High Availability Edition supports bulk updates to selected attribute fields in existing documents.
add operation example
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
/**
* Retrieval Engine client
*/
private Client client;
@Before
public void clientInit() throws Exception {
Config config = new Config();
// Instance name, shown in the upper-left corner of the instance details page. Example: ha-cn-i7*****605.
config.setInstanceId("ha-cn-i7*****605");
// Username, available in Instance Details > API Endpoint.
config.setAccessUserName("username");
// Password, editable in Instance Details > API Endpoint.
config.setAccessPassWord("password");
// API domain name, available in Instance Details > API Endpoint.
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void add() throws Exception {
// Table name for document pushes
String tableName = "<table_name>";
// Primary key field for document pushes.
String pkField = "<field_pk>";
try {
// Outer structure for document pushes. Add one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Add a document.
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// Insert document content as matching key-value pairs.
// The field_pk field must match the pkField setting.
add2DocumentFields.put("<field_pk>", "<field_pk_value>");
add2DocumentFields.put("<field_map_key_1>", "<field_map_value_1>");
add2DocumentFields.put("<field_map_key_2>", "<field_map_value_2>");
// Multi-value attribute types are configured with "multi_value": true in the index.
ArrayList<Object> addDocumentMultiFields = new ArrayList<>();
addDocumentMultiFields.add("multi_value_1");
addDocumentMultiFields.add("multi_value_2");
add2DocumentFields.put("<multi_value_key>", addDocumentMultiFields);
// Add the document content to add2Document.
add2Document.put("fields", add2DocumentFields);
// Command for adding the document: add
add2Document.put("cmd", "add");
documents.add(add2Document);
// Push data.
PushDocumentsRequestModel requestModel = new PushDocumentsRequestModel();
// By default, document pushes validate that the primary key field exists. To disable validation, set the X-Opensearch-Validate-Data: false request header.
// requestModel.headers = new HashMap<>();
// requestModel.headers.put("X-Opensearch-Validate-Data", "false");
requestModel.setBody(documents);
PushDocumentsResponseModel responseModel = client.pushDocuments(tableName, pkField, requestModel);
String responseBody = responseModel.getBody();
System.out.println("result:" + responseBody);
} catch (TeaException e) {
System.out.println(e.getCode());
System.out.println(e.getMessage());
Map<String, Object> exceptionData = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(exceptionData));
}
}
}
Request body
[
{
"cmd": "add",
"fields": {
"id": "1",
"title": "This is the title",
"body": "This is the body",
"tags" : [1, 2, 3]
}
}
]
update_field operation example
This example applies to the High Availability Edition. The table uses id as the primary key and stock as an INT32 attribute field. The documents with primary keys 1 and 2 already exist. After pushing the updates, query both documents to verify the new values.
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.PushDocumentsRequestModel;
import com.aliyun.ha3engine.models.PushDocumentsResponseModel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class UpdateFields {
public static void main(String[] args) throws Exception {
Config config = new Config()
.setEndpoint("<instance_services_domain>")
.setInstanceId("<instance_id>")
.setProtocol("http")
.setAccessUserName("<user_name>")
.setAccessPassWord("<user_password>");
Client client = new Client(config);
PushDocumentsRequestModel request = new PushDocumentsRequestModel()
.setBody(Arrays.asList(stockUpdate("1", 12), stockUpdate("2", 18)));
PushDocumentsResponseModel response =
client.pushDocuments("<table_name>", "id", request);
System.out.println(response.getBody());
}
private static Map<String, ?> stockUpdate(String id, int stock) {
Map<String, Object> fields = new HashMap<>();
fields.put("id", id);
fields.put("stock", stock);
Map<String, Object> document = new HashMap<>();
document.put("cmd", "update_field");
document.put("fields", fields);
return document;
}
}
delete operation example
package com.aliyun.ha3engine;
import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @author alibaba
*/
public class PushDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// API domain name, available in Instance Details > API Endpoint.
config.setEndpoint("<instance_services_domain>");
// Instance name, shown in the upper-left corner of the instance details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instance_id>");
// Username, available in Instance Details > Network Information.
config.setAccessUserName("<user_name>");
// Password, editable in Instance Details > Network Information.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// Data source configuration name for document pushes, available in Instance Management > Configuration Center > Data Source Configuration.
String tableName = "<instance_datasource_table_name>";
// Primary key field for document pushes.
String pkField = "<field_pk>";
try {
// Outer structure for document pushes. Add one or more document operations.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// Delete a document.
Map<String, Object> delete2Document = new HashMap<>();
Map<String, Object> delete2DocumentFields = new HashMap<>();
// Insert document content as matching key-value pairs.
// The field_pk field must match the pkField setting.
delete2DocumentFields.put("<field_pk>", "<field_pk_value>");
// Add the document content to delete2Document.
delete2Document.put("fields", delete2DocumentFields);
// Command for deleting the document: delete
delete2Document.put("cmd", "delete");
documents.add(delete2Document);
// Push data.
PushDocumentsRequestModel requestModel = new PushDocumentsRequestModel();
requestModel.setBody(documents);
PushDocumentsResponseModel responseModel = client.pushDocuments(tableName, pkField, requestModel);
String responseBody = responseModel.getBody();
System.out.println("result:" + responseBody);
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}
Request body
[
{
"cmd":"delete",
"fields":{
"id":"3"
}
}
]
Is this page helpful?