侧边栏壁纸
  • 累计撰写 106 篇文章
  • 累计创建 3 个标签
  • 累计收到 19 条评论
标签搜索

目 录CONTENT

文章目录

elasticsearch-RestClient数据聚合

卑微幻想家
2022-01-28 / 0 评论 / 0 点赞 / 218 阅读 / 931 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-03-24,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

上一节中我们讲到DSL的数据聚合操作,这一节我们讲用RestClient如果进行数据聚合操作

先看图

img

聚合的结果也与查询结

果不同,API 也比较特殊。不过同样是 JSON 逐层解析

img

代码演示

@Test
    public void testAggregation() throws IOException {
        SearchRequest request = new SearchRequest("hotel");
        request.source().aggregation(AggregationBuilders.terms("brandAgg").field("brand").size(20));
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        Terms brandAgg = response.getAggregations().get("brandAgg");
        List<? extends Terms.Bucket> buckets = brandAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            String key = bucket.getKeyAsString();
            long count = bucket.getDocCount();
            System.out.println("key = " + key+", count = " + count);
        }
    }

结果

key = 7天酒店, count = 30
key = 如家, count = 30
key = 皇冠假日, count = 17
key = 速8, count = 15
key = 万怡, count = 13
key = 华美达, count = 13
key = 和颐, count = 12
key = 万豪, count = 11
key = 喜来登, count = 11
key = 希尔顿, count = 10
key = 汉庭, count = 10
key = 凯悦, count = 8
key = 维也纳, count = 7
key = 豪生, count = 6
key = 君悦, count = 4
key = 万丽, count = 2
key = 丽笙, count = 2
0

评论区