use of org.opensearch.client.Response in project ml-commons by opensearch-project.
the class RestMLTrainAndPredictIT method trainAndPredictKmeansWithIrisData.
private void trainAndPredictKmeansWithIrisData(KMeansParams params, MLInputDataset inputData, Consumer<Map<Double, Integer>> function) throws IOException {
MLInput kmeansInput = MLInput.builder().algorithm(FunctionName.KMEANS).parameters(params).inputDataset(inputData).build();
Response kmeansResponse = TestHelper.makeRequest(client(), "POST", "/_plugins/_ml/_train_predict/kmeans", ImmutableMap.of(), TestHelper.toHttpEntity(kmeansInput), null);
HttpEntity entity = kmeansResponse.getEntity();
assertNotNull(kmeansResponse);
String entityString = TestHelper.httpEntityToString(entity);
Map map = gson.fromJson(entityString, Map.class);
Map predictionResult = (Map) map.get("prediction_result");
ArrayList rows = (ArrayList) predictionResult.get("rows");
Map<Double, Integer> clusterCount = new HashMap<>();
for (Object obj : rows) {
Double value = (Double) ((Map) ((ArrayList) ((Map) obj).get("values")).get(0)).get("value");
if (!clusterCount.containsKey(value)) {
clusterCount.put(value, 1);
} else {
Integer count = clusterCount.get(value);
clusterCount.put(value, ++count);
}
}
function.accept(clusterCount);
}
use of org.opensearch.client.Response in project ml-commons by opensearch-project.
the class RestMLSearchModelActionIT method testSearchModelAPI_Success.
public void testSearchModelAPI_Success() throws IOException {
Response trainModelResponse = ingestModelData();
HttpEntity entity = trainModelResponse.getEntity();
assertNotNull(trainModelResponse);
String entityString = TestHelper.httpEntityToString(entity);
Map map = gson.fromJson(entityString, Map.class);
String model_id = (String) map.get("model_id");
Response searchModelResponse = TestHelper.makeRequest(client(), "GET", "/_plugins/_ml/models/_search", null, matchAllSearchQuery(), null);
assertNotNull(searchModelResponse);
assertEquals(RestStatus.OK, TestHelper.restStatus(searchModelResponse));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class OpenSearchRestTestCase method minimumNodeVersion.
/**
* Returns the minimum node version among all nodes of the cluster
*/
protected static Version minimumNodeVersion() throws IOException {
final Request request = new Request("GET", "_nodes");
request.addParameter("filter_path", "nodes.*.version");
final Response response = adminClient().performRequest(request);
final Map<String, Object> nodes = ObjectPath.createFromResponse(response).evaluate("nodes");
Version minVersion = null;
for (Map.Entry<String, Object> node : nodes.entrySet()) {
@SuppressWarnings("unchecked") Version nodeVersion = Version.fromString((String) ((Map<String, Object>) node.getValue()).get("version"));
if (minVersion == null || minVersion.after(nodeVersion)) {
minVersion = nodeVersion;
}
}
assertNotNull(minVersion);
return minVersion;
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class OpenSearchRestTestCase method openIndex.
protected static void openIndex(String index) throws IOException {
Response response = client().performRequest(new Request("POST", "/" + index + "/_open"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class OpenSearchRestTestCase method restoreSnapshot.
protected static void restoreSnapshot(String repository, String snapshot, boolean waitForCompletion) throws IOException {
final Request request = new Request(HttpPost.METHOD_NAME, "_snapshot/" + repository + '/' + snapshot + "/_restore");
request.addParameter("wait_for_completion", Boolean.toString(waitForCompletion));
final Response response = client().performRequest(request);
assertThat("Failed to restore snapshot [" + snapshot + "] from repository [" + repository + "]: " + response, response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
}
Aggregations