io.searchbox.client.JestClient Java Examples

The following examples show how to use io.searchbox.client.JestClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JestClientHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchExecuteWithException() throws Exception
{
    // Mock
    Search search = mock(Search.class);
    JestClient jestClient = mock(JestClient.class);
    when(jestClientFactory.getJestClient()).thenReturn(jestClient);
    when(jestClient.execute(search)).thenThrow(new IOException());

    try
    {
        // Test
        jestClientHelper.execute(search);
    }
    catch (RuntimeException runtimeException)
    {
        // Validate
        assertThat(runtimeException, is(instanceOf(RuntimeException.class)));
    }

    // Verify
    verify(jestClientFactory).getJestClient();
    verify(jestClient).execute(search);
    verifyNoMoreInteractions(createdMocks.toArray());
}
 
Example #2
Source File: Jest.java    From easy-sync with Apache License 2.0 6 votes vote down vote up
private JestClient createJestClient(Es es) {
    JestClientFactory factory = new JestClientFactory();
    List<String> addresses = Arrays.asList(es.getAddresses().split(";"));
    factory.setHttpClientConfig(new HttpClientConfig
            .Builder(addresses)// "http://localhost:9200")
            .multiThreaded(true)
            .connTimeout(10 * 000)
            .readTimeout(10 * 000)
            //Per default this implementation will create no more than 2 concurrent connections per given route
            // .defaultMaxTotalConnectionPerRoute(<YOUR_DESIRED_LEVEL_OF_CONCURRENCY_PER_ROUTE>)
            // and no more 20 connections in total
            // .maxTotalConnection(<YOUR_DESIRED_LEVEL_OF_CONCURRENCY_TOTAL>)
            .build());
    JestClient jestClient = factory.getObject();
    return jestClient;
}
 
Example #3
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 6 votes vote down vote up
private synchronized JestResult scrollResults(String scrollId, int retry, JestClient client) throws InterruptedException {
    if (client != null) {
        final SearchScroll scroll = new SearchScroll.Builder(scrollId, SCROLL_TIME_SESSION).build();
        try {
            final JestResult result = client.execute(scroll);
            log.debug("Completed scroll query. Succeeded: {}", result.isSucceeded());
            return result;
        } catch (IOException e) {
            log.warn("Error in scroll request query: {}", e.getMessage(), e);
            if(retry > ES_MAX_TRIES) {
                log.error("Error in scroll request: reached maximum number of scroll tries [{}].", retry);
                throw new RuntimeException("Error in scroll request query: " + e.getMessage(), e);

            } else {
                Thread.sleep((retry + 1) * ES_WAIT_TIME);
                return scrollResults(scrollId, retry + 1, client);
            }
        }
    }
    log.error("Error in scroll request query: ES client has not been initialized, client is null.");
    throw new RuntimeException("Error in scroll request query: ES client has not been initialized, client is null.");
}
 
Example #4
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 6 votes vote down vote up
private void bulkUpdate(Bulk.Builder bulkProcessor, int retries, JestClient client) throws InterruptedException {
    if (Objects.nonNull(client)) {
        try {
            BulkResult result = client.execute(bulkProcessor.build());
            if (result.getFailedItems().size() > 0) {
                final String errorIds = result.getFailedItems().stream()
                            .map( fi -> fi.id)
                            .collect(Collectors.joining(", "));
                log.error("Error executing bulk update: {} items where no updated [{}].", result.getFailedItems().size(), errorIds);

            }
        } catch (IOException e) {
            log.warn("Error executing bulk update: {}", e.getMessage(), e);
            if (retries > ES_MAX_TRIES) {
                log.error("Error executing bulk update: reached maximum number of retries [{}].", retries);
                throw new RuntimeException("Error executing bulk update: " + e.getMessage(), e);
            } else {
                Thread.sleep((retries + 1) * ES_WAIT_TIME);
                bulkUpdate(bulkProcessor, retries + 1, client);
            }
        }
    } else {
        log.error("Error in bulk update request: ES client has not been initialized, client is null.");
        throw new RuntimeException("Error in bulk update request: ES client has not been initialized, client is null.");
    }
}
 
