Search in sources :

Example 11 with GetAliasesResponse

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);
    }
}
Also used : GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) IOException(java.io.IOException)

Example 12 with GetAliasesResponse

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);
}
Also used : AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Set(java.util.Set) HashSet(java.util.HashSet) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndicesClient(org.elasticsearch.client.IndicesClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with GetAliasesResponse

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);
}
Also used : AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Set(java.util.Set) HashSet(java.util.HashSet) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) HashMap(java.util.HashMap) IndicesClient(org.elasticsearch.client.IndicesClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with GetAliasesResponse

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);
}
Also used : AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Set(java.util.Set) HashSet(java.util.HashSet) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) HashMap(java.util.HashMap) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndicesClient(org.elasticsearch.client.IndicesClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with GetAliasesResponse

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);
}
Also used : AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Set(java.util.Set) HashSet(java.util.HashSet) GetAliasesResponse(org.elasticsearch.client.GetAliasesResponse) HashMap(java.util.HashMap) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndicesClient(org.elasticsearch.client.IndicesClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

GetAliasesResponse (org.elasticsearch.client.GetAliasesResponse)18 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)15 Set (java.util.Set)13 HashMap (java.util.HashMap)10 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)10 IndicesClient (org.elasticsearch.client.IndicesClient)10 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)10 HashSet (java.util.HashSet)9 AliasMetadata (org.elasticsearch.cluster.metadata.AliasMetadata)9 Test (org.junit.Test)9 IOException (java.io.IOException)7 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)5 ArrayList (java.util.ArrayList)4 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)4 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)3 ElasticException (io.hops.hopsworks.exceptions.ElasticException)2 IndicesAliasesRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)2 GetAliasesResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.GetAliasesResponse)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1