在elasticsearch中获取唯一的索引名称

时间:2018-02-18 11:03:39

标签: java spring elasticsearch elasticsearch-plugin spring-data-elasticsearch

使用ES rest客户端,如何根据某些搜索条件获取所有唯一索引名称的列表。下面的代码给出了我的所有索引名称,但也是重复的。请帮忙。

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 

boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.user","95103"));
boolQueryBuilder.must(QueryBuilders.matchQuery("indexMetadata.indexLevel","LOCAL"));

sourceBuilder.query(boolQueryBuilder);

SearchRequest searchRequest = new SearchRequest();
String[] includeFields = new String[]{"hits.hits._index"};
String[] excludeFields = new String[]{};
sourceBuilder.fetchSource(includeFields, excludeFields);

searchRequest.source(sourceBuilder);

SearchResponse searchResponse = client.search(searchRequest);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
       for (SearchHit hit : searchHits) {
            System.out.println(hit.getIndex());
}

1 个答案:

答案 0 :(得分:0)

您可以使用_cat API

GET /_cat/indices

它返回集群中存在的所有索引,没有重复项。