Search in sources :

Example 1 with ElasticsearchAsyncClient

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"));
}
Also used : ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) Test(org.junit.Test)

Example 2 with ElasticsearchAsyncClient

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());
}
Also used : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) BeforeClass(org.junit.BeforeClass) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) CompletableFuture(java.util.concurrent.CompletableFuture) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) DateTime(co.elastic.clients.util.DateTime) Map(java.util.Map) OperationType(co.elastic.clients.elasticsearch.core.bulk.OperationType) ModelTestCase(co.elastic.clients.elasticsearch.model.ModelTestCase) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Refresh(co.elastic.clients.elasticsearch._types.Refresh) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) NodesResponse(co.elastic.clients.elasticsearch.cat.NodesResponse) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ElasticsearchTestServer(co.elastic.clients.elasticsearch.ElasticsearchTestServer) Test(org.junit.Test) IOException(java.io.IOException) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) Instant(java.time.Instant) GetIndicesSettingsResponse(co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) MsearchResponse(co.elastic.clients.elasticsearch.core.MsearchResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClosePointInTimeResponse(co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) DiskUsageResponse(co.elastic.clients.elasticsearch.indices.DiskUsageResponse) ClearScrollResponse(co.elastic.clients.elasticsearch.core.ClearScrollResponse) Assert(org.junit.Assert) Collections(java.util.Collections) IndexResponse(co.elastic.clients.elasticsearch.core.IndexResponse) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) ExecutionException(java.util.concurrent.ExecutionException) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Example 3 with ElasticsearchAsyncClient

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;
}
Also used : ArrayList(java.util.ArrayList) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) DestroyListener(com.erudika.para.core.listeners.DestroyListener) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) HttpHost(org.apache.http.HttpHost) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient)

Example 4 with ElasticsearchAsyncClient

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
}
Also used : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) Arrays(java.util.Arrays) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) LogManager(java.util.logging.LogManager) TransportException(co.elastic.clients.transport.TransportException) Test(org.junit.Test) HashMap(java.util.HashMap) NodeStatistics(co.elastic.clients.elasticsearch._types.NodeStatistics) Logger(java.util.logging.Logger) Aggregation(co.elastic.clients.elasticsearch._types.aggregations.Aggregation) Alias(co.elastic.clients.elasticsearch.indices.Alias) List(java.util.List) ElasticsearchTransport(co.elastic.clients.transport.ElasticsearchTransport) CreateIndexRequest(co.elastic.clients.elasticsearch.indices.CreateIndexRequest) ApiTypeHelper(co.elastic.clients.util.ApiTypeHelper) Map(java.util.Map) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) Assert(org.junit.Assert) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) Test(org.junit.Test)

Aggregations

ElasticsearchAsyncClient (co.elastic.clients.elasticsearch.ElasticsearchAsyncClient)4 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)3 CreateIndexResponse (co.elastic.clients.elasticsearch.indices.CreateIndexResponse)3 Test (org.junit.Test)3 SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)2 GetIndexResponse (co.elastic.clients.elasticsearch.indices.GetIndexResponse)2 IndexState (co.elastic.clients.elasticsearch.indices.IndexState)2 Map (java.util.Map)2 ElasticsearchTestServer (co.elastic.clients.elasticsearch.ElasticsearchTestServer)1 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 NodeStatistics (co.elastic.clients.elasticsearch._types.NodeStatistics)1 Refresh (co.elastic.clients.elasticsearch._types.Refresh)1 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)1 Aggregation (co.elastic.clients.elasticsearch._types.aggregations.Aggregation)1 HistogramAggregate (co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate)1 Property (co.elastic.clients.elasticsearch._types.mapping.Property)1 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)1 NodesResponse (co.elastic.clients.elasticsearch.cat.NodesResponse)1 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)1 ClearScrollResponse (co.elastic.clients.elasticsearch.core.ClearScrollResponse)1