Elasticsearch分析聚合介绍了分析聚合的REST命令,这篇博客介绍一下如何使用Java API。
一、准备数据
测试数据请参考我的上一篇博客:Elasticsearch分析聚合。
二、需求
查询title中包含关键字”程序”的文档,统计查询按编程语言分组,统计每组的文档数量。
三、REST命令行
REST命令行如下,有三部分,query查询所有title中包含关键词程序
的文档,size指定返回结果中文档数量,其值为0只返回聚合的结果,aggs部分通过language字段进行分组。
GET books/_search
{
"query": {
"match": {
"title": "程序"
}
},
"size": 0,
"aggs": {
"per_count": {
"terms": {
"field": "language"
}
}
}
}
查询结果如下:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"per_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "java",
"doc_count": 1
},
{
"key": "javascript",
"doc_count": 1
}
]
}
}
}
四、聚合的JAVA API
//创建TransportClient对象
client=EsUtils.getEsClient();
QueryBuilder matchQuery = QueryBuilders.matchQuery("title", "程序");
AbstractAggregationBuilder aggregation = AggregationBuilders.terms("per_count").field("language");
SearchResponse response = client.prepareSearch("books").setTypes("IT")
.setQuery(matchQuery)
.addAggregation(aggregation)
.execute()
.actionGet();
SearchHits hits = response.getHits();
for(SearchHit hit:hits){
System.out.println("id:"+hit.getId()+"\ttitle:"+hit.getSource().get("title"));
}
Terms terms = response.getAggregations().get("per_count");
List<Bucket> buckets = terms.getBuckets();
for(Bucket bucket:buckets){
System.out.println(bucket.getKey()+"----"+bucket.getDocCount());
}
client.close();
运行结果:
id:2 title:Java程序性能优化
id:5 title:JavaScript高级程序设计
java----1
javascript----1