如何在 Elasticsearch java api 中按别名查找索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25349947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find Index by Alias in Elasticsearch java api?
提问by Tommy
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:
重新索引需要 30 秒,我不希望每次需要重新索引时我的搜索离线 30 秒。我正在尝试这样做:
- Find old index with alias = abc123
- Create new index and fill with new data
- Remove alias and delete old index
- Give new index alias = abc123
- 使用别名 = abc123 查找旧索引
- 创建新索引并填充新数据
- 删除别名并删除旧索引
- 赋予新索引别名 = 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?
我似乎找不到任何执行 1) 的 Java 代码。其他一切都很好。任何人?还是有另一种更好的方法?
Using Elasticsearch 0.90.9.
使用 Elasticsearch 0.90.9。
采纳答案by Dan Tuffery
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.
这将返回一个以索引名称作为 thekey
和 thealiases
作为值的映射。因此,您可以遍历地图以获取索引名称。
回答by user1111566
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;
}
回答by Ankur Goel
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();
回答by jderda
With the latest API the accepted answer no longer works. Similar solution using latest client (5.2.0):
使用最新的 API,接受的答案不再有效。使用最新客户端(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();
或者,您可以像这样使用别名 API:
esClient.admin().indices().prepareGetAliases(ALIAS_NAME).get().getAliases();
Key of the resulting map is the index name that given alias maps to.
结果映射的键是给定别名映射到的索引名称。
回答by Nawa
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;
}