Es 聚合查询 nested分组聚合时无需指定字段类型

文章介绍了如何在Elasticsearch中通过nested聚合查询来获取游客来源地的前五个城市的统计信息,包括使用terms聚合和value_count以及nestedAggregationBuilder进行数据分组和排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 查询某个字段值出现的topn

                查询城市top5

  AggregationBuilder aggregationBuilder = AggregationBuilders
                .terms("value_count").field("neste.cityId").size(5);

        NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("nested的字段名", "nested的字段名").subAggregation(aggregationBuilder);
        searchSourceBuilder.aggregation(nestedAggregationBuilder);

      2.nested分组聚合查询

        

  AggregationBuilder provinceTermsBuilder= AggregationBuilders.terms("top5").field("nestedTouristList.cityId")
                .subAggregation(touristCount)
                .order( BucketOrder.aggregation("touristCount", false));




        //在最外层的查询将agg聚合设置进去,并且使用nested
       NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("nestedTouristList", "nestedTouristList")
               .subAggregation(provinceTermsBuilder);

        

行程单索引,一个订单对应多个游客,将游客信息采用nested类型存入订单索引中

{
    "mappings": {
        "_doc": {
            "properties": {
                //oid
                "oid": {
                    "type": "keyword"
                },
                "routeName": {
                    "type": "keyword"
                },
                
            
                "nestedTouristList": {
                    "type": "nested",
                    "properties": {
                        "oid": {
                            "type": "keyword"
                        },
                        "itineraryId": {
                            "type": "long"
                        },
                        "certificateNo": {
                            "type": "keyword"
                        },
                        "certificateNoEncrypt": {
                            "type": "keyword"
                        },
                        "certificateType": {
                            "type": "keyword"
                        },
                        "name": {
                            "analyzer": "whitespace",
                            "type": "text"
                        },
                        "nameEncrypt": {
                            "type": "keyword"
                        },
                        "phone": {
                            "type": "keyword"
                        },
                        "phoneEncrypt": {
                            "type": "keyword"
                        },
                        "gender": {
                            "type": "keyword"
                        },
                        "age": {
                            "type": "keyword"
                        },
                        "sourceAddress": {
                            "type": "keyword"
                        },
                        "sourceAddressName": {
                            "type": "keyword"
                        },
                        "provinceId": {
                            "type": "keyword"
                        },
                        "cityId": {
                            "type": "keyword"
                        }
      
                }
                
            }
        }
    }

实列:

统计游客来源地top5

方式一:

 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//查询条件
searchSourceBuilder.query(nestedQueryBuilder);
//统计数量
ValueCountAggregationBuilder touristCount = AggregationBuilders.count("touristCount")
    										.field("nestedTouristList.oid");
//分组条件
AggregationBuilder provinceTermsBuilder= AggregationBuilders.terms("top5")
    	.field("nestedTouristList.cityId")
                .subAggregation(touristCount)
                .order( BucketOrder.aggregation("touristCount", false));




        //在最外层的查询将agg聚合设置进去,并且使用nested
NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders
    .nested("nestedTouristList", "nestedTouristList")
 .subAggregation(provinceTermsBuilder);
 searchSourceBuilder.aggregation(nestedAggregationBuilder);


//取数据
Aggregations brandNameAggregations = searchResponse.getAggregations();
        ParsedNested brandDataNested = brandNameAggregations.get("nestedTouristList");
        Aggregations brandDataNestedAggregations = brandDataNested.getAggregations();
        Terms cityTerms = brandDataNestedAggregations.get("top5");
        for (Terms.Bucket cityTermsBucket : cityTerms.getBuckets()) {
            Aggregations aggregationsSon = cityTermsBucket.getAggregations();
            String keyAsString = cityTermsBucket.getKeyAsString();
            long docCount = cityTermsBucket.getDocCount();
            ParsedValueCount brandSalesVolumeMax1 = aggregationsSon.get("touristCount");
            Long value = brandSalesVolumeMax1.getValue();
            System.out.println("cc" + value);
        }

方式二

//统计出现频数最多的前5条
AggregationBuilder aggregationBuilder = AggregationBuilders
                .terms("value_count").field("nestedTouristList.cityId").size(5);

 NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("nestedTouristList", "nestedTouristList")
searchSourceBuilder.aggregation(nestedAggregationBuilder);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值