(六)ElasticSearch7.X之聚合查询java代码(指定队伍指定字段的cardinality(去重之后总数))

POST /nba/_search
{
"query": {
"term": {
"teamNameEn.keyword": {
"value": "Rockets"
}
}
},
"aggs": {
"counAget": {
"cardinality": {
"field": "playYear"
}
}
},
"size": 0
}
 @Test
    public void test05() throws IOException {
        SearchRequest searchRequest = new SearchRequest("nba");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //限定了查询的队伍名称
        searchSourceBuilder.query(QueryBuilders.termQuery("teamName.keyword","火箭")).size(100);
        CardinalityAggregationBuilder cardinalityAggregationBuilder = AggregationBuilders.cardinality("playYear").field("playYear");
        searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        Cardinality valueCount = search.getAggregations().get("playYear");
        long value = valueCount.getValue();
        System.out.println(value);
    }

你可能感兴趣的:(API))