【ElasticSearch】spring-data方式对ES进行聚合操作(二)


Spring-Data方式对ES进行分组聚合操作


上文【ElasticSearch】spring-data方式操作elasticsearch(一)进阶


1.10 搜索-分组



@Test
public void searchAggr() {

    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder()
        .addAggregation(AggregationBuilders.terms("ageCount")
                        .field("age")
                        .size(100)
                        .subAggregation(AggregationBuilders.terms("professionalCount").field("professional.keyword")
                                        .subAggregation(AggregationBuilders.topHits("fieldNms")
                                                        .fetchSource(new String[] {"uid","age","professional","name"}, null))));

    // 分组聚合分页结果
    AggregatedPage<PeopleBean> peopleAggrPage = (AggregatedPage<PeopleBean>)peopleEsDao.search(queryBuilder.build());

    System.out.println("结果:");
    ParsedLongTerms ageAggr = (ParsedLongTerms)peopleAggrPage.getAggregation("ageCount");
    List<? extends Bucket> buckets = ageAggr.getBuckets();
    for (Bucket bucket : buckets) {

        Number age = bucket.getKeyAsNumber();
        long docCount = bucket.getDocCount();

        System.out.println("年龄:"+age + " 总数:" + docCount);

        Aggregations aggregations = bucket.getAggregations();
        for (Aggregation aggregation : aggregations) {

            ParsedStringTerms profAggr = (ParsedStringTerms) aggregation;
            List<? extends Bucket> profBuckets = profAggr.getBuckets();
            for (Bucket bucket2 : profBuckets) {
                long profCount = bucket2.getDocCount();
                String prof = bucket2.getKeyAsString();
                System.out.println("\t职业:"+prof+" 总数:"+profCount);

                Aggregations filedNmAggrs = bucket2.getAggregations();
                for (Aggregation filedNmAggr : filedNmAggrs) {

                    ParsedTopHits topHits = (ParsedTopHits) filedNmAggr ;
                    SearchHits hits = topHits.getHits();
                    for (SearchHit hit : hits) {
                        String source = hit.getSourceAsString();
                        System.out.println("\t\t" + JSON.parseObject(source, PeopleBean.class));
                    }

                }
            }

        }

    }


}


类似SQL:


select 
	uid, age, professional, name
from people
group by age, professional


结果:
结果:
年龄:45 总数:4
	职业:演员 总数:2
		PeopleBean(uid=8, name=张家辉, age=45, addr=null, birthDay=null, professional=演员, interest=null)
		PeopleBean(uid=1, name=吴彦祖, age=45, addr=null, birthDay=null, professional=演员, interest=null)
	职业:歌手 总数:1
		PeopleBean(uid=9, name=毛不易, age=45, addr=null, birthDay=null, professional=歌手, interest=null)
	职业:武打演员 总数:1
		PeopleBean(uid=3, name=吴京, age=45, addr=null, birthDay=null, professional=武打演员, interest=null)
年龄:30 总数:2
	职业:歌手 总数:1
		PeopleBean(uid=6, name=刘亦菲, age=30, addr=null, birthDay=null, professional=歌手, interest=null)
	职业:运动员 总数:1
		PeopleBean(uid=7, name=刘翔, age=30, addr=null, birthDay=null, professional=运动员, interest=null)
年龄:55 总数:2
	职业:歌手 总数:1
		PeopleBean(uid=2, name=吴奇隆, age=55, addr=null, birthDay=null, professional=歌手, interest=null)
	职业:演员 总数:1
		PeopleBean(uid=4, name=古天乐, age=55, addr=null, birthDay=null, professional=演员, interest=null)
年龄:35 总数:1
	职业:运动员 总数:1
		PeopleBean(uid=5, name=苏炳添, age=35, addr=null, birthDay=null, professional=运动员, interest=null)
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天涯共明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值