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);
}
}
Aggregations