All Products
Search
Document Center

OpenSearch:Data push demo

Last Updated:Aug 27, 2024

This topic provides sample code to show 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 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 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 modify 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 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 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();
 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
            ]
        }
    }
]

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 modify 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 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"
        }
    }
]