Search in sources :

Example 21 with RestClient

use of org.opensearch.client.RestClient in project opensearch-java by opensearch-project.

the class ConnectingTest method createClient.

// we don't have a running ES
@Ignore
@Test
public void createClient() throws Exception {
    // tag::create-client
    // Create the low-level client
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
    // Create the transport with a Jackson mapper
    OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    OpenSearchClient client = new OpenSearchClient(transport);
    // end::create-client
    // tag::first-request
    SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
    for (Hit<Product> hit : search.hits().hits()) {
        processProduct(hit.source());
    }
// end::first-request
}
Also used : JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) Ignore(org.junit.Ignore) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) RestClient(org.opensearch.client.RestClient) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) HttpHost(org.apache.http.HttpHost) Hit(org.opensearch.client.opensearch.core.search.Hit) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) HttpHost(org.apache.http.HttpHost) RestClient(org.opensearch.client.RestClient) JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with RestClient

use of org.opensearch.client.RestClient in project opensearch-java by opensearch-project.

the class RequestOptionsTest method testClientHeader.

@Test
public void testClientHeader() throws IOException {
    final RestClientTransport trsp = new RestClientTransport(restClient, new JsonbJsonpMapper());
    final OpenSearchClient client = new OpenSearchClient(trsp).withTransportOptions(trsp.options().with(b -> b.addHeader("X-Foo", "Bar").addHeader("uSer-agEnt", "MegaClient/1.2.3")));
    Properties props = getProps(client);
    assertEquals("Bar", props.getProperty("header-x-foo"));
    assertEquals("MegaClient/1.2.3", props.getProperty("header-user-agent"));
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Properties(java.util.Properties) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) InetAddress(java.net.InetAddress) List(java.util.List) ResponseException(org.opensearch.client.ResponseException) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) Locale(java.util.Locale) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) OutputStreamWriter(java.io.OutputStreamWriter) NameValuePair(org.apache.http.NameValuePair) Assert(org.junit.Assert) HttpHost(org.apache.http.HttpHost) Before(org.junit.Before) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Properties(java.util.Properties) Test(org.junit.Test)

Example 23 with RestClient

use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.

the class DanglingIndicesRestIT method testDanglingIndicesCanBeListed.

/**
 * Check that when dangling indices are discovered, then they can be listed via the REST API.
 */
public void testDanglingIndicesCanBeListed() throws Exception {
    internalCluster().setBootstrapClusterManagerNodeIndex(1);
    internalCluster().startNodes(3, buildSettings(0));
    final DanglingIndexDetails danglingIndexDetails = createDanglingIndices(INDEX_NAME);
    final String stoppedNodeId = mapNodeNameToId(danglingIndexDetails.stoppedNodeName);
    final RestClient restClient = getRestClient();
    final Response listResponse = restClient.performRequest(new Request("GET", "/_dangling"));
    assertOK(listResponse);
    final XContentTestUtils.JsonMapView mapView = createJsonMapView(listResponse.getEntity().getContent());
    assertThat(mapView.get("_nodes.total"), equalTo(3));
    assertThat(mapView.get("_nodes.successful"), equalTo(3));
    assertThat(mapView.get("_nodes.failed"), equalTo(0));
    List<Object> indices = mapView.get("dangling_indices");
    assertThat(indices, hasSize(1));
    assertThat(mapView.get("dangling_indices.0.index_name"), equalTo(INDEX_NAME));
    assertThat(mapView.get("dangling_indices.0.index_uuid"), equalTo(danglingIndexDetails.indexToUUID.get(INDEX_NAME)));
    assertThat(mapView.get("dangling_indices.0.creation_date_millis"), instanceOf(Long.class));
    assertThat(mapView.get("dangling_indices.0.node_ids.0"), equalTo(stoppedNodeId));
}
Also used : Response(org.opensearch.client.Response) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) XContentTestUtils(org.opensearch.test.XContentTestUtils)

Example 24 with RestClient

use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.

the class DanglingIndicesRestIT method testDanglingIndicesCanBeImported.

/**
 * Check that dangling indices can be imported.
 */
