use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method setAlias.
@Override
public int setAlias(final String index, final String alias) throws CalFacadeException {
// IndicesAliasesResponse resp = null;
try {
/* Other is the alias name - index is the index we were just indexing into
*/
final IndicesAdminClient idx = getAdminIdx();
final GetAliasesRequestBuilder igarb = idx.prepareGetAliases(alias);
final ActionFuture<GetAliasesResponse> getAliasesAf = idx.getAliases(igarb.request());
final GetAliasesResponse garesp = getAliasesAf.actionGet();
final ImmutableOpenMap<String, List<AliasMetaData>> aliasesmeta = garesp.getAliases();
final IndicesAliasesRequestBuilder iarb = idx.prepareAliases();
final Iterator<String> it = aliasesmeta.keysIt();
while (it.hasNext()) {
final String indexName = it.next();
for (final AliasMetaData amd : aliasesmeta.get(indexName)) {
if (amd.getAlias().equals(alias)) {
iarb.removeAlias(indexName, alias);
}
}
}
iarb.addAlias(index, alias);
final ActionFuture<IndicesAliasesResponse> af = idx.aliases(iarb.request());
/*resp = */
af.actionGet();
return 0;
} catch (final ElasticsearchException ese) {
// Failed somehow
error(ese);
return -1;
} catch (final CalFacadeException cfe) {
throw cfe;
} catch (final Throwable t) {
throw new CalFacadeException(t);
}
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder in project fess by codelibs.
the class FessEsClient method updateAlias.
public boolean updateAlias(final String newIndex) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
String updateAlias = fessConfig.getIndexDocumentUpdateIndex();
String searchAlias = fessConfig.getIndexDocumentSearchIndex();
final GetIndexResponse response1 = client.admin().indices().prepareGetIndex().addIndices(updateAlias).execute().actionGet(fessConfig.getIndexIndicesTimeout());
final String[] updateIndices = response1.indices();
final GetIndexResponse response2 = client.admin().indices().prepareGetIndex().addIndices(searchAlias).execute().actionGet(fessConfig.getIndexIndicesTimeout());
final String[] searchIndices = response2.indices();
IndicesAliasesRequestBuilder builder = client.admin().indices().prepareAliases().addAlias(newIndex, updateAlias).addAlias(newIndex, searchAlias);
for (String index : updateIndices) {
builder.removeAlias(index, updateAlias);
}
for (String index : searchIndices) {
builder.removeAlias(index, searchAlias);
}
IndicesAliasesResponse response = builder.execute().actionGet(fessConfig.getIndexIndicesTimeout());
return response.isAcknowledged();
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder in project stash-codesearch-plugin by palantir.
the class SearchUpdaterImpl method initializeAliasedIndex.
/**
* Initializes a stash-codesearch index with an alias pointing to it. If overwrite is true,
* a new index will be created even if the alias is already assigned. Returns true iff a new
* index was created.
*/
private synchronized boolean initializeAliasedIndex(String alias, boolean overwrite) {
String prevIndex = getIndexFromAlias(alias);
if (!overwrite && prevIndex != null) {
return false;
}
String newIndex = random.nextLong() + "-" + System.nanoTime();
try {
es.getClient().admin().indices().prepareCreate(newIndex).addMapping("latestindexed", jsonBuilder().startObject().startObject("properties").startObject("project").field("type", "string").field("index", "not_analyzed").endObject().startObject("repository").field("type", "string").field("index", "not_analyzed").endObject().startObject("ref").field("type", "string").field("index", "not_analyzed").endObject().startObject("hash").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()).addMapping("commit", jsonBuilder().startObject().startObject("properties").startObject("project").field("type", "string").field("index", "not_analyzed").endObject().startObject("repository").field("type", "string").field("index", "not_analyzed").endObject().startObject("refs").field("type", "multi_field").startObject("fields").startObject("refs").field("type", "string").field("index_analyzer", "ref_analyzer").field("search_analyzer", "ref_analyzer").endObject().startObject("untouched").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject().startObject("authorname").field("type", "string").field("index_analyzer", "name_analyzer").field("search_analyzer", "name_analyzer").endObject().startObject("authoremail").field("type", "multi_field").startObject("fields").startObject("authoremail").field("type", "string").field("index_analyzer", "email_analyzer").field("search_analyzer", "email_analyzer").endObject().startObject("untouched").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject().startObject("body").field("type", "string").endObject().startObject("subject").field("type", "string").endObject().startObject("commitdate").field("type", "date").field("format", "date_time").endObject().startObject("hash").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject()).addMapping("file", jsonBuilder().startObject().startObject("properties").startObject("project").field("type", "string").field("index", "not_analyzed").endObject().startObject("repository").field("type", "string").field("index", "not_analyzed").endObject().startObject("refs").field("type", "multi_field").startObject("fields").startObject("refs").field("type", "string").field("index_analyzer", "ref_analyzer").field("search_analyzer", "ref_analyzer").endObject().startObject("untouched").field("type", "string").field("index", "not_analyzed").endObject().endObject().endObject().startObject("blob").field("type", "string").field("index", "not_analyzed").endObject().startObject("path").field("type", "string").field("index_analyzer", "path_analyzer").field("search_analyzer", "path_analyzer").endObject().startObject("extension").field("type", "string").field("index", "not_analyzed").endObject().startObject("contents").field("type", "string").field("index_analyzer", "code_analyzer").field("search_analyzer", "code_analyzer").endObject().startObject("charcount").field("type", "integer").startObject("fielddata").field("format", "doc_values").endObject().endObject().startObject("linecount").field("type", "integer").startObject("fielddata").field("format", "doc_values").endObject().endObject().endObject().endObject()).setSettings(jsonBuilder().startObject().startObject("analysis").startObject("analyzer").startObject("email_analyzer").field("type", "pattern").field("pattern", "[^A-Za-z0-9_]+").endObject().startObject("name_analyzer").field("type", "pattern").field("pattern", "[^A-Za-z0-9_]+").endObject().startObject("ref_analyzer").field("type", "pattern").field("pattern", "/").endObject().startObject("code_analyzer").field("type", "pattern").field("pattern", "[^A-Za-z0-9_]+").endObject().startObject("path_analyzer").field("type", "pattern").field("pattern", "[/\\\\.]").endObject().endObject().startObject("filter").endObject().endObject().endObject()).get();
} catch (Exception e) {
log.error("Caught exception while creating {} ({}), aborting", newIndex, alias, e);
return false;
}
// Perform alias switch
IndicesAliasesRequestBuilder aliasBuilder = es.getClient().admin().indices().prepareAliases();
if (prevIndex != null) {
aliasBuilder.removeAlias(prevIndex, alias);
}
aliasBuilder.addAlias(newIndex, alias).get();
return true;
}
use of org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder in project stash-codesearch-plugin by palantir.
the class SearchUpdaterImpl method redirectAndDeleteAliasedIndex.
/**
* Makes one alias point to another's index, deleting the old index afterwards. Returns true
* iff the operation was successful.
*/
private synchronized boolean redirectAndDeleteAliasedIndex(String fromAlias, String toAlias) {
// Find indices corresponding to aliases
String fromIndex = getIndexFromAlias(fromAlias);
String toIndex = getIndexFromAlias(toAlias);
if (toIndex == null) {
log.error("{} does not resolve to an index", toAlias);
return false;
}
if (toIndex.equals(fromIndex)) {
log.warn("{} and {} resolve to the same index", fromAlias, toAlias);
return false;
}
// Perform alias switch
IndicesAliasesRequestBuilder builder = es.getClient().admin().indices().prepareAliases();
if (fromIndex != null) {
builder.removeAlias(fromIndex, fromAlias);
}
builder.addAlias(toIndex, fromAlias).get();
// Delete old index
if (fromIndex != null) {
es.getClient().admin().indices().prepareDelete(fromIndex).get();
}
return true;
}
Aggregations