2

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

2
  • This doesn't really seem to fit the Q&A format here. I'm not quite sure which parts you would consider to be the question, and which are part of the solution, so I won't attempt to edit myself. Would you be able to edit this to suit the format? Commented Jul 1, 2015 at 20:08
  • 1
    Edited. Separated question and answer. Commented Jul 2, 2015 at 18:13

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.