use of co.elastic.clients.elasticsearch.ElasticsearchAsyncClient in project elasticsearch-java by elastic.
the class RequestTest method testIndexCreation.
@Test
public void testIndexCreation() throws Exception {
ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient(client._transport());
// Ping the server
assertTrue(client.ping().value());
// Create an index...
final CreateIndexResponse createResponse = client.indices().create(b -> b.index("my-index"));
assertTrue(createResponse.acknowledged());
assertTrue(createResponse.shardsAcknowledged());
// Find info about it, using the async client
CompletableFuture<GetIndexResponse> futureResponse = asyncClient.indices().get(b -> b.index("my-index"));
GetIndexResponse response = futureResponse.get(10, TimeUnit.SECONDS);
Map<String, IndexState> indices = response.result();
assertEquals(1, indices.size());
assertNotNull(indices.get("my-index"));
}
use of co.elastic.clients.elasticsearch.ElasticsearchAsyncClient in project elasticsearch-java by elastic.
the class RequestTest method errorResponse.
@Test
public void errorResponse() throws Exception {
BooleanResponse exists = client.exists(_0 -> _0.index("doesnotexist").id("reallynot"));
assertFalse(exists.value());
ElasticsearchException ex = assertThrows(ElasticsearchException.class, () -> {
GetResponse<String> response = client.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class);
});
assertEquals("es/get", ex.endpointId());
assertEquals(404, ex.status());
assertEquals("index_not_found_exception", ex.error().type());
assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class));
ExecutionException ee = assertThrows(ExecutionException.class, () -> {
ElasticsearchAsyncClient aClient = new ElasticsearchAsyncClient(client._transport());
GetResponse<String> response = aClient.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class).get();
});
ex = ((ElasticsearchException) ee.getCause());
assertEquals("es/get", ex.endpointId());
assertEquals(404, ex.status());
assertEquals("index_not_found_exception", ex.error().type());
}
use of co.elastic.clients.elasticsearch.ElasticsearchAsyncClient in project para-search-elasticsearch by Erudika.
the class ESUtils method getRESTClient.
/**
* Creates an instance of the high-level REST client that talks to Elasticsearch.
* @return a RestHighLevelClient instance
*/
public static ElasticsearchClient getRESTClient() {
if (restClient != null) {
return restClient;
}
String esScheme = Para.getConfig().getConfigParam("es.restclient_scheme", Para.getConfig().inProduction() ? "https" : "http");
String esHost = Para.getConfig().getConfigParam("es.restclient_host", "localhost");
int esPort = Para.getConfig().getConfigInt("es.restclient_port", 9200);
boolean signRequests = Para.getConfig().getConfigBoolean("es.sign_requests_to_aws", esHost.contains("amazonaws.com"));
HttpHost host = new HttpHost(esHost, esPort, esScheme);
RestClientBuilder clientBuilder = RestClient.builder(host);
String esPrefix = Para.getConfig().getConfigParam("es.restclient_context_path", "");
if (StringUtils.isNotEmpty(esPrefix)) {
clientBuilder.setPathPrefix(esPrefix);
}
List<RestClientBuilder.HttpClientConfigCallback> configurationCallbacks = new ArrayList<>();
if (signRequests) {
configurationCallbacks.add(getAWSRequestSigningInterceptor(host.getSchemeName() + "://" + host.getHostName()));
}
configurationCallbacks.add(getAuthenticationCallback());
// register all customizations
clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
configurationCallbacks.forEach(c -> c.customizeHttpClient(httpClientBuilder));
return httpClientBuilder;
});
// Create the transport with a Jackson mapper
RestClientTransport transport = new RestClientTransport(clientBuilder.build(), new JacksonJsonpMapper());
restClient = new ElasticsearchClient(transport);
restClientAsync = new ElasticsearchAsyncClient(transport);
Para.addDestroyListener(new DestroyListener() {
public void onDestroy() {
shutdownClient();
}
});
if (!existsIndex(Para.getConfig().getRootAppIdentifier())) {
createIndex(Para.getConfig().getRootAppIdentifier());
}
return restClient;
}
use of co.elastic.clients.elasticsearch.ElasticsearchAsyncClient in project elasticsearch-java by elastic.
the class ApiConventionsTest method blockingAndAsync.
@Test(expected = TransportException.class)
public void blockingAndAsync() throws Exception {
// tag::blocking-and-async
// Synchronous blocking client
ElasticsearchClient client = new ElasticsearchClient(transport);
if (client.exists(b -> b.index("products").id("foo")).value()) {
logger.info("product exists");
}
// Asynchronous non-blocking client
ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient(transport);
asyncClient.exists(b -> b.index("products").id("foo")).thenAccept(response -> {
if (response.value()) {
logger.info("product exists");
}
});
// end::blocking-and-async
}
Aggregations