public void testDanglingIndicesCanBeImported() throws Exception {
    internalCluster().setBootstrapClusterManagerNodeIndex(1);
    internalCluster().startNodes(3, buildSettings(0));
    createDanglingIndices(INDEX_NAME);
    final RestClient restClient = getRestClient();
    final List<String> danglingIndexIds = listDanglingIndexIds();
    assertThat(danglingIndexIds, hasSize(1));
    final Request importRequest = new Request("POST", "/_dangling/" + danglingIndexIds.get(0));
    importRequest.addParameter("accept_data_loss", "true");
    // Ensure this parameter is accepted
    importRequest.addParameter("timeout", "20s");
    importRequest.addParameter("cluster_manager_timeout", "20s");
    final Response importResponse = restClient.performRequest(importRequest);
    assertThat(importResponse.getStatusLine().getStatusCode(), equalTo(ACCEPTED.getStatus()));
    final XContentTestUtils.JsonMapView mapView = createJsonMapView(importResponse.getEntity().getContent());
    assertThat(mapView.get("acknowledged"), equalTo(true));
    assertTrue("Expected dangling index " + INDEX_NAME + " to be recovered", indexExists(INDEX_NAME));
}
Also used : Response(org.opensearch.client.Response) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) XContentTestUtils(org.opensearch.test.XContentTestUtils)

Example 25 with RestClient

use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.

the class DanglingIndicesRestIT method testDanglingIndicesCanBeDeleted.

/**
 * Check that dangling indices can be deleted. Since this requires that
 * we add an entry to the index graveyard, the graveyard size must be
 * greater than 1. To test deletes, we set the index graveyard size to
 * 1, then create two indices and delete them both while one node in
 * the cluster is stopped. The deletion of the second pushes the deletion
 * of the first out of the graveyard. When the stopped node is resumed,
 * only the second index will be found into the graveyard and the the
 * other will be considered dangling, and can therefore be listed and
 * deleted through the API
 */
public void testDanglingIndicesCanBeDeleted() throws Exception {
    internalCluster().setBootstrapClusterManagerNodeIndex(1);
    internalCluster().startNodes(3, buildSettings(1));
    createDanglingIndices(INDEX_NAME, OTHER_INDEX_NAME);
    final RestClient restClient = getRestClient();
    final List<String> danglingIndexIds = listDanglingIndexIds();
    assertThat(danglingIndexIds, hasSize(1));
    final Request deleteRequest = new Request("DELETE", "/_dangling/" + danglingIndexIds.get(0));
    deleteRequest.addParameter("accept_data_loss", "true");
    // Ensure these parameters is accepted
    deleteRequest.addParameter("timeout", "20s");
    deleteRequest.addParameter("cluster_manager_timeout", "20s");
    final Response deleteResponse = restClient.performRequest(deleteRequest);
    assertThat(deleteResponse.getStatusLine().getStatusCode(), equalTo(ACCEPTED.getStatus()));
    final XContentTestUtils.JsonMapView mapView = createJsonMapView(deleteResponse.getEntity().getContent());
    assertThat(mapView.get("acknowledged"), equalTo(true));
    assertBusy(() -> assertThat("Expected dangling index to be deleted", listDanglingIndexIds(), hasSize(0)));
    // The dangling index that we deleted ought to have been removed from disk. Check by
    // creating and deleting another index, which creates a new tombstone entry, which should
    // not cause the deleted dangling index to be considered "live" again, just because its
    // tombstone has been pushed out of the graveyard.
    createIndex("additional");
    deleteIndex("additional");
    assertThat(listDanglingIndexIds(), is(empty()));
}
Also used : Response(org.opensearch.client.Response) RestClient(org.opensearch.client.RestClient) Request(org.opensearch.client.Request) XContentTestUtils(org.opensearch.test.XContentTestUtils)

Aggregations

RestClient (org.opensearch.client.RestClient)44 Request (org.opensearch.client.Request)20 Response (org.opensearch.client.Response)16 HttpHost (org.apache.http.HttpHost)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)7 Settings (org.opensearch.common.settings.Settings)7 List (java.util.List)6 DefaultScheduler (org.opensearch.client.sniff.Sniffer.DefaultScheduler)6 Scheduler (org.opensearch.client.sniff.Sniffer.Scheduler)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Node (org.opensearch.client.Node)5 Map (java.util.Map)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ResponseException (org.opensearch.client.ResponseException)4 Environment (org.opensearch.env.Environment)4 TestEnvironment (org.opensearch.env.TestEnvironment)4