亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Elastic 的 High Level Rest Client 獲取所有索引?

如何使用 Elastic 的 High Level Rest Client 獲取所有索引?

慕森王 2021-08-04 16:47:29
我想要一種很好、快速和簡單的方法來使用他們的Java REST 客戶端獲取 elasticsearch 中的所有索引。我目前可以通過抓取他們的低級客戶端來做到這一點,如下所示: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 中找到方法,但想知道是否有人知道我不知道的事情。必須與s 和各種s一起工作并不一定很糟糕,但只想清理 hacky 字符串解析。InputStreamReader
查看完整描述

3 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

從 Elasticsearch 7.5.0 開始,您可以使用以下內容來檢索所有索引:

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


查看完整回答
反對 回復 2021-08-04
?
浮云間

TA貢獻1829條經驗 獲得超4個贊

在 es 6.8 上,當沒有索引時使用*or _allinGetIndexRequest會拋出 NoSuchIndexException。


我發現這是更安全的,沒有扔的方式:


    GetMappingsResponse response = esClient.indices().getMapping(new GetMappingsRequest().indices("*"),

            RequestOptions.DEFAULT);

    return new ArrayList<>(response.mappings().keySet());


查看完整回答
反對 回復 2021-08-04
  • 3 回答
  • 0 關注
  • 717 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號