Skip to content

Instantly share code, notes, and snippets.

@suresk
Created April 10, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suresk/fe07f27c22463dbbff0454e40f1e4e53 to your computer and use it in GitHub Desktop.
Save suresk/fe07f27c22463dbbff0454e40f1e4e53 to your computer and use it in GitHub Desktop.
Elasticsearch determine new index
private int findCurrentIndex(String aliasName, int indexCount)
{
Map<String, AliasOrIndex> allAliases = elasticClient
.admin()
.cluster()
.state(Requests.clusterStateRequest())
.actionGet()
.getState()
.getMetaData()
.getAliasAndIndexLookup();
AliasOrIndex alias = allAliases.get(aliasName);
if(alias != null)
{
if(!alias.isAlias())
{
throw new IllegalStateException(String.format("Concrete index with alias name (%s) already exists. Aborting.", aliasName));
}
if(alias.getIndices().size() > 1)
{
throw new IllegalStateException("Alias has multiple records. Aborting.");
}
else
{
return getIndexId(alias.getIndices().get(0).getIndex());
}
}
return -1;
}
private String buildIndexName(String aliasName, int id)
{
return String.format("%s-%d", aliasName, id);
}
private int getIndexId(String indexName)
{
String[] parts = indexName.split("-");
return Integer.valueOf(parts[parts.length - 1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment