このトピックでは、OpenSearch Vector Search Edition SDK for Javaクライアントを使用して、OpenSearch Vector Search Editionインスタンスにリアルタイムでデータを同期する方法を示すサンプルコードを提供します。ドキュメントのアップロード、更新、削除ができます。
ドキュメントのアップロード
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エンドポイント。APIエンドポイントは、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setEndpoint("<instance_services_domain>");
// OpenSearchインスタンスの名前。インスタンス名は、インスタンスの詳細ページの左上隅で確認できます。例:ha-cn-i7*****605。
config.setInstanceId("<instance_id>");
// ユーザー名。ユーザー名は、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setAccessUserName("<user_name>");
// パスワード。パスワードは、インスタンスの詳細ページの[APIエンドポイント]タブで変更できます。
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// ドキュメントデータをプッシュするデータソースの名前。データソース名を確認するには、OpenSearchコンソールのインスタンスの詳細ページに移動します。左側のペインで、[構成センター]>[データソース]を選択します。データソースページでデータソース名を確認できます。
String tableName = "<instance_datasource_table_name>";
// プッシュするドキュメントのプライマリキーフィールド。
String pkField = "<field_pk>";
try {
// ドキュメントデータをプッシュするために使用する外部構造に、ドキュメント操作を指定するために追加された構造。この構造には、1つ以上のドキュメント操作を指定できます。
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// アップロードするドキュメント。
Map<String, Object> add2Document = new HashMap<>();
Map<String, Object> add2DocumentFields = new HashMap<>();
// ドキュメントの内容。キーと値はペアにする必要があります。
// field_pkフィールドの値は、pkFieldフィールドの値と同じである必要があります。
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>");
// このコンテンツは、OpenSearch Vector Search Editionでサポートされている複数値属性タイプにすることができます。インデックステーブルを構成するときに、multi_valueパラメーターをtrueに設定します。
ArrayList<Object> addDocumentMultiFields = new ArrayList<>();
addDocumentMultiFields.add("multi_value_1");
addDocumentMultiFields.add("multi_value_2");
add2DocumentFields.put("<multi_value_key>", addDocumentMultiFields);
// ドキュメントの内容をadd2Document構造に追加します。
add2Document.put("fields", add2DocumentFields);
// addコマンドを実行してドキュメントをアップロードします。
add2Document.put("cmd", "add");
documents.add(add2Document);
// データをプッシュします。
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));
}
}
}サンプル構造
[
{
"cmd":"add",
"fields":{
"id":"1",
"title":"This is the title",
"body":"This is the body",
"tags":[
1,
2,
3
]
}
}
]ドキュメントの更新
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エンドポイント。APIエンドポイントは、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setEndpoint("<instance_services_domain>");
// OpenSearchインスタンスの名前。インスタンス名は、インスタンスの詳細ページの左上隅で確認できます。例:ha-cn-i7*****605。
config.setInstanceId("<instance_id>");
// ユーザー名。ユーザー名は、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setAccessUserName("<user_name>");
// パスワード。パスワードは、インスタンスの詳細ページの[APIエンドポイント]タブで変更できます。
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// ドキュメントデータをプッシュするデータソースの名前。データソース名を確認するには、OpenSearchコンソールのインスタンスの詳細ページに移動します。左側のペインで、[構成センター]>[データソース]を選択します。データソースページでデータソース名を確認できます。
String tableName = "<instance_datasource_table_name>";
// プッシュするドキュメントのプライマリキーフィールド。
String pkField = "<field_pk>";
try {
// ドキュメントデータをプッシュするために使用する外部構造に、ドキュメント操作を指定するために追加された構造。この構造には、1つ以上のドキュメント操作を指定できます。
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// 更新するドキュメント。
Map<String, Object> update2Document = new HashMap<>();
Map<String, Object> update2DocumentFields = new HashMap<>();
// ドキュメントの内容。キーと値はペアにする必要があります。
// field_pkフィールドの値は、pkFieldフィールドの値と同じである必要があります。
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>");
// このコンテンツは、OpenSearch Vector Search Editionでサポートされている複数値属性タイプにすることができます。インデックステーブルを構成するときに、multi_valueパラメーターをtrueに設定します。
ArrayList<Object> updateDocumentMultiFields = new ArrayList<>();
updateDocumentMultiFields.add("multi_value_1");
updateDocumentMultiFields.add("multi_value_2");
update2DocumentFields.put("<multi_value_key>", updateDocumentMultiFields);
// ドキュメントの内容をupdate2Document構造に追加します。
update2Document.put("fields", update2DocumentFields);
// updateコマンドを実行してドキュメントを更新します。
update2Document.put("cmd", "update");
documents.add(update2Document);
// データをプッシュします。
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));
}
}
}サンプル構造
[
{
"cmd":"update_field",
"fields":{
"id":"2",
"title":"This is the new title"
}
}
]ドキュメントの削除
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エンドポイント。APIエンドポイントは、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setEndpoint("<instance_services_domain>");
// OpenSearchインスタンスの名前。インスタンス名は、インスタンスの詳細ページの左上隅で確認できます。例:ha-cn-i7*****605。
config.setInstanceId("<instance_id>");
// ユーザー名。ユーザー名は、インスタンスの詳細ページの[APIエンドポイント]タブで確認できます。
config.setAccessUserName("<user_name>");
// パスワード。パスワードは、インスタンスの詳細ページの[APIエンドポイント]タブで変更できます。
config.setAccessPassWord("<user_password>");
Client client = new Client(config);
// ドキュメントデータをプッシュするデータソースの名前。データソース名を確認するには、OpenSearchコンソールのインスタンスの詳細ページに移動します。左側のペインで、[構成センター]>[データソース]を選択します。データソースページでデータソース名を確認できます。
String tableName = "<instance_datasource_table_name>";
// プッシュするドキュメントのプライマリキーフィールド。
String pkField = "<field_pk>";
try {
// ドキュメントデータをプッシュするために使用する外部構造に、ドキュメント操作を指定するために追加された構造。この構造には、1つ以上のドキュメント操作を指定できます。
ArrayList<Map<String, ?>> documents = new ArrayList<>();
// 削除するドキュメント。
Map<String, Object> delete2Document = new HashMap<>();
Map<String, Object> delete2DocumentFields = new HashMap<>();
// ドキュメントの内容。キーと値はペアにする必要があります。
// field_pkフィールドの値は、pkFieldフィールドの値と同じである必要があります。
delete2DocumentFields.put("<field_pk>", "<field_pk_value>");
// ドキュメントの内容をdelete2Document構造に追加します。
delete2Document.put("fields", delete2DocumentFields);
// deleteコマンドを実行してドキュメントを削除します。
delete2Document.put("cmd", "delete");
documents.add(delete2Document);
// データをプッシュします。
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));
}
}
}サンプル構造
[
{
"cmd":"delete",
"fields":{
"id":"3"
}
}
]