繁体   English   中英

如何使用java列出elasticsearch的所有索引名称?

[英]how to list all the indices' name of elasticsearch using java?

在我的elasticsearch中,我想获取集群的所有索引名称。 如何使用Java? 我在互联网上搜索,但是没有太多有用的信息。

您绝对可以使用以下简单的Java代码来做到这一点:

List<IndexMetaData> indices = client.admin().cluster()
    .prepareState().get().getState()
    .getMetaData().getIndices();

您获得的列表包含ES群集中​​所有可用索引的详细信息。

您可以使用:

client.admin().indices().prepareGetIndex().setFeatures().get().getIndices();

使用不带参数的setFeatures()仅获取索引名称。 否则,默认情况下还将返回其他数据,例如索引的MAPPINGSSETTINGS

感谢@Val的回答。 根据您的方法,我在项目中使用它,代码为:

ClusterStateResponse response = transportClient.admin().cluster() .prepareState() 
    .execute().actionGet(); 
String[] indices=response.getState().getMetaData().getConcreteAllIndices();

此方法可以将所有索引名称放入String数组。 该方法有效。

我认为但没有尝试过另一种方法:

ImmutableOpenMap<String, MappingMetaData> mappings = node.client().admin().cluster()
    .prepareState().execute().actionGet().getState().‌getMetaData().getIndices(). 

然后,我们可以获取映射的键以获取所有索引。 再次感谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM