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
}
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"));
}
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));
}
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));
}
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()));
}
Aggregations