use of org.opensearch.client.opensearch.OpenSearchAsyncClient in project opensearch-java by opensearch-project.
the class RequestTest method errorResponse.
@Test
public void errorResponse() throws Exception {
BooleanResponse exists = highLevelClient().exists(_0 -> _0.index("doesnotexist").id("reallynot"));
assertFalse(exists.value());
OpenSearchException ex = assertThrows(OpenSearchException.class, () -> {
GetResponse<String> response = highLevelClient().get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class);
});
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, () -> {
OpenSearchAsyncClient aClient = new OpenSearchAsyncClient(highLevelClient()._transport());
GetResponse<String> response = aClient.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class).get();
});
ex = ((OpenSearchException) ee.getCause());
assertEquals(404, ex.status());
assertEquals("index_not_found_exception", ex.error().type());
}
use of org.opensearch.client.opensearch.OpenSearchAsyncClient in project opensearch-java by opensearch-project.
the class IndicesClientIT method testCreateIndex.
public void testCreateIndex() throws Exception {
OpenSearchAsyncClient asyncClient = new OpenSearchAsyncClient(highLevelClient()._transport());
CreateIndexResponse createResponse = highLevelClient().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 org.opensearch.client.opensearch.OpenSearchAsyncClient in project opensearch-java by opensearch-project.
the class ApiConventionsTest method blockingAndAsync.
@Test(expected = TransportException.class)
public void blockingAndAsync() throws Exception {
// tag::blocking-and-async
// Synchronous blocking client
OpenSearchClient client = new OpenSearchClient(transport);
if (client.exists(b -> b.index("products").id("foo")).value()) {
logger.info("product exists");
}
// Asynchronous non-blocking client
OpenSearchAsyncClient asyncClient = new OpenSearchAsyncClient(transport);
asyncClient.exists(b -> b.index("products").id("foo")).thenAccept(response -> {
if (response.value()) {
logger.info("product exists");
}
});
// end::blocking-and-async
}
use of org.opensearch.client.opensearch.OpenSearchAsyncClient in project opensearch-java by opensearch-project.
the class RequestTest method testIndexCreation.
@Test
public void testIndexCreation() throws Exception {
OpenSearchAsyncClient asyncClient = new OpenSearchAsyncClient(highLevelClient()._transport());
// Ping the server
assertTrue(highLevelClient().ping().value());
// Create an index...
final CreateIndexResponse createResponse = highLevelClient().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"));
}
Aggregations