Search in sources :

Example 1 with IndicesAliasesRequestBuilder

use of org.opensearch.action.admin.indices.alias.IndicesAliasesRequestBuilder in project fess-suggest by codelibs.

the class Suggester method createNextIndex.

public void createNextIndex() {
    try {
        final List<String> prevIndices = new ArrayList<>();
        final IndicesExistsResponse response = client.admin().indices().prepareExists(getUpdateAlias(index)).execute().actionGet(suggestSettings.getIndicesTimeout());
        if (response.isExists()) {
            final GetAliasesResponse getAliasesResponse = client.admin().indices().prepareGetAliases(getUpdateAlias(index)).execute().actionGet(suggestSettings.getIndicesTimeout());
            getAliasesResponse.getAliases().keysIt().forEachRemaining(prevIndices::add);
        }
        final String mappingSource = getDefaultMappings();
        final String settingsSource = getDefaultIndexSettings();
        final String indexName = createIndexName(index);
        if (logger.isInfoEnabled()) {
            logger.info("Create next index: {}", indexName);
        }
        final CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(indexName).setSettings(settingsSource, XContentType.JSON).addMapping(SuggestConstants.DEFAULT_TYPE, mappingSource, XContentType.JSON).execute().actionGet(suggestSettings.getIndicesTimeout());
        if (!createIndexResponse.isAcknowledged()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not create next index: {}", indexName);
            }
            throw new SuggesterException("Could not create next index: " + indexName);
        }
        client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(suggestSettings.getClusterTimeout());
        final IndicesAliasesRequestBuilder aliasesRequestBuilder = client.admin().indices().prepareAliases().addAlias(indexName, getUpdateAlias(index));
        for (final String prevIndex : prevIndices) {
            aliasesRequestBuilder.removeAlias(prevIndex, getUpdateAlias(index));
        }
        aliasesRequestBuilder.execute().actionGet(suggestSettings.getIndicesTimeout());
    } catch (final Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to replace with new index.", e);
        }
        throw new SuggesterException("Failed to replace with new index.", e);
    }
}
Also used : GetAliasesResponse(org.opensearch.action.admin.indices.alias.get.GetAliasesResponse) ArrayList(java.util.ArrayList) IndicesExistsResponse(org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) IOException(java.io.IOException) SuggesterException(org.codelibs.fess.suggest.exception.SuggesterException) IndicesAliasesRequestBuilder(org.opensearch.action.admin.indices.alias.IndicesAliasesRequestBuilder)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SuggesterException (org.codelibs.fess.suggest.exception.SuggesterException)1 IndicesAliasesRequestBuilder (org.opensearch.action.admin.indices.alias.IndicesAliasesRequestBuilder)1 GetAliasesResponse (org.opensearch.action.admin.indices.alias.get.GetAliasesResponse)1 CreateIndexResponse (org.opensearch.action.admin.indices.create.CreateIndexResponse)1 IndicesExistsResponse (org.opensearch.action.admin.indices.exists.indices.IndicesExistsResponse)1