How do you get all aliases (and their respective indices) in ES on 2.X?

I need to be able to query all of the aliases in my ES database.

I can easily do this through the REST API using this:

curl 'localhost:9200/_cat/aliases?v'

This is the code that was used to pull all aliases in our Java implementation of 1.5.2:

ClusterStateRequestBuilder builder = client.admin().cluster().prepareState();
builder.request();

return builder.execute().actionGet().getState().getMetaData().getAliases();

getAliases() is gone now. I am struggling to find the replacement for it.

Try this

ClusterStateRequestBuilder builder = client.admin().cluster().prepareState();
Set<String> aliases = new HashSet<>();
for (IndexMetaData indexMetaData : builder.execute().actionGet().getState().metaData()) {
    for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
      aliases.add(cursor.value);
    }
}

Thanks Jörg! I actually need something a little deeper than that. I was hoping getting the alias names would have been more than enough for me to run with, but I am still coming up short.

Currently looking into how to get the associated indices with each alias... If anyone knows how to build on the code Jörg shared to do that, please share.

Update:

I am really struggling to get anywhere on this. Every obscure method I am finding on the objects has no documentation associated with it. I can't get any momentum.

You can try to use client.admin().indices().getAliases(), since you don't care about whether an index is healthy.

That method is asking me for arguments. I dug in a little and it looks like it is for finding specific aliases in the system. I need them all.

Isn't there a method that just looks at the db and says "Give me all the aliases you know about"?

curl 'localhost:9200/_cat/aliases?v

gives me everything I want, but I need the Java form of it.

IndexMetaData holds the index name in method getIndex(), so it's easy to construct an aliases map for all concrete indices.

ClusterStateRequestBuilder builder = client.admin().cluster().prepareState();
Map<String,Set<String>> aliasMap = new HashMap<>();
for (IndexMetaData indexMetaData : builder.execute().actionGet().getState().metaData()) {
    Set<String> aliases = new HashSet<>();
    for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
      aliases.add(cursor.value);
    }
    aliasMap.put(indexMetaData.getIndex(), aliases)
}

Thanks! This looks good! I think you got what I needed. I will implement it and report back if the unit tests start exploding and can't figure it out.