我正在使用elasticsearch 5.4
我需要使用elasticsearch query
获取不同的字段值我正在尝试此查询,但不是在考虑
GET /index/index_type/_search
{
"size": 0,
"aggs": {
"distinct": {
"terms": {
"field": "status"
}
}
}
谢谢
答案 0 :(得分:0)
解决方案
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
TransportClient client;
SearchResponse response = client.prepareSearch(indexName).setTypes(typeName)
.addAggregation(AggregationBuilders.terms("distinct_line_status").field("line_status.keyword"))
.setSize(0).get();
//AggregationProfileShardResult searchHits = response.getAggregations().getAsMap();
System.out.println(response);
Terms genders = response.getAggregations().get("distinct_line_status");
// For each entry
for (Terms.Bucket entry : genders.getBuckets()) {
System.out.println(entry.getKey()); // Term
System.out.println(entry.getDocCount()); // Doc count
}
答案 1 :(得分:0)
您可以使用基数来实现此目的。
GET /index/index_type/_search
{
"size": 0,
"aggs": {
"distinct": {
"cardinality": {
"field": "status"
}
}
}