private List<String> getAliases(String aliasName) { GetAliases getAliases = new GetAliases.Builder().build(); JestResult jestResult = jestClientHelper.execute(getAliases); Assert.isTrue(jestResult.isSucceeded(), jestResult.getErrorMessage()); List<String> indexNameList = jestResult.getJsonObject().entrySet().stream().filter(e -> e.getKey().startsWith(aliasName)).map(Map.Entry::getKey).collect(Collectors.toList()); return indexNameList; } }
private List<String> getAliases(String aliasName) { GetAliases getAliases = new GetAliases.Builder().build(); JestResult jestResult = jestClientHelper.execute(getAliases); Assert.isTrue(jestResult.isSucceeded(), jestResult.getErrorMessage()); List<String> indexNameList = jestResult.getJsonObject().entrySet().stream().filter(e -> e.getKey().startsWith(aliasName)).map(Map.Entry::getKey).collect(Collectors.toList()); return indexNameList; } }
/** * The delete index function will take as an argument the index name and will delete the index. */ @Override public final void deleteIndex(String indexName) { Action action = new DeleteIndex.Builder(indexName).build(); LOGGER.info("Deleting Elasticsearch index, indexName={}.", indexName); JestResult result = jestClientHelper.execute(action); LOGGER.info("Deleting Elasticsearch index, indexName={}. result successful is {} ", indexName, result.isSucceeded()); }
/** * The delete index function will take as an argument the index name and will delete the index. */ @Override public final void deleteIndex(String indexName) { Action action = new DeleteIndex.Builder(indexName).build(); LOGGER.info("Deleting Elasticsearch index, indexName={}.", indexName); JestResult result = jestClientHelper.execute(action); LOGGER.info("Deleting Elasticsearch index, indexName={}. result successful is {} ", indexName, result.isSucceeded()); }
@Override public DocsStats getIndexStats(String indexName) { Action getStats = new Stats.Builder().addIndex(indexName).build(); JestResult jestResult = jestClientHelper.execute(getStats); Assert.isTrue(jestResult.isSucceeded(), jestResult.getErrorMessage()); JsonObject statsJson = jestResult.getJsonObject().getAsJsonObject("indices").getAsJsonObject(indexName).getAsJsonObject("primaries"); JsonObject docsJson = statsJson.getAsJsonObject("docs"); return new DocsStats(docsJson.get("count").getAsLong(), docsJson.get("deleted").getAsLong()); }
@Override public DocsStats getIndexStats(String indexName) { Action getStats = new Stats.Builder().addIndex(indexName).build(); JestResult jestResult = jestClientHelper.execute(getStats); Assert.isTrue(jestResult.isSucceeded(), jestResult.getErrorMessage()); JsonObject statsJson = jestResult.getJsonObject().getAsJsonObject("indices").getAsJsonObject(indexName).getAsJsonObject("primaries"); JsonObject docsJson = statsJson.getAsJsonObject("docs"); return new DocsStats(docsJson.get("count").getAsLong(), docsJson.get("deleted").getAsLong()); }
@Override public boolean isValidDocumentIndex(String indexName, String documentType, String id, String json) { Get get = new Get.Builder(indexName, id).type(documentType).build(); JestResult jestResult = jestClientHelper.execute(get); // Retrieve the JSON string from the get response final String jsonStringFromIndex = jestResult.getSourceAsString(); // Return true if the json from the index is not null or empty and the json from the index matches the object from the database return StringUtils.isNotEmpty(jsonStringFromIndex) && jsonStringFromIndex.equals(json); }
@Override public boolean isValidDocumentIndex(String indexName, String documentType, String id, String json) { Get get = new Get.Builder(indexName, id).type(documentType).build(); JestResult jestResult = jestClientHelper.execute(get); // Retrieve the JSON string from the get response final String jsonStringFromIndex = jestResult.getSourceAsString(); // Return true if the json from the index is not null or empty and the json from the index matches the object from the database return StringUtils.isNotEmpty(jsonStringFromIndex) && jsonStringFromIndex.equals(json); }
@Override public final boolean isIndexExists(String indexName) { Action action = new IndicesExists.Builder(indexName).build(); JestResult result = jestClientHelper.execute(action); return result.isSucceeded(); }
/** * The delete document by id function will delete a document in the index by the document id. */ @Override public final void deleteDocumentById(String indexName, String documentType, String id) { LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={}.", indexName, documentType, id); Action action = new Delete.Builder(id).index(indexName).type(documentType).build(); JestResult result = jestClientHelper.execute(action); LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={} is successfully {}. ", indexName, documentType, id, result.isSucceeded()); }
/** * The delete document by id function will delete a document in the index by the document id. */ @Override public final void deleteDocumentById(String indexName, String documentType, String id) { LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={}.", indexName, documentType, id); Action action = new Delete.Builder(id).index(indexName).type(documentType).build(); JestResult result = jestClientHelper.execute(action); LOGGER.info("Deleting Elasticsearch document from index, indexName={}, documentType={}, id={} is successfully {}. ", indexName, documentType, id, result.isSucceeded()); }
@Override public final boolean isIndexExists(String indexName) { Action action = new IndicesExists.Builder(indexName).build(); JestResult result = jestClientHelper.execute(action); return result.isSucceeded(); }
@Test public void testCreateIndexFunction() { JestResult jestResult = mock(JestResult.class); // Build mocks when(jestClientHelper.execute(any())).thenReturn(jestResult); //indexFunctionsDao.createIndex("Index", "Document_Type", "Mapping", "Settings"); // verifyNoMoreInteractions(jestClientHelper); }
@Test public void testIndexExistsFunction() { JestResult jestResult = mock(JestResult.class); // Build mocks when(jestClientHelper.execute(any())).thenReturn(jestResult); indexFunctionsDao.isIndexExists("Index"); verify(jestClientHelper).execute(any()); verify(jestResult).isSucceeded(); verifyNoMoreInteractions(jestClientHelper); }
@Test public void testDeleteIndexFunction() { JestResult jestResult = mock(JestResult.class); // Build mocks when(jestClientHelper.execute(any())).thenReturn(jestResult); indexFunctionsDao.deleteIndex("Index"); verify(jestClientHelper).execute(any()); verify(jestResult).isSucceeded(); verifyNoMoreInteractions(jestClientHelper); }
@Override public long getNumberOfTypesInIndex(String indexName, String documentType) { Count count = new Count.Builder().addIndex(indexName).addType(documentType).build(); JestResult jestResult = jestClientHelper.execute(count); return Long.parseLong(jestResult.getSourceAsString()); }
@Test public void testDeleteDocumentByIdFunction() { JestResult jestResult = mock(JestResult.class); when(jestClientHelper.execute(any())).thenReturn(jestResult); when(jestResult.isSucceeded()).thenReturn(true); indexFunctionsDao.deleteDocumentById("INDEX_NAME", "DOCUMENT_TYPE", "ID"); verify(jestClientHelper).execute(any()); verify(jestResult).isSucceeded(); verifyNoMoreInteractions(jestClientHelper); }
@Override public Settings getIndexSettings(String indexName) { GetSettings getSettings = new GetSettings.Builder().addIndex(indexName).build(); JestResult result = jestClientHelper.execute(getSettings); Assert.isTrue(result.isSucceeded(), result.getErrorMessage()); JsonObject json = result.getJsonObject().getAsJsonObject(indexName).getAsJsonObject("settings"); return Settings.builder().loadFromSource(json.toString()).build(); }
@Test public void testIndexFunction() { SearchResult jestResult = mock(SearchResult.class); // Build mocks when(jestClientHelper.execute(any())).thenReturn(jestResult); // Call the method under test indexFunctionsDao.createIndexDocument("INDEX_NAME", "DOCUMENT_TYPE", "ID", "JSON"); // Verify the calls to external methods verify(jestClientHelper).execute(any()); verify(jestResult).isSucceeded(); verifyNoMoreInteractions(jestClientHelper); }
@Test public void testValidateFunctionNoActionRequiredValidDocument() { SearchResult jestResult = mock(SearchResult.class); // Build mocks when(jestClientHelper.execute(any())).thenReturn(jestResult); when(jestResult.getSourceAsString()).thenReturn("JSON"); indexFunctionsDao.validateDocumentIndex("INDEX_NAME", "DOCUMENT_TYPE", "ID", "JSON"); // Verify the calls to external methods verify(jestClientHelper, times(1)).execute(any()); verify(jestResult).getSourceAsString(); verifyNoMoreInteractions(jestClientHelper); }