15

Reindexing takes 30 seconds and I don't want my search to be offline for 30 seconds every time I need to reindex. I'm trying to do this:

  1. Find old index with alias = abc123
  2. Create new index and fill with new data
  3. Remove alias and delete old index
  4. Give new index alias = abc123

I can't seem to find any java code that does 1). Everything else is fine. Anyone? or is there another way that is better?

Using Elasticsearch 0.90.9.

2
  • Are you using any admin plugin? Like elasticsearch-head, elasticsearch-hq Commented Aug 17, 2014 at 14:11
  • No. Just using the basic TransportClient() Commented Aug 17, 2014 at 14:14

5 Answers 5

18

Here is the method for your reference for finding all indices in a given aliasName:

public Set<String> getIndicesFromAliasName(String aliasName) {

    IndicesAdminClient iac = client.admin().indices();
    ImmutableOpenMap<String, List<AliasMetaData>> map = iac.getAliases(new GetAliasesRequest(aliasName))
            .actionGet().getAliases();

    final Set<String> allIndices = new HashSet<>();
    map.keysIt().forEachRemaining(allIndices::add);
    return allIndices;
}
Sign up to request clarification or add additional context in comments.

Comments

10

You can use this to get all of the aliases:

 client.admin().cluster()
    .prepareState().execute()
    .actionGet().getState()
    .getMetaData().getAliases();

This returns a map with the index name as the key and the aliases as the value. So you can iterate over the map to get the index name.

1 Comment

Actually, it seems to return a map in this format: alias => [ index_name => meta data, ... ]
4

a better solution would be , the map will have indexes as keys

GetAliasesResponse var = client.admin().indices().getAliases(new GetAliasesResponse var = client.admin().indices().getAliases(new GetAliasesRequest("merged")).actionGet();
ImmutableOpenMap<String, List<AliasMetaData>> v1 = var.getAliases();

1 Comment

I agree. Instead of getting the entire cluster's metadata, this is a better request
4

With the latest API the accepted answer no longer works. Similar solution using latest client (5.2.0):

SortedMap<String, AliasOrIndex> lookup = esClient.admin().cluster()
    .prepareState().execute()
    .actionGet().getState()
    .getMetaData().getAliasAndIndexLookup();
if (lookup.containsKey(ALIAS_NAME)) {
    lookup.get(ALIAS_NAME).getIndices().get(0).getIndex().getName();
}

Alternatively, you can use Aliases API like this: esClient.admin().indices().prepareGetAliases(ALIAS_NAME).get().getAliases();

Key of the resulting map is the index name that given alias maps to.

Comments

2
private String getIndexNameFromAliasName(final String aliasName) {
    ImmutableOpenMap<String, AliasMetaData> indexToAliasesMap = client.admin().cluster()
            .state(Requests.clusterStateRequest())
            .actionGet()
            .getState()
            .getMetaData()
            .aliases().get(aliasName);
    if(indexToAliasesMap != null && !indexToAliasesMap.isEmpty()){
        return indexToAliasesMap.keys().iterator().next().value;
    }
    return null;
}

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.