'Elasticsearch find all indexes using the Java client

Is there a way to use the Java client to get a list of indexes that are in Elasticsearch? I have been able to find examples of doing this using Marvel/Sense, but I cant seem to find any examples of doing this using the Java client.



Solution 1:[1]

It's definitely possible but it's unfortunately not documented in the official documentation for the Java client. You can achieve this with:

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

Solution 2:[2]

Another way I found to do this:

client.admin()
    .indices()
    .getIndex(new GetIndexRequest())
    .actionGet()
    .getIndices()

Solution 3:[3]

Elasticsearch 6.5, RestHighLevelClient:

ClusterHealthRequest request = new ClusterHealthRequest();
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
Set<String> indices = response.getIndices().keySet();

Solution 4:[4]

This also works for Elasticsearch 6.2.3 and java API-client 6.2.3:

String[] indices = client.admin().indices().prepareGetIndex().setFeatures().get().getIndices();

Solution 5:[5]

For RestHighLevelClient:

Try using: /_cat/indices?h=i

InputStream inputStream = restHighLevelClient.getLowLevelClient()
.performRequest("GET", "/_cat/indices?h=i")
.getHttpResponse()
.getEntity()
.getContent();

List<String> indexes = new BufferedReader(new InputStreamReader(inputStream))
    .lines()
    .collect(Collectors.toList());

Also, if you want to search using a regex: /_cat/indices?h=i&index=test*

Solution 6:[6]

ElasticSearch 8

import co.elastic.clients.elasticsearch.cat.IndicesResponse;
import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;

...

IndicesResponse indicesResponse = client.cat().indices();

for (IndicesRecord indicesRecord : indicesResponse.valueBody()) {
    String index = indicesRecord.index();
}

Solution 7:[7]

I'm using client version 6.8.0 and I was able to get indices like this:

GetIndexRequest getIndexRequest = new GetIndexRequest("*")
           .indicesOptions(IndicesOptions.lenientExpandOpen());
String[] indices = highLevelRestClient.indices()
           .get(getIndexRequest, RequestOptions.DEFAULT).getIndices();

So actually "*" is a wildcard here and you can filter your indices by prefix like "my_index-*"

Link to the documentaion - elasticsearch java REST client Get Index API

Solution 8:[8]

I found this to be working for the 6.6.2 version.

SearchRequest searchRequest = new SearchRequest();
SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = response.getHits().getHits();

Solution 9:[9]

Elasticsearch version 7.9.2

Request request = new Request("GET", "/_cat/indices?h=i");
    
InputStream inputStream = restHighLevelClient.getLowLevelClient()
            .performRequest(request)
            .getEntity()
            .getContent();

List<String> indexes = new BufferedReader(new 
InputStreamReader(inputStream))
            .lines()
            .collect(Collectors.toList());

    for(String indexName: indexes){
        System.out.println("indexName: "+indexName);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Val
Solution 2 Mike Rylander
Solution 3 user2394763
Solution 4
Solution 5 veben
Solution 6 Gael
Solution 7 mishak471
Solution 8 krizajb
Solution 9 Rajiv Singh