Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Index by Alias in Elasticsearch java api?

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.

like image 997
Tommy Avatar asked Aug 17 '14 14:08

Tommy


2 Answers

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;
}
like image 118
user1111566 Avatar answered Oct 24 '22 08:10

user1111566


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.

like image 10
Dan Tuffery Avatar answered Oct 24 '22 09:10

Dan Tuffery