Reputation: 1092
How to find aliases for given index in ElasticSearch using Java?
By using REST API it is pretty easy
https://www.elastic.co/guide/en/elasticsearch/reference/1.x/indices-aliases.html#alias-retrieving
But I was unable to find any good reference on how to do it via Java API
Upvotes: 2
Views: 1394
Reputation: 14611
The previous answer is OK for ElasticSearch 2.x. Things have changed slightly in version 5.x of ElasticSearch though.
This is what has worked for me for version 5.x:
public Collection<String> findAliasForIndices(String... indices) {
if(indices == null || indices.length == 0) {
return Collections.emptyList();
}
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.routingTable(false)
.nodes(false)
.indices(indices);
ImmutableOpenMap<String, IndexMetaData> aliasMap = client
.admin().cluster().state(clusterStateRequest)
.actionGet().getState().getMetaData()
.getIndices();
return StreamSupport.stream(aliasMap.spliterator(), false).flatMap((e) -> {
Iterable<String> iterable = () -> e.value.getAliases().keysIt();
return StreamSupport.stream(iterable.spliterator(), false);
}).collect(Collectors.toSet());
}
This method returns all the aliases as strings for a collection of indices.
Usage example with JUnit and AssertJ:
@Test
public void whenFindAliasForIndices_ShouldRetrieveIndices() throws IOException {
String testIndex = "blah1";
String alias1 = "alias1";
createIndexWithAlias(testIndex, alias1);
String testIndex2 = "blah2";
String alias2 = "alias2";
createIndexWithAlias(testIndex2, alias2);
Collection<String> indices = adapter.findAliasForIndices(testIndex, testIndex2);
assertThat(indices.contains(alias1)).isTrue();
assertThat(indices.contains(alias2)).isTrue();
adapter.deleteIndices(testIndex);
adapter.deleteIndices(testIndex2);
}
Upvotes: 0
Reputation: 1092
While working with ElasticSearch, I ran into an issue where I needed to get a list of aliases based on provided index.
While getting a list of aliases is pretty straightforward:
client.admin().cluster()
.prepareState().execute()
.actionGet().getState()
.getMetaData().aliases();
I struggled to find an easy way to be able to get aliases for given index without having to iterate through everything first.
My first implementation looked something like this:
ImmutableOpenMap<String, ImmutableOpenMap<String, AliasMetaData>> aliases = client.admin().cluster()
.prepareState().execute()
.actionGet().getState()
.getMetaData().aliases();
for (ObjectCursor<String> key: aliases.keys()) {
ImmutableOpenMap<String, AliasMetaData> indexToAliasesMap = client.admin().cluster()
.state(Requests.clusterStateRequest())
.actionGet().getState()
.getMetaData().aliases().get(key.value);
if(indexToAliasesMap != null && !indexToAliasesMap.isEmpty()){
String index= indexToAliasesMap.keys().iterator().next().value;
String alias = indexToAliasesMap.values().iterator().next().value.alias();
}
}
I did not like it... and after poking around, I was able to get an idea on how to do it more efficiently by looking at RestGetIndicesAliasesAction (package org.elasticsearch.rest.action.admin.indices.alias.get)
This is what I end up with:
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.routingTable(false)
.nodes(false)
.indices("your_index_name_goes_here");
ObjectLookupContainer<String> setAliases= client
.admin().cluster().state(clusterStateRequest)
.actionGet().getState().getMetaData()
.aliases().keys();
You will be able to find aliases for the index that you specified in setAliases
Hope it helps someone!
Upvotes: 3