【问题标题】:ElasticSeach JAVA API to find aliases given indexElasticSearch JAVA API 查找给定索引的别名
【发布时间】:2015-07-01 19:32:41
【问题描述】:

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

使用 REST API 非常简单

https://www.elastic.co/guide/en/elasticsearch/reference/1.x/indices-aliases.html#alias-retrieving

但我找不到任何关于如何通过 Java API 进行操作的好的参考

【问题讨论】:

  • 这似乎不适合这里的问答格式。我不太确定您会认为哪些部分是问题,哪些是解决方案的一部分,所以我不会尝试自己编辑。你能编辑它以适应格式吗?
  • 已编辑。分开的问题和答案。

标签: java lucene elasticsearch


【解决方案1】:

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

虽然获取别名列表非常简单:

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

我努力寻找一种简单的方法来获取给定索引的别名,而不必先遍历所有内容。

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

    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(包 org.elasticsearch.rest.action.admin.indices.alias.得到)

这就是我最终的结果:

    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 中指定的索引的别名

希望对某人有所帮助!

【讨论】:

    【解决方案2】:

    之前的答案对于 ElasticSearch 2.x 是可以的。不过,在 ElasticSearch 的 5.x 版中,情况略有变化。

    这是 5.x 版对我有用的东西:

    public Collection<String> findAliasForIndices(String... indices) {
        if(indices == null || indices.length == 0) {
            return Collections.emptyList();
        }
        ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
                .routingTable(false)
                .nodes(false)
                .indices(indices);
    
        ImmutableOpenMap<String, IndexMetaData> aliasMap = client
                .admin().cluster().state(clusterStateRequest)
                .actionGet().getState().getMetaData()
                .getIndices();
    
        return StreamSupport.stream(aliasMap.spliterator(), false).flatMap((e) -> {
            Iterable<String> iterable = () -> e.value.getAliases().keysIt();
            return StreamSupport.stream(iterable.spliterator(), false);
        }).collect(Collectors.toSet());
    }
    

    此方法将所有别名作为索引集合的字符串返回。

    JUnit 和 AssertJ 的使用示例:

    @Test
    public void whenFindAliasForIndices_ShouldRetrieveIndices() throws IOException {
        String testIndex = "blah1";
        String alias1 = "alias1";
        createIndexWithAlias(testIndex, alias1);
    
        String testIndex2 = "blah2";
        String alias2 = "alias2";
        createIndexWithAlias(testIndex2, alias2);
    
        Collection<String> indices = adapter.findAliasForIndices(testIndex, testIndex2);
        assertThat(indices.contains(alias1)).isTrue();
        assertThat(indices.contains(alias2)).isTrue();
        adapter.deleteIndices(testIndex);
        adapter.deleteIndices(testIndex2);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多