有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Elasticsearch使用Java客户端查找所有索引

有没有办法使用Java客户机获取Elasticsearch中的索引列表?我已经找到了使用Marvel/Sense做这件事的例子,但我似乎找不到任何使用Java客户端做这件事的例子


共 (4) 个答案

  1. # 1 楼答案

    Elasticsearch 6.5,RestHighLevelClient:

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

    对于RestHighLevelClient

    尝试使用:/_cat/indices?h=i

    InputStream inputStream = restHighLevelClient.getLowLevelClient()
    .performRequest("GET", "/_cat/indices?h=i")
    .getHttpResponse()
    .getEntity()
    .getContent();
    
    List<String> indexes = new BufferedReader(new InputStreamReader(inputStream))
        .lines()
        .collect(Collectors.toList());
    

    另外,如果要使用正则表达式进行搜索:/_cat/indices?h=i&index=test*

  3. # 3 楼答案

    这当然是可能的,但不幸的是,Java客户机的官方文档中没有记录这一点。您可以通过以下方式实现这一点:

    List<IndexMetaData> indices = client.admin().cluster()
        .prepareState().get().getState()
        .getMetaData().getIndices();
    
  4. # 4 楼答案

    我使用的是客户机版本6.8.0,我可以得到如下索引:

    GetIndexRequest getIndexRequest = new GetIndexRequest("*")
               .indicesOptions(IndicesOptions.lenientExpandOpen());
    String[] indices = highLevelRestClient.indices()
               .get(getIndexRequest, RequestOptions.DEFAULT).getIndices();
    

    所以实际上“*”在这里是一个通配符,你可以通过前缀过滤你的索引,比如“my_index-*”

    链接到文档-elasticsearch java REST client Get Index API