Example #5
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public JestClient createClient() {
    if (client == null) {

        HttpClientConfig.Builder builder = new HttpClientConfig.Builder(serverUris)
                .maxTotalConnection(maxTotalConnections)
                .defaultMaxTotalConnectionPerRoute(defaultMaxTotalConnectionsPerRoute)
                .connTimeout(connTimeout)
                .readTimeout(readTimeout)
                .discoveryEnabled(discoveryEnabled)
                .multiThreaded(true);

        if (this.auth != null) {
            auth.configure(builder);
        }

        WrappedHttpClientConfig.Builder wrappedHttpClientConfigBuilder =
                new WrappedHttpClientConfig.Builder(builder.build())
                        .ioThreadCount(ioThreadCount);

        client = getClientProvider(wrappedHttpClientConfigBuilder).createClient();
    }
    return client;
}
 
Example #6
Source File: JestClientFactoryTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJestClientHttps()
{
    // Mock the external calls.
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_AWS_REGION_NAME)).thenReturn(AWS_REGION_NAME);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_HOSTNAME)).thenReturn(ELASTICSEARCH_HOSTNAME);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_PORT, Integer.class)).thenReturn(ELASTICSEARCH_PORT);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_SCHEME)).thenReturn("https");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_READ_TIMEOUT, Integer.class)).thenReturn(READ_TIMEOUT);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_CONNECTION_TIMEOUT, Integer.class)).thenReturn(CONNECTION_TIMEOUT);

    // Call the method under test.
    JestClient jestClient = jestClientFactory.getJestClient();

    // Verify the external calls.
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_AWS_REGION_NAME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_HOSTNAME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_PORT, Integer.class);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_SCHEME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_READ_TIMEOUT, Integer.class);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_CONNECTION_TIMEOUT, Integer.class);
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertNotNull(jestClient);
}
 
Example #7
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failureHandlerExecutesFailoverForEachBatchItemSeparately() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<JestClient, Bulk> config = builder.build();

    FailoverPolicy failoverPolicy = Mockito.spy(new NoopFailoverPolicy());

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = new Bulk.Builder()
            .addAction(spy(new Index.Builder(payload1)).build())
            .addAction(spy(new Index.Builder(payload2)).build())
            .build();

    // when
    config.createFailureHandler(failoverPolicy).apply(bulk);

    // then
    ArgumentCaptor<FailedItemSource> captor = ArgumentCaptor.forClass(FailedItemSource.class);
    verify(failoverPolicy, times(2)).deliver(captor.capture());

    assertTrue(captor.getAllValues().get(0).getSource().equals(payload1));
    assertTrue(captor.getAllValues().get(1).getSource().equals(payload2));
}
 
Example #8
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void clientIsCalledWhenListenerIsNotified() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<JestClient, Bulk> config = spy(builder.build());

    JestClient mockedJestClient = mock(JestClient.class);
    when(config.createClient()).thenReturn(mockedJestClient);

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
    Function<Bulk, Boolean> listener = config.createBatchListener(failoverPolicy);

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = createTestBatch(payload1, payload2);

    // when
    listener.apply(bulk);

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(mockedJestClient, times(1)).executeAsync((Bulk) captor.capture(), Mockito.any());

    assertEquals(bulk, captor.getValue());
}
 
Example #9
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void configReturnsACopyOfServerUrisList() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    builder.withServerUris("http://localhost:9200;http://localhost:9201;http://localhost:9202");
    ClientObjectFactory<JestClient, Bulk> config = builder.build();

    // when
    Collection<String> serverUrisList = config.getServerList();
    serverUrisList.add("test");

    // then
    assertNotEquals(serverUrisList, config.getServerList());

}
 
Example #10
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void clientIsCalledWhenListenerIsNotified() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<JestClient, Bulk> config = spy(builder.build());

    JestClient mockedJestClient = mock(JestClient.class);
    when(config.createClient()).thenReturn(mockedJestClient);

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
    Function<Bulk, Boolean> listener = config.createBatchListener(failoverPolicy);

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");
    Bulk bulk = createTestBatch(payload1, payload2);

    // when
    listener.apply(bulk);

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(mockedJestClient, times(1)).executeAsync((Bulk) captor.capture(), Mockito.any());

    assertEquals(bulk, captor.getValue());
}
 
Example #11
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void lifecycleStartStartItemSourceFactoryOnlyOnce() {

    // given
    PooledItemSourceFactory itemSourceFactory = mock(PooledItemSourceFactory.class);
    when(itemSourceFactory.isStarted()).thenAnswer(trueOnlyOnce());

    BufferedJestHttpObjectFactory objectFactory = spy(createTestObjectFactoryBuilder()
            .withItemSourceFactory(itemSourceFactory).build());

    JestClient client = mock(JestClient.class);
    when(objectFactory.createClient()).thenReturn(client);

    // when
    objectFactory.start();
    objectFactory.start();

    // then
    verify(itemSourceFactory).start();

}
 
