es实体类:
public class ChemicalES {@IndexId(type = IdType.CUSTOMIZE)private Long id;@HighLight@IndexField(fieldType = FieldType.TEXT, analyzer = "ik_max_word")private String name;@IndexField(fieldType = FieldType.KEYWORD)private List<String> productTags;
}
存入es中的查询结构:
现在希望对字段 productTags 进行聚合,想要聚合后结构如下:
[
{key:'化肥',value:2},
{key:'农药',value:1},
{key:'尿素',value:2},
]
请教如何操作呢? 我用easy-es的api进行聚合一直报错。
解决方案:
因为这种方式叫做嵌套聚合,所以无法使用easy-es相关api,easy-es的groupBy只适合 key_word类型的 非数组属性的字段,而数组属性的字段 需要利用restHighLevelClient。
以下是通用代码示例:
@Autowiredpublic RestHighLevelClient restHighLevelClient;private List<Map<Object,Object>> commonGroup( String indexName, MultiMatchQueryBuilder multiMatchQuery,String fieldName,String fieldNameAs) throws IOException {SearchRequest request = new SearchRequest();request.indices(indexName);SearchSourceBuilder builder = new SearchSourceBuilder();builder.size(0);AggregationBuilder aggregationBuilder = AggregationBuilders.terms(fieldNameAs).field(fieldName).order(BucketOrder.count(false));builder.aggregation(aggregationBuilder);if(multiMatchQuery != null){builder.query(multiMatchQuery);}request.source(builder);SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);Aggregations aggregations = response.getAggregations();ParsedStringTerms colorGroup = aggregations.get(fieldNameAs);List<? extends Terms.Bucket> buckets = colorGroup.getBuckets();List<Map<Object,Object>> result = new ArrayList<>();for (Terms.Bucket bucket : buckets) {Map<Object,Object> map =new HashMap<>(2);System.out.println("name:" + bucket.getKey() + "," + "count:" + bucket.getDocCount());map.put("name",bucket.getKey());map.put("count",bucket.getDocCount());result.add(map);}return result;}
返回的结果如图: