このトピックでは、SDK を使用して OpenSearch Retrieval Engine Edition を呼び出し、データをクエリする方法を示すサンプルコードを提供します。
サンプルコード
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 SearchDoc {
public static void main(String[] args) throws Exception {
Config config = new Config();
// ユーザー名。ユーザー名は、インスタンスの詳細ページの API エンドポイントタブで確認できます。
config.setAccessUserName("<userName>");
// パスワード。パスワードは、インスタンスの詳細ページの API エンドポイントタブで変更できます。
config.setAccessPassWord("<password>");
// インスタンス名。インスタンス名は、インスタンスの詳細ページの左上で確認できます。例: ha-cn-i7*****605。
config.setInstanceId("<instanceID>");
// 内部エンドポイントを使用してインスタンスにアクセスするには、次の行のコメントを外してエンドポイントを指定します。
//config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
// パブリックエンドポイントを使用してインスタンスにアクセスするには、次の行のコメントを外してエンドポイントを指定します。
//config.setEndpoint("ha-cn-******.public.ha.aliyuncs.com");
// インターネット経由でインスタンスにアクセスするには、次の行のコメントを外して HTTP プロキシを指定します。
// config.setHttpProxy("http://Public IP address:Port number");
Client client = new Client(config);
try {
/*
例: RESTful API を使用してデータをクエリします。
*/
// リクエストボディでクエリパラメータを指定します。
JSONObject body = new JSONObject();
body.put("query", "index_id:1");
JSONObject configJson = new JSONObject();
configJson.put("format", "json");
body.put("config", configJson);
// インデックステーブルの名前。
String indexName = "index_odps";
SearchRequestModel haQueryRequestModel = new SearchRequestModel();
haQueryRequestModel.setBody(body.toJSONString());
SearchResponseModel searchResponseModel = client.SearchRest(haQueryRequestModel, indexName);
System.out.println("result:" + searchResponseModel.getBody());
} catch (TeaException e) {
System.out.println(e.getMessage());
Map<String, Object> abc = e.getData();
System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
}
}
}