Example #12
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void lifecycleStopStopsItemSourceFactoryOnlyOnce() {

    // given
    PooledItemSourceFactory itemSourceFactory = mock(PooledItemSourceFactory.class);
    when(itemSourceFactory.isStopped()).thenAnswer(falseOnlyOnce());

    BufferedJestHttpObjectFactory objectFactory = spy(createTestObjectFactoryBuilder()
            .withItemSourceFactory(itemSourceFactory).build());

    JestClient client = mock(JestClient.class);
    ClientProvider<JestClient> clientProvider = () -> client;
    when(objectFactory.getClientProvider(any())).thenReturn(clientProvider);

    objectFactory.start();

    // when
    objectFactory.stop();
    objectFactory.stop();

    // then
    verify(itemSourceFactory).stop();

}
 
Example #13
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void lifecycleStartDoesntStartClient() {

    // given
    JestHttpObjectFactory objectFactory = spy(createTestObjectFactoryBuilder().build());
    when(objectFactory.isStarted()).thenAnswer(falseOnlyOnce());

    JestClient client = mock(JestClient.class);
    when(objectFactory.createClient()).thenReturn(client);

    // when
    objectFactory.start();
    objectFactory.start();

    // then
    assertEquals(0, mockingDetails(client).getInvocations().size());

}
 
Example #14
Source File: JestClientHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearchExecute() throws Exception
{
    // Mock
    Search search = mock(Search.class);
    SearchResult searchResult = mock(SearchResult.class);
    JestClient jestClient = mock(JestClient.class);
    when(jestClientFactory.getJestClient()).thenReturn(jestClient);
    when(jestClient.execute(search)).thenReturn(searchResult);

    // Test
    SearchResult result = jestClientHelper.execute(search);

    // Validate
    assertThat(result, is(not(nullValue())));

    // Verify
    verify(jestClientFactory).getJestClient();
    verify(jestClient).execute(search);
    verifyNoMoreInteractions(createdMocks.toArray());
}
 
Example #15
Source File: JestConfig.java    From light-reading-cloud with MIT License 6 votes vote down vote up
@Bean
public JestClient jestClient() {
    JestClientFactory factory = new JestClientFactory();
    String[] sers = es_servers.split(",");
    HttpClientConfig httpClientConfig =
            new HttpClientConfig.Builder(Arrays.asList(sers))
                    .multiThreaded(true)
                    .connTimeout(5000)
                    .readTimeout(3000)
                    .maxTotalConnection(10)
                    .defaultMaxTotalConnectionPerRoute(10)
                    .build();
    factory.setHttpClientConfig(httpClientConfig);
    LOGGER.info("es_servers:{}", es_servers);
    return factory.getObject();
}
 
Example #16
Source File: JestClientFactoryTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJestClientHttp()
{
    // Mock the external calls.
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_AWS_REGION_NAME)).thenReturn(AWS_REGION_NAME);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_HOSTNAME)).thenReturn(ELASTICSEARCH_HOSTNAME);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_PORT, Integer.class)).thenReturn(ELASTICSEARCH_PORT);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_SCHEME)).thenReturn("http");
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_READ_TIMEOUT, Integer.class)).thenReturn(READ_TIMEOUT);
    when(configurationHelper.getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_CONNECTION_TIMEOUT, Integer.class)).thenReturn(CONNECTION_TIMEOUT);

    // Call the method under test.
    JestClient jestClient = jestClientFactory.getJestClient();

    // Verify the external calls.
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_AWS_REGION_NAME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_HOSTNAME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_PORT, Integer.class);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_DOMAIN_REST_CLIENT_SCHEME);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_READ_TIMEOUT, Integer.class);
    verify(configurationHelper).getProperty(ConfigurationValue.ELASTICSEARCH_REST_CLIENT_CONNECTION_TIMEOUT, Integer.class);
    verifyNoMoreInteractionsHelper();

    // Validate the results.
    assertNotNull(jestClient);
}
 
