ElasticSearchDataCleaner implements Runnable { public BulkByScrollResponse deleteData(ElasticSearchClient elasticSearchClient) { log.info("deleting old data"); BulkByScrollResponse bulkByScrollResponse = null; DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(INDEX); RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder(FIELD).lt("now-" + RETENTION_PERIOD_DAYS + "d/d"); deleteByQueryRequest.setQuery(rangeQueryBuilder); try { bulkByScrollResponse = elasticSearchClient .deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT); log.info(bulkByScrollResponse.toString()); } catch (IOException e) { log.error(e.getLocalizedMessage(),e); } catch (Exception e) { log.error(e.getLocalizedMessage(),e); } finally { elasticSearchClient.close(); } return bulkByScrollResponse; } ElasticSearchDataCleaner(); @Override void run(); BulkByScrollResponse deleteData(ElasticSearchClient elasticSearchClient);  }
@Test public void testDelete() throws Exception { BulkByScrollResponse expected = buildBulkResponse(); when(client.deleteByQuery(any(DeleteByQueryRequest.class), any())) .thenReturn(expected); ElasticSearchDataCleaner elasticSearchDataCleaner = new ElasticSearchDataCleaner(); BulkByScrollResponse actual = elasticSearchDataCleaner.deleteData(client); assertEquals(actual,expected); }
@Test public void testDeleteIOException() throws Exception { when(client.deleteByQuery(any(DeleteByQueryRequest.class), any())) .thenThrow(IOException.class); ElasticSearchDataCleaner elasticSearchDataCleaner = new ElasticSearchDataCleaner(); BulkByScrollResponse actual = elasticSearchDataCleaner.deleteData(client); assertNull(actual); }
@Test public void testDeleteException() throws Exception { when(client.deleteByQuery(any(DeleteByQueryRequest.class), any())) .thenThrow(Exception.class); ElasticSearchDataCleaner elasticSearchDataCleaner = new ElasticSearchDataCleaner(); BulkByScrollResponse actual = elasticSearchDataCleaner.deleteData(client); assertNull(actual); }