如何在ElasticSearch中找到索引的别名

时间:2014-10-08 16:28:45

标签: elasticsearch

我想找出ElasticSearch中给定索引的别名。基本上,这个问题的反面是:How to find Index by Alias in Elasticsearch java api?

1 个答案:

答案 0 :(得分:2)

这就是我做到的。

// Get the map of [alias => [index => metadata, ...], ...]
ImmutableOpenMap<String, ImmutableOpenMap<String, AliasMetaData>> aliasesAndIndices = 
        client.admin().cluster()
        .prepareState().execute().actionGet().getState()
        .getMetaData().getAliases();
Map<String, String> aliasForIndex = new HashMap<>();

// Convert it to a map of [index => alias, ...]
for (String alias : aliasesAndIndices.keys().toArray(String.class)) {
    ImmutableOpenMap<String, AliasMetaData> innerMap = aliasesAndIndices.get(alias);
    for (String index : innerMap.keys().toArray(String.class)) {
        aliasForIndex.put(index, alias);
    }
}

然后我可以得到像这样的索引的别名。

aliasForIndex.get(index)