Example #17
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void httpParamsArePassedToCreatedObject() throws IllegalArgumentException, IllegalAccessException {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    builder.withConnTimeout(TEST_CONNECTION_TIMEOUT);
    builder.withReadTimeout(TEST_READ_TIMEOUT);
    builder.withMaxTotalConnection(TEST_MAX_TOTAL_CONNECTIONS);
    builder.withDefaultMaxTotalConnectionPerRoute(TEST_DEFAULT_MAX_TOTAL_CONNECTIONS_PER_ROUTE);
    builder.withDiscoveryEnabled(TEST_DISCOVERY_ENABLED);
    builder.withIoThreadCount(TEST_IO_THREAD_COUNT);

    // when
    ClientObjectFactory<JestClient, Bulk> httpObjectFactory = builder.build();

    // then
    assertEquals(TEST_CONNECTION_TIMEOUT,
            org.powermock.api.mockito.PowerMockito.field(httpObjectFactory.getClass(), "connTimeout").get(httpObjectFactory));
    assertEquals(TEST_READ_TIMEOUT,
            PowerMockito.field(httpObjectFactory.getClass(), "readTimeout").get(httpObjectFactory));
    assertEquals(TEST_MAX_TOTAL_CONNECTIONS,
            PowerMockito.field(httpObjectFactory.getClass(), "maxTotalConnections").get(httpObjectFactory));
    assertEquals(TEST_DEFAULT_MAX_TOTAL_CONNECTIONS_PER_ROUTE,
            PowerMockito.field(httpObjectFactory.getClass(), "defaultMaxTotalConnectionsPerRoute").get(httpObjectFactory));
    assertEquals(TEST_DISCOVERY_ENABLED,
            PowerMockito.field(httpObjectFactory.getClass(), "discoveryEnabled").get(httpObjectFactory));
    assertEquals(TEST_IO_THREAD_COUNT,
            PowerMockito.field(httpObjectFactory.getClass(), "ioThreadCount").get(httpObjectFactory));

}
 
Example #18
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void batchListenerExecutesOperationsIfOperationsAvailable() throws Exception {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<JestClient, Bulk> config = spy(builder.build());

    JestClient mockedJestClient = mock(JestClient.class);
    when(config.createClient()).thenReturn(mockedJestClient);

    FailoverPolicy failoverPolicy = spy(new NoopFailoverPolicy());
    Function<Bulk, Boolean> listener = config.createBatchListener(failoverPolicy);

    Operation operation = spy(new Operation() {
        @Override
        public void execute() {
        }
    });

    config.addOperation(operation);

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = createTestBatch(payload1, payload2);

    // when
    listener.apply(bulk);

    // then
    verify(operation).execute();

}
 
Example #19
Source File: BufferedJestClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final JestClient getObject() {

    // no other way than copying almost whole super.getObject() ..
    BufferedJestHttpClient client = createDefaultClient();

    HttpClientConfig httpClientConfig = wrappedHttpClientConfig.getHttpClientConfig();
    client.setServers(httpClientConfig.getServerList());

    final HttpClientConnectionManager connectionManager = getConnectionManager();
    client.setHttpClient(createHttpClient(connectionManager));

    final NHttpClientConnectionManager asyncConnectionManager = getAsyncConnectionManager();
    client.setAsyncClient(createAsyncHttpClient(asyncConnectionManager));

    // schedule idle connection reaping if configured
    if (httpClientConfig.getMaxConnectionIdleTime() > 0) {
        createConnectionReaper(client, connectionManager, asyncConnectionManager);
    } else {
        LOG.info("Idle connection reaping disabled");
    }

    // set discovery (should be set after setting the httpClient on jestClient)
    if (httpClientConfig.isDiscoveryEnabled()) {
        createNodeChecker(client, httpClientConfig);
    } else {
        LOG.info("Node Discovery disabled");
    }

    client.getAsyncClient().start();
    return client;

}
 
Example #20
Source File: BufferedJestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
ClientProvider<JestClient> getClientProvider(WrappedHttpClientConfig.Builder clientConfigBuilder) {
    return new JestClientProvider(clientConfigBuilder) {
        @Override
        public JestClient createClient() {
            WrappedHttpClientConfig wrappedHttpClientConfig = clientConfigBuilder.build();
            JestClientFactory jestClientFactory = new BufferedJestClientFactory(wrappedHttpClientConfig);
            return jestClientFactory.getObject();
        }
    };
}
 
Example #21
Source File: ElasticsearchConfiguration.java    From 21-points with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public ElasticsearchOperations elasticsearchTemplate(final JestClient jestClient,
                                                     final ElasticsearchConverter elasticsearchConverter,
                                                     final SimpleElasticsearchMappingContext simpleElasticsearchMappingContext,
                                                     EntityMapper mapper) {
    return new JestElasticsearchTemplate(
        jestClient,
        elasticsearchConverter,
        new DefaultJestResultsMapper(simpleElasticsearchMappingContext, mapper));
}
 
