我想要一種很好、快速和簡(jiǎn)單的方法來(lái)使用他們的Java REST 客戶(hù)端獲取 elasticsearch 中的所有索引。我目前可以通過(guò)抓取他們的低級(jí)客戶(hù)端來(lái)做到這一點(diǎn),如下所示: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; }}基本上我只是按照他們的文檔中的建議調(diào)用/_cat/indices?v端點(diǎn)。這工作正常,但我想知道是否有更好的方法使用 Java API 來(lái)做到這一點(diǎn)。我似乎無(wú)法在他們當(dāng)前的 API 中找到方法,但想知道是否有人知道我不知道的事情。必須與s 和各種s一起工作并不一定很糟糕,但只想清理 hacky 字符串解析。InputStreamReader
3 回答

繁星點(diǎn)點(diǎn)滴滴
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
從 Elasticsearch 7.5.0 開(kāi)始,您可以使用以下內(nèi)容來(lái)檢索所有索引:
GetIndexRequest request = new GetIndexRequest("*"); GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT); String[] indices = response.getIndices();

浮云間
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
在 es 6.8 上,當(dāng)沒(méi)有索引時(shí)使用*or _allinGetIndexRequest會(huì)拋出 NoSuchIndexException。
我發(fā)現(xiàn)這是更安全的,沒(méi)有扔的方式:
GetMappingsResponse response = esClient.indices().getMapping(new GetMappingsRequest().indices("*"),
RequestOptions.DEFAULT);
return new ArrayList<>(response.mappings().keySet());
添加回答
舉報(bào)
0/150
提交
取消