¿Cómo obtener todos los índices con Elastic's High Level Rest Client?
Quiero una manera agradable, rápida y fácil de obtener todos los índices en Elasticsearch usando suJava REST client. Actualmente puedo hacer esto agarrando a su cliente de nivel inferior, así:
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;
}
}
Esencialmente solo llamo a/_cat/indices?v
punto final como se recomienda en sus documentos. Esto funciona bien, pero me preguntaba si había una mejor manera de hacerlo usando la API de Java. Parece que no puedo encontrar una manera en su API actual, pero me pregunto si alguien sabe algo que yo no. Tener que trabajar conInputStream
s y los variosReader
s no es necesariamente terrible, pero solo quiero limpiar el análisis de cadenas hacky.