Elasticsearch Java Rest Client: how to get the list of all indices

馋奶兔 提交于 2021-02-10 05:05:39

问题


How do I get the list of all indices in Elasticsearch using the Rest Client?

(All answers I've found online seem to deal with the old type of client.

I fail to find the direct answer in the doc,

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html

can't figure out which section to look into, either Cluster or Index APIs etc.)


回答1:


Via the REST API you can verify with this URL : http://elasticsearch:9200/_cat/indices?v

Via the Java Client API (I just realised you asked this way) : you can bet on the Cluster Health API : https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-cluster-health.html

And use

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

And you will get the list of indices ;)




回答2:


In current Java High Level REST Client you can list all indices simply by requesting a GetIndex request with "*" as an index name.

GetIndexRequest request = new GetIndexRequest().indices("*");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
String[] indices = response.getIndices();


来源:https://stackoverflow.com/questions/53413584/elasticsearch-java-rest-client-how-to-get-the-list-of-all-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!