This topic provides sample code to show you how to use OpenSearch Vector Search Edition SDK for Java clients to synchronize data to an OpenSearch Vector Search Edition instance in real time. You can upload, update, and delete documents.
Upload 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. You can view the API endpoint on the API Endpoint tab of the Instance Details page.
config.setEndpoint("<instance_services_domain>");
// The name of the OpenSearch 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 on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<user_name>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// The name of the data source from which you want to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side 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 structure added to specify document operations in the outer structure that is used to push document data. 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 must be paired with values.
// 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-valued attribute types supported by OpenSearch Vector Search 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();
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":"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. You can view the API endpoint on the API Endpoint tab of the Instance Details page.
config.setEndpoint("<instance_services_domain>");
// The name of the OpenSearch 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 on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<user_name>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// The name of the data source from which you want to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side 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 structure added to specify document operations in the outer structure that is used to push document data. 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 must be paired with values.
// 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-valued attribute types supported by OpenSearch Vector Search 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. You can view the API endpoint on the API Endpoint tab of the Instance Details page.
config.setEndpoint("<instance_services_domain>");
// The name of the OpenSearch 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 on the API Endpoint tab of the Instance Details page.
config.setAccessUserName("<user_name>");
// The password. You can change the password on the API Endpoint tab of the Instance Details page.
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// The name of the data source from which you want to push document data. To view the data source name, go to the Instance Details page in the OpenSearch console. In the left-side 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 structure added to specify document operations in the outer structure that is used to push document data. 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 must be paired with values.
// 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"
}
}
]