use of org.elasticsearch.client.GetAliasesResponse in project registry by gbif.
the class EsClient method swapAlias.
/**
* Points the indexName to the alias, and deletes all the indices that were pointing to the alias.
*/
public void swapAlias(String alias, String indexName) {
try {
GetAliasesRequest getAliasesRequest = new GetAliasesRequest();
getAliasesRequest.aliases(alias);
GetAliasesResponse getAliasesResponse = restHighLevelClient.indices().getAlias(getAliasesRequest, RequestOptions.DEFAULT);
Set<String> idxsToDelete = getAliasesResponse.getAliases().keySet();
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
indicesAliasesRequest.addAliasAction(IndicesAliasesRequest.AliasActions.add().alias(alias).index(indexName));
restHighLevelClient.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
if (!idxsToDelete.isEmpty()) {
idxsToDelete.forEach(idx -> indicesAliasesRequest.addAliasAction(IndicesAliasesRequest.AliasActions.remove().index(idx).alias(alias)));
restHighLevelClient.indices().delete(new DeleteIndexRequest().indices(idxsToDelete.toArray(new String[0])), RequestOptions.DEFAULT);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of org.elasticsearch.client.GetAliasesResponse in project herd by FINRAOS.
the class IndexFunctionsDaoTest method testDeleteIndexDocumentsFunctionWithFailures.
@Test
public void testDeleteIndexDocumentsFunctionWithFailures() throws Exception {
// Build mocks
GetAliasesResponse getAliasesResponse = mock(GetAliasesResponse.class);
IndicesClient indicesClient = mock(IndicesClient.class);
RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);
AliasMetadata AliasMetadata = mock(AliasMetadata.class);
BulkResponse bulkResponse = mock(BulkResponse.class);
// Create objects needed for the test.
Set<AliasMetadata> AliasMetadataSet = new HashSet<>();
AliasMetadataSet.add(AliasMetadata);
Map<String, Set<AliasMetadata>> aliases = new HashMap<>();
aliases.put(SEARCH_INDEX_ALIAS_BDEF, AliasMetadataSet);
List<Long> businessObjectDefinitionIds = new ArrayList<>();
businessObjectDefinitionIds.add(SEARCH_INDEX_BUSINESS_OBJECT_DEFINITION_DOCUMENT_ID);
// Mock the calls to external methods
when(elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()).thenReturn(restHighLevelClient);
when(restHighLevelClient.indices()).thenReturn(indicesClient);
when(indicesClient.getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getAliasesResponse);
when(getAliasesResponse.getAliases()).thenReturn(aliases);
when(restHighLevelClient.bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(bulkResponse);
when(bulkResponse.hasFailures()).thenReturn(true);
when(bulkResponse.buildFailureMessage()).thenReturn(ERROR_MESSAGE);
// Call the method under test
indexFunctionsDao.deleteIndexDocuments(SEARCH_INDEX_NAME, businessObjectDefinitionIds);
// Verify the calls to external methods
verify(elasticsearchRestHighLevelClientFactory, times(2)).getRestHighLevelClient();
verify(restHighLevelClient).indices();
verify(restHighLevelClient).bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT));
verify(indicesClient).getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT));
verify(getAliasesResponse).getAliases();
verify(bulkResponse).hasFailures();
verify(bulkResponse).buildFailureMessage();
verify(restHighLevelClient, times(2)).close();
verifyNoMoreInteractions(bulkResponse, elasticsearchRestHighLevelClientFactory, getAliasesResponse, indicesClient, restHighLevelClient);
}
use of org.elasticsearch.client.GetAliasesResponse in project herd by FINRAOS.
the class IndexFunctionsDaoTest method testUpdateIndexDocumentsFunctionThrowsElasticsearchRestClientException.
@Test(expected = ElasticsearchRestClientException.class)
public void testUpdateIndexDocumentsFunctionThrowsElasticsearchRestClientException() throws Exception {
// Build mocks
GetAliasesResponse getAliasesResponse = mock(GetAliasesResponse.class);
IndicesClient indicesClient = mock(IndicesClient.class);
RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);
AliasMetadata AliasMetadata = mock(AliasMetadata.class);
// Create objects needed for the test.
Map<String, String> documentMap = new HashMap<>();
documentMap.put(SEARCH_INDEX_DOCUMENT, SEARCH_INDEX_DOCUMENT_JSON);
Set<AliasMetadata> AliasMetadataSet = new HashSet<>();
AliasMetadataSet.add(AliasMetadata);
Map<String, Set<AliasMetadata>> aliases = new HashMap<>();
aliases.put(SEARCH_INDEX_ALIAS_BDEF, AliasMetadataSet);
// Mock the calls to external methods
when(elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()).thenReturn(restHighLevelClient);
when(restHighLevelClient.indices()).thenReturn(indicesClient);
when(indicesClient.getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getAliasesResponse);
when(getAliasesResponse.getAliases()).thenReturn(aliases);
when(restHighLevelClient.bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT))).thenThrow(new IOException());
// Call the method under test
indexFunctionsDao.updateIndexDocuments(SEARCH_INDEX_NAME, documentMap);
}
use of org.elasticsearch.client.GetAliasesResponse in project herd by FINRAOS.
the class IndexFunctionsDaoTest method testCreateIndexDocumentsFunctionWithFailures.
@Test
public void testCreateIndexDocumentsFunctionWithFailures() throws Exception {
// Build mocks
GetAliasesResponse getAliasesResponse = mock(GetAliasesResponse.class);
IndicesClient indicesClient = mock(IndicesClient.class);
RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);
AliasMetadata AliasMetadata = mock(AliasMetadata.class);
BulkResponse bulkResponse = mock(BulkResponse.class);
// Create objects needed for the test.
Map<String, String> documentMap = new HashMap<>();
documentMap.put(SEARCH_INDEX_DOCUMENT, SEARCH_INDEX_DOCUMENT_JSON);
Set<AliasMetadata> AliasMetadataSet = new HashSet<>();
AliasMetadataSet.add(AliasMetadata);
Map<String, Set<AliasMetadata>> aliases = new HashMap<>();
aliases.put(SEARCH_INDEX_ALIAS_BDEF, AliasMetadataSet);
// Mock the calls to external methods
when(elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()).thenReturn(restHighLevelClient);
when(restHighLevelClient.indices()).thenReturn(indicesClient);
when(indicesClient.getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getAliasesResponse);
when(getAliasesResponse.getAliases()).thenReturn(aliases);
when(restHighLevelClient.bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(bulkResponse);
when(bulkResponse.hasFailures()).thenReturn(true);
when(bulkResponse.buildFailureMessage()).thenReturn(ERROR_MESSAGE);
// Call the method under test
indexFunctionsDao.createIndexDocuments(SEARCH_INDEX_NAME, documentMap);
// Verify the calls to external methods
verify(elasticsearchRestHighLevelClientFactory, times(2)).getRestHighLevelClient();
verify(restHighLevelClient).indices();
verify(restHighLevelClient).bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT));
verify(indicesClient).getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT));
verify(getAliasesResponse).getAliases();
verify(bulkResponse).hasFailures();
verify(bulkResponse).buildFailureMessage();
verify(restHighLevelClient, times(2)).close();
verifyNoMoreInteractions(bulkResponse, elasticsearchRestHighLevelClientFactory, getAliasesResponse, indicesClient, restHighLevelClient);
}
use of org.elasticsearch.client.GetAliasesResponse in project herd by FINRAOS.
the class IndexFunctionsDaoTest method testUpdateIndexDocumentsFunction.
@Test
public void testUpdateIndexDocumentsFunction() throws Exception {
// Build mocks
GetAliasesResponse getAliasesResponse = mock(GetAliasesResponse.class);
IndicesClient indicesClient = mock(IndicesClient.class);
RestHighLevelClient restHighLevelClient = mock(RestHighLevelClient.class);
AliasMetadata AliasMetadata = mock(AliasMetadata.class);
BulkResponse bulkResponse = mock(BulkResponse.class);
// Create objects needed for the test.
Map<String, String> documentMap = new HashMap<>();
documentMap.put(SEARCH_INDEX_DOCUMENT, SEARCH_INDEX_DOCUMENT_JSON);
Set<AliasMetadata> AliasMetadataSet = new HashSet<>();
AliasMetadataSet.add(AliasMetadata);
Map<String, Set<AliasMetadata>> aliases = new HashMap<>();
aliases.put(SEARCH_INDEX_ALIAS_BDEF, AliasMetadataSet);
// Mock the calls to external methods
when(elasticsearchRestHighLevelClientFactory.getRestHighLevelClient()).thenReturn(restHighLevelClient);
when(restHighLevelClient.indices()).thenReturn(indicesClient);
when(indicesClient.getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getAliasesResponse);
when(getAliasesResponse.getAliases()).thenReturn(aliases);
when(restHighLevelClient.bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(bulkResponse);
when(bulkResponse.hasFailures()).thenReturn(false);
// Call the method under test
indexFunctionsDao.updateIndexDocuments(SEARCH_INDEX_NAME, documentMap);
// Verify the calls to external methods
verify(elasticsearchRestHighLevelClientFactory, times(2)).getRestHighLevelClient();
verify(restHighLevelClient).indices();
verify(restHighLevelClient).bulk(any(BulkRequest.class), eq(RequestOptions.DEFAULT));
verify(indicesClient).getAlias(any(GetAliasesRequest.class), eq(RequestOptions.DEFAULT));
verify(getAliasesResponse).getAliases();
verify(bulkResponse).hasFailures();
verify(restHighLevelClient, times(2)).close();
verifyNoMoreInteractions(bulkResponse, elasticsearchRestHighLevelClientFactory, getAliasesResponse, indicesClient, restHighLevelClient);
}
Aggregations