11

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.

9 Answers 9

17

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();
Sign up to request clarification or add additional context in comments.

Comments

14

Another way I found to do this:

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

1 Comment

In case anyone else gets here and is wondering, this one returns a String[] which may be easier to deal with.
5

Elasticsearch 6.5, RestHighLevelClient:

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

Comments

2

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

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

Comments

2

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*

Comments

2

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();
}

Comments

1

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

Comments

1
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);
    }

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.