Elasticsearch finds all indexes using Java client

Is there a way to use the Java client to get a list of indexes located in Elasticsearch? I was able to find examples of this using Marvel / Sense, but I cannot find examples of this using the Java client.

+8
source share
5 answers

This is definitely possible, but unfortunately it is 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(); 
+12
source

Another way I found for this:

 client.admin() .indices() .getIndex(new GetIndexRequest()) .actionGet() .getIndices() 
+10
source

Elasticsearch 6.5, RestHighLevelClient:

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

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

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

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 the regular expression: /_cat/indices?h=i&index=test*

0
source

Source: https://habr.com/ru/post/1233736/


All Articles