本文介绍了ElasticSeach JAVA API找到给定索引的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Java在ElasticSearch中找到给定索引的别名?

How to find aliases for given index in ElasticSearch using Java?

通过使用REST API很容易

By using REST API it is pretty easy

但是我无法找到任何关于如何通过Java API做的好的参考

But I was unable to find any good reference on how to do it via Java API

推荐答案

在使用ElasticSearch时,遇到了一个问题,需要根据提供的索引获取别名列表。

While working with ElasticSearch, I ran into an issue where I needed to get a list of aliases based on provided index.

获取别名列表非常简单:

While getting a list of aliases is pretty straightforward:

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

我努力寻找一种简单的方式来获取给定索引的别名,而无需迭代一切都是第一个。

I struggled to find an easy way to be able to get aliases for given index without having to iterate through everything first.

我的第一个实现看起来像这样:

My first implementation looked something like this:

    ImmutableOpenMap<String, ImmutableOpenMap<String, AliasMetaData>> aliases = client.admin().cluster()
        .prepareState().execute()
        .actionGet().getState()
        .getMetaData().aliases();

    for (ObjectCursor<String> key: aliases.keys()) {
        ImmutableOpenMap<String, AliasMetaData> indexToAliasesMap = client.admin().cluster()
          .state(Requests.clusterStateRequest())
          .actionGet().getState()
          .getMetaData().aliases().get(key.value);

        if(indexToAliasesMap != null && !indexToAliasesMap.isEmpty()){
            String index= indexToAliasesMap.keys().iterator().next().value;
            String alias = indexToAliasesMap.values().iterator().next().value.alias();
        }
    }

我不喜欢它,周围,​​我能够通过查看RestGetIndicesAliasesAction(package org.elasticsearch.rest.action.admin.indices.alias.get)获得有关如何更有效的想法。

I did not like it... and after poking around, I was able to get an idea on how to do it more efficiently by looking at RestGetIndicesAliasesAction (package org.elasticsearch.rest.action.admin.indices.alias.get)

这是我最终的结论:

    ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
            .routingTable(false)
            .nodes(false)
            .indices("your_index_name_goes_here");

    ObjectLookupContainer<String> setAliases= client
            .admin().cluster().state(clusterStateRequest)
            .actionGet().getState().getMetaData()
            .aliases().keys();

您将能够找到您在setAliases中指定的索引的别名

You will be able to find aliases for the index that you specified in setAliases

希望它帮助某人!

这篇关于ElasticSeach JAVA API找到给定索引的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:23