如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:52:05  来源:igfitidea点击:

How to find Index by Alias in Elasticsearch java api?

javaelasticsearch

提问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 秒。我正在尝试这样做:

  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
  1. 使用别名 = abc123 查找旧索引
  2. 创建新索引并填充新数据
  3. 删除别名并删除旧索引
  4. 赋予新索引别名 = 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 keyand the aliasesas 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;
}