简体   繁体   中英

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

In my elasticsearch I want to get all the indices' name of the cluster. How can I do using java? I search the internet but there's no much useful information.

You can definitely do it with the following simple Java code:

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

The list you obtain contains the details on all the indices available in your ES cluster.

You can use:

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

Use setFeatures() without parameter to just get index name. Otherwise, other data, such as MAPPINGS and SETTINGS of index, will also be returned by default.

Thanks for @Val's answer. According to your method, I use it in my projects, the code is:

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

This method can put all the indices name into a String array. The method works.

there's another method I think but not tried:

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

then, we can get the keys of mappings to get all the indices. Thanks again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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