QueryBuilder querybuilder = QueryBuilders.matchAllQuery();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(querybuilder);
// they are two records with same value
searchSourceBuilder.aggregation(AggregationBuilders.global("agg")
.subAggregation(AggregationBuilders.terms("label_agg")
.field("myField").size(2)));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("index name");
searchRequest.source(searchSourceBuilder);
RestHighLevelClient restHighLevelClient = elasticSearchConfig.createInstance();
try {
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
long hits = searchResponse.getHits().getTotalHits();
if (hits > 0) {
ObjectMapper objectMapper = new ObjectMapper();
for (SearchHit hit : searchResponse.getHits()) {
String jsonSource = hit.getSourceAsString();
}
} else {
log.debug("No results matching the criteria.");
}
}catch(Exception ae) {
ae.toString();
}
Я пытаюсь получить отдельное значение из имени поля. но это дало мне ошибку всех шардов. sql запрос будет эквивалентен запросу «ВЫБРАТЬ отдельный myField FROM имя_таблицы»