Search in sources :

Example 1 with OpenSearchAsyncClient

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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) ExecutionException(java.util.concurrent.ExecutionException) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Example 2 with OpenSearchAsyncClient

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"));
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) IndexState(org.opensearch.client.opensearch.indices.IndexState)

Example 3 with OpenSearchAsyncClient

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
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) Alias(org.opensearch.client.opensearch.indices.Alias) Query(org.opensearch.client.opensearch._types.query_dsl.Query) CreateIndexRequest(org.opensearch.client.opensearch.indices.CreateIndexRequest) LogManager(java.util.logging.LogManager) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) HashMap(java.util.HashMap) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Logger(java.util.logging.Logger) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) TransportException(org.opensearch.client.transport.TransportException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) NodeStatistics(org.opensearch.client.opensearch._types.NodeStatistics) SortOrder(org.opensearch.client.opensearch._types.SortOrder) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) Assert(org.junit.Assert) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test)

Example 4 with OpenSearchAsyncClient

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"));
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test)

Aggregations

OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)4 CreateIndexResponse (org.opensearch.client.opensearch.indices.CreateIndexResponse)4 Test (org.junit.Test)3 GetIndexResponse (org.opensearch.client.opensearch.indices.GetIndexResponse)3 IndexState (org.opensearch.client.opensearch.indices.IndexState)3 Map (java.util.Map)2 SearchResponse (org.opensearch.client.opensearch.core.SearchResponse)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 LogManager (java.util.logging.LogManager)1 Logger (java.util.logging.Logger)1 Assert (org.junit.Assert)1 OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)1 NodeStatistics (org.opensearch.client.opensearch._types.NodeStatistics)1