如何使用Elastic的High Level Rest Client获取所有索引? [英] How to get all indices with Elastic's High Level Rest Client?
问题描述
我希望使用它们的 Java REST客户端。我目前能够通过抓住他们的较低级别的客户端来做到这一点,就像这样:
I want a nice, quick and easy way to get all of the indices in elasticsearch using the their Java REST client. I am currently able to do this by grabbing their lower level client, like this:
public void fetchIndices() throws IOException {
List<String> indices = null;
RestClient restClient = client.getLowLevelClient();
Response response = null;
try {
response = restClient.performRequest("GET", "/_cat/indices?v");
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.toString(), e);
}
InputStream inputStream = null;
if (response != null) {
try {
inputStream = response.getEntity().getContent();
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.toString(), e);
}
}
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
indices = new ArrayList<>();
String line;
while ((line = bufferedReader.readLine()) != null) {
// Get tokens with no whitespace
String[] tokens = line.split("\\s+");
for (String token : tokens) {
// TODO - make the startsWith() token configurable
if (token.startsWith(SOME_TOKEN)) {
LOGGER.log(Level.INFO, "Found elasticsearch index " + token);
indices.add(token);
break;
}
}
}
}
// Only update if we got data back from our REST call
if (indices != null) {
this.indices = indices;
}
}
基本上,我只是称 / _cat / indices?v
端点按照他们的文档中的建议。这可以正常工作,但是我想知道是否有更好的方法来使用Java API。我似乎无法在他们当前的API中找到方法,但想知道是否有人知道我不了解的内容。必须使用 InputStream
s和各种 Reader
s并不一定很糟糕,而只是想清理hacky字符串解析。
Essentially I just call the /_cat/indices?v
endpoint as recommended in their docs. This works fine, but I was wondering if there was a nicer way to do this using the Java API. I can't seem to find a way in their current API, but wondering if anyone knows something I don't. Having to work with InputStream
s and the various Reader
s isn't necessarily terrible, but just want to clean up the hacky string parsing.
推荐答案
从Elasticsearch 7.5.0开始,您可以使用以下内容检索所有索引:
As of Elasticsearch 7.5.0 you can use the following to retrieve all indices:
GetIndexRequest request = new GetIndexRequest("*");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
String[] indices = response.getIndices();
这篇关于如何使用Elastic的High Level Rest Client获取所有索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!