- 查询某个字段值出现的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聚合设置进去,并且使用nestedNestedAggregationBuilder 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);