Suman
Suman

Reputation: 9581

How do I find the alias for an index in ElasticSearch

I would like to find out the alias for a given index in ElasticSearch. Basically, the reverse of this question: How to find Index by Alias in Elasticsearch java api?

Upvotes: 0

Views: 1066

Answers (1)

Suman
Suman

Reputation: 9581

This is how I did it.

// Get the map of [alias => [index => metadata, ...], ...]
ImmutableOpenMap<String, ImmutableOpenMap<String, AliasMetaData>> aliasesAndIndices = 
        client.admin().cluster()
        .prepareState().execute().actionGet().getState()
        .getMetaData().getAliases();
Map<String, String> aliasForIndex = new HashMap<>();

// Convert it to a map of [index => alias, ...]
for (String alias : aliasesAndIndices.keys().toArray(String.class)) {
    ImmutableOpenMap<String, AliasMetaData> innerMap = aliasesAndIndices.get(alias);
    for (String index : innerMap.keys().toArray(String.class)) {
        aliasForIndex.put(index, alias);
    }
}

And then I could get the alias for an index like this.

aliasForIndex.get(index)

Upvotes: 2

Related Questions