Created
April 10, 2016 05:59
-
-
Save suresk/fe07f27c22463dbbff0454e40f1e4e53 to your computer and use it in GitHub Desktop.
Elasticsearch determine new index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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