Rylander
Rylander

Reputation: 20119

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.

Upvotes: 11

Views: 14366

Answers (9)

Gael
Gael

Reputation: 634

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

Upvotes: 2

Rajiv Singh
Rajiv Singh

Reputation: 1078

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

Upvotes: 1

krizajb
krizajb

Reputation: 1814

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

Upvotes: 0

mishak471
mishak471

Reputation: 197

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

Upvotes: 1

Gurpreet Nanda
Gurpreet Nanda

Reputation: 49

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*

Upvotes: 2

user2394763
user2394763

Reputation: 51

Elasticsearch 6.5, RestHighLevelClient:

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

Upvotes: 5

Abhijith S
Abhijith S

Reputation: 532

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

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

Upvotes: 2

Rylander
Rylander

Reputation: 20119

Another way I found to do this:

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

Upvotes: 14

Val
Val

Reputation: 217274

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

Upvotes: 17

Related Questions