This topic provides sample code to show you how to use OpenSearch Retrieval Engine Edition SDK for Java clients to synchronize data to an OpenSearch Retrieval Engine Edition instance in real time. You can upload, update, and delete documents.
Upload a document
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 {
/**
* The engine client of the Retrieval Engine Edition instance.
*/
private Client client;
@Before
public void clientInit() throws Exception {
Config config = new Config();
// The name of the instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("ha-cn-i7*****605");
// The username. You can view the username in the API Endpoint section of the Instance Details page.
config.setAccessUserName("username");
// The password. You can modify the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("password");
// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
config.setEndpoint("ha-cn-i7*****605.public.ha.aliyuncs.com");
client = new Client(config);
}
@Test
public void add() throws Exception {
// The table name of the document whose data is to be pushed.
String tableName = "<table_name>";
// The primary key field of the document whose data is to be pushed.
String pkField = "<field_pk>";
try {
// The outer structure that is used to push documents. You can specify one or more document operations in the structure.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// The document to be uploaded.
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// The content of the document. Keys and values must be matched in pairs.
// The value of the field_pk field must be the same as the value of the pkField field.
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>");
// The content can be of multi-value attribute types supported by OpenSearch Retrieval Engine Edition. Set the multi_value parameter to true when you configure an index table.
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 an add2Document structure.
add2Document.put("fields", add2DocumentFields);
// Run the add command to upload the document.
add2Document.put("cmd", "add");
documents.add(add2Document);
// Push data.
PushDocumentsRequestModel requestModel = new PushDocumentsRequestModel();
// By default, whether the primary key field exists is checked when data is pushed. To disable the check, set the request header X-Opensearch-Validate-Data to false.
// 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));
}
}
}Sample structure
[
{
"cmd": "add",
"fields": {
"id": "1",
"title": "This is the title",
"body": "This is the body",
"tags" : [1, 2, 3]
}
}
]Update a document
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();
// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
config.setEndpoint("<instance_services_domain>");
// The name of the instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instance_id>");
// The username. You can view the username in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<user_name>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// The name of the data source to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side navigation pane, choose Configuration Center > Data Source. You can view the data source name on the Data Source page.
String tableName = "<instance_datasource_table_name>";
// The primary key field of the document whose data is to be pushed.
String pkField = "<field_pk>";
try {
// The outer structure that is used to push documents. You can specify one or more document operations in the structure.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// The document to be updated.
Map<String, Object> update2Document = new HashMap<>();
Map<String, Object> update2DocumentFields = new HashMap<>();
// The content of the document. Keys and values must be matched in pairs.
// The value of the field_pk field must be the same as the value of the pkField field.
update2DocumentFields.put("<field_pk>", "<field_pk_value>");
update2DocumentFields.put("<field_map_key_1>", "<field_map_value_1>");
update2DocumentFields.put("<field_map_key_2>", "<field_map_value_2>");
// The content can be of multi-value attribute types supported by OpenSearch Retrieval Engine Edition. Set the multi_value parameter to true when you configure an index table.
ArrayList<Object> updateDocumentMultiFields = new ArrayList<>();
updateDocumentMultiFields.add("multi_value_1");
updateDocumentMultiFields.add("multi_value_2");
update2DocumentFields.put("<multi_value_key>", updateDocumentMultiFields);
// Add the document content to an update2Document structure.
update2Document.put("fields", update2DocumentFields);
// Run the update command to update the document.
update2Document.put("cmd", "update");
documents.add(update2Document);
// 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));
}
}
}Sample structure
[
{
"cmd": "update_field",
"fields": {
"id": "2",
"title": "This is the new title"
}
}
]Delete a document
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();
// The API endpoint of the instance. You can view the API endpoint in the API Endpoint section of the Instance Details page.
config.setEndpoint("<instance_services_domain>");
// The name of the instance. You can view the instance name in the upper-left corner of the Instance Details page. Example: ha-cn-i7*****605.
config.setInstanceId("<instance_id>");
// The username. You can view the username in the API Endpoint section of the Instance Details page.
config.setAccessUserName("<user_name>");
// The password. You can change the password in the API Endpoint section of the Instance Details page.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// The name of the data source to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side navigation pane, choose Configuration Center > Data Source. You can view the data source name on the Data Source page.
String tableName = "<instance_datasource_table_name>";
// The primary key field of the document whose data is to be pushed.
String pkField = "<field_pk>";
try {
// The outer structure that is used to push documents. You can specify one or more document operations in the structure.
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// The document to be deleted.
Map<String, Object> delete2Document = new HashMap<>();
Map<String, Object> delete2DocumentFields = new HashMap<>();
// The content of the document. Keys and values must be matched in pairs.
// The value of the field_pk field must be the same as the value of the pkField field.
delete2DocumentFields.put("<field_pk>", "<field_pk_value>");
// Add the document content to a delete2Document structure.
delete2Document.put("fields", delete2DocumentFields);
// Run the delete command to delete the document.
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));
}
}
}Sample structure
[
{
"cmd":"delete",
"fields":{
"id":"3"
}
}
]