Example #22
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void clientIsInitializedOnlyOnce() {

    // given
    BufferedJestHttpObjectFactory factory = spy(createTestObjectFactoryBuilder().build());

    // when
    JestClient client1 = factory.createClient();
    JestClient client2 = factory.createClient();

    // then
    assertEquals(client1, client2);

}
 
Example #23
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public void bulkUpdate(List<JsonObject> updates, String docType){

        final Bulk.Builder bulkProcessor = new Bulk.Builder();
        //prepare update actions
        updates.forEach( u -> {
            final String id = u.remove("_id").getAsString();
            final String index = u.remove("_index").getAsString();
            final JsonObject updateDoc = new JsonObject();
            updateDoc.add("doc",u.getAsJsonObject());
            final Update update = new Update
                    .Builder(updateDoc.toString())
                    .index(index)
                    .id(id)
                    .type(docType).build();

            bulkProcessor.addAction(update);
        });


        final JestClient client = getElasticSearchClient();
        try {
            bulkUpdate(bulkProcessor, 0, client);
        } catch (InterruptedException e) {
            log.error("Error executing bulk update: {}", e.getMessage(),e);
            throw new RuntimeException("Error executing bulk update: " + e.getMessage(), e);
        }
    }
 
Example #24
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
/**
 * Cache a result
 *
 * @param content
 */
private void cacheResult(final String content) {
    final JestClient client = getElasticSearchClient();
    if (client != null) {
        final Index contentIndex = new Index.Builder(content).index(elasticIndex).type("logEntry").build();
        try {
            final DocumentResult result = client.execute(contentIndex);
            log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded());
        } catch (IOException e) {
            log.error("Error indexing content {}: {}", content, e.getMessage(), e);
        }
        //TODO: move to async at some point
        /*client.executeAsync(contentIndex, new JestResultHandler<JestResult>() {
            @Override
            public void completed(JestResult result) {
                log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded());
            }

            @Override
            public void failed(Exception e) {
                log.error("Error indexing content {}: {}", content, e.getMessage(), e);
            }
        });*/

    } else {
        log.warn("Content {} won't be cached, there is not target bucket", content);
    }
}
 
Example #25
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
private synchronized SearchResult  getScrollQuery(String query, int retry, JestClient client) throws InterruptedException {

        if (client != null) {
            final Search.Builder searchBuilder = new Search.Builder(query)
                    .addIndex(elasticIndex)
                    .setParameter(Parameters.SCROLL, SCROLL_TIME_SESSION);

            final Search search = searchBuilder.build();

            try {
                final SearchResult result = client.execute(search);
                log.debug("Completed query scroll query in {} tries. Succeeded: {}", retry + 1, result.isSucceeded());
                return result;
            } catch (IOException e) {
                log.warn("Try {} - Error in query scroll request query: {}", retry, e.getMessage(), e);
                if(retry > ES_MAX_TRIES) {
                    log.error("Error in query scroll request: reached maximum number of scroll tries [{}].", retry);
                    throw new RuntimeException("Error in query scroll request query: " + e.getMessage(), e);
                } else {
                    Thread.sleep((retry + 1) * ES_WAIT_TIME);
                    return getScrollQuery(query, retry + 1, client);
                }
            }
            //TODO: move to async at some point
            /*client.executeAsync(search,new JestResultHandler<JestResult>() {
                @Override
                public void completed(JestResult result) {
                    log.debug("Completed total requests query. Succeeded: {}", result.isSucceeded());
                }

                @Override
                public void failed(Exception e) {
                    log.error("Error indexing content : {}", e.getMessage(), e);
                }
            });*/
        }
        log.error("Error in scroll request query: ES client has not been initialized, client is null.");
        throw new RuntimeException("Error in scroll request query: ES client has not been initialized, client is null.");
    }
 
Example #26
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public synchronized SearchResult  getScrollQuery(String query) {
    final JestClient client = getElasticSearchClient();
    try {
        return getScrollQuery(query, 0 , client);
    } catch (InterruptedException e) {
        log.error("Error in query scroll request: {}", e.getMessage(), e );
        throw new RuntimeException("Error in query scroll request query: " + e.getMessage(), e);
    }
}
 
Example #27
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void paramsArePassedToCreatedObject() throws IllegalArgumentException, IllegalAccessException {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();

    builder.withMixIns(TEST_MIXINS)
            .withConnTimeout(TEST_CONNECTION_TIMEOUT)
            .withReadTimeout(TEST_READ_TIMEOUT)
            .withMaxTotalConnection(TEST_MAX_TOTAL_CONNECTIONS)
            .withDefaultMaxTotalConnectionPerRoute(TEST_DEFAULT_MAX_TOTAL_CONNECTIONS_PER_ROUTE)
            .withDiscoveryEnabled(TEST_DISCOVERY_ENABLED)
            .withIoThreadCount(TEST_IO_THREAD_COUNT)
            .withMappingType(TEST_MAPPING_TYPE);

    // when
    ClientObjectFactory<JestClient, Bulk> config = builder.build();

    // then
    assertEquals(TEST_CONNECTION_TIMEOUT,
            org.powermock.api.mockito.PowerMockito.field(config.getClass(), "connTimeout").get(config));
    assertEquals(TEST_READ_TIMEOUT,
            PowerMockito.field(config.getClass(), "readTimeout").get(config));
    assertEquals(TEST_MAX_TOTAL_CONNECTIONS,
            PowerMockito.field(config.getClass(), "maxTotalConnections").get(config));
    assertEquals(TEST_DEFAULT_MAX_TOTAL_CONNECTIONS_PER_ROUTE,
            PowerMockito.field(config.getClass(), "defaultMaxTotalConnectionsPerRoute").get(config));
    assertEquals(TEST_DISCOVERY_ENABLED,
            PowerMockito.field(config.getClass(), "discoveryEnabled").get(config));
    assertEquals(TEST_IO_THREAD_COUNT,
            PowerMockito.field(config.getClass(), "ioThreadCount").get(config));
    assertEquals(TEST_MAPPING_TYPE,
            PowerMockito.field(config.getClass(), "mappingType").get(config));
    assertEquals(TEST_MIXINS,
            PowerMockito.field(config.getClass(), "mixIns").get(config));

}
 
Example #28
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public synchronized JestResult scrollResults(String scrollId) {
    final JestClient client = getElasticSearchClient();
    try {
        return scrollResults(scrollId, 0 , client);
    } catch (InterruptedException e) {
        log.error("Error in scroll request: {}", e.getMessage(), e );
        throw new RuntimeException("Error in scroll request query: " + e.getMessage(), e);
    }
}
 
Example #29
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void failureHandlerExecutesFailoverForEachBatchItemSeparately() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    ClientObjectFactory<JestClient, Bulk> config = builder.build();

    FailoverPolicy failoverPolicy = Mockito.spy(new NoopFailoverPolicy());

    String payload1 = "test1";
    String payload2 = "test2";
    ItemSource<ByteBuf> source1 = createDefaultTestBuffereItemSource(payload1);
    ItemSource<ByteBuf> source2 = createDefaultTestBuffereItemSource(payload2);
    Bulk bulk = createTestBatch(source1, source2);

    // when
    config.createFailureHandler(failoverPolicy).apply(bulk);

    // then
    ArgumentCaptor<FailedItemSource> captor = ArgumentCaptor.forClass(FailedItemSource.class);
    verify(failoverPolicy, times(2)).deliver(captor.capture());

    FailedItemSource<ByteBuf> item1 = captor.getAllValues().get(0);
    assertTrue(item1.getSource().toString(Charset.defaultCharset()).equals(payload1));

    FailedItemSource<ByteBuf> item2 = captor.getAllValues().get(1);
    assertTrue(item2.getSource().toString(Charset.defaultCharset()).equals(payload2));
}
 
Example #30
Source File: JkesDocumentDeleter.java    From jkes with Apache License 2.0 5 votes vote down vote up
JkesDocumentDeleter(
        JestClient client,
        boolean ignoreSchema,
        Set<String> ignoreSchemaTopics,
        String versionType,
        long flushTimeoutMs,
        int maxBufferedRecords,
        int maxInFlightRequests,
        int batchSize,
        long lingerMs,
        int maxRetries,
        long retryBackoffMs
) {
  this.client = client;
  this.ignoreSchema = ignoreSchema;
  this.ignoreSchemaTopics = ignoreSchemaTopics;
  this.versionType = versionType;
  this.flushTimeoutMs = flushTimeoutMs;

  bulkProcessor = new BulkProcessor<>(
          new SystemTime(),
          new BulkDeletingClient(client),
          maxBufferedRecords,
          maxInFlightRequests,
          batchSize,
          lingerMs,
          maxRetries,
          retryBackoffMs
  );

  existingMappings = new HashSet<>();
}