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