Search in sources :

Example 71 with Response

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);
}
Also used : Response(org.opensearch.client.Response) MLInput(org.opensearch.ml.common.parameter.MLInput) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 72 with Response

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));
}
Also used : Response(org.opensearch.client.Response) HttpEntity(org.apache.http.HttpEntity) Map(java.util.Map)

Example 73 with Response

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;
}
Also used : Response(org.opensearch.client.Response) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap)

Example 74 with Response

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()));
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Example 75 with Response

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()));
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7