我有超过100个索引在弹性。我想返回所有索引的所有唯一值。
String[] instanceNames = getAllIndices().toArray(String[]::new);
Map<String, Map<String, Set<String>>> results = new HashMap<>();
for (String element : instanceNames) {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(element);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery);
searchSourceBuilder.aggregation(AggregationBuilders.terms("DISTINCT_VALUES").field("provider.keyword"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = null;
try {
searchResponse = getClient().search(searchRequest, RequestOptions.DEFAULT);
} catch (ElasticsearchException | IOException e) {
throw new ServiceException(I18n.ELASTIC_SEARCH_ERROR, e);
}
List<SearchHit> searchHits = Arrays.asList(searchResponse.getHits().getHits());
}
instanceNames -〉包含了我的elasticsearch中的所有索引。这个方法可以工作,但是返回数据非常慢。
1条答案
按热度按时间jm2pwxwz1#
好了,我解决了这个问题。我用AggregationBuilders创建了两个聚合。第一个是索引,第二个是提供者。