文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

ES结合java代码聚合查询详细示例

2023-05-19 08:11

关注

思路不清晰的小伙伴可以先在es中把聚合代码写出来

 
{
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brandName.keyword"
      },
      "aggs": {
        "typeAgg": {
        "terms": {
          "field": "typeTwoName.keyword"
        }
        },
        "ruleAgg": {
          "terms": {
            "field": "ruleName.keyword"
          },
            "aggs": {
                "ruleValueAgg": {
                  "terms": {
                    "field": "ruleAttrValue.keyword"
                  }
                }
              }
        }
      }
    }
  }

注:字段名称加keyword是精准查询,模糊查询可以去掉

 查询完成后这是聚合的结构

 pom文件中引入依赖es依赖,在这里就不详细介绍了,首先在java中构建对象

//构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

 在java代码中先把要聚合的名称创建出来

根据刚刚的es代码设置java中的terms名称和field后面的名称相对应

//构建聚合名称
TermsAggregationBuilder brandAgg = AggregationBuilders.terms("brandAgg").field("brandName.keyword");
TermsAggregationBuilder typeAgg = AggregationBuilders.terms("typeAgg").field("typeTwoName.keyword");
TermsAggregationBuilder ruleAgg = AggregationBuilders.terms("ruleAgg").field("ruleName.keyword");
TermsAggregationBuilder ruleValueAgg = AggregationBuilders.terms("ruleValueAgg").field("ruleAttrValue.keyword");

这里是根据es代码中创建的层级关系来依次分配 

//根据聚合分配层级
brandAgg.subAggregation(typeAgg);
brandAgg.subAggregation(ruleAgg);
ruleAgg.subAggregation(ruleValueAgg);
//添加最外层聚合
searchSourceBuilder.aggregation(brandAgg);

创建实体类来接参

 
    
    
    private Set<BrandVO> brands = new HashSet<>();
 
    
    private Set<TypeVO> types = new HashSet<>();
 
    
    private Set<AttrVO> attrs = new HashSet<>();
 
    @Data
    public static class BrandVO {
        private Long brandId;
        private String brandName;
        private String brandImg;
    }
 
    @Data
    public static class TypeVO {
        private Long TypeId;
        private String TypeName;
    }
 
    @Data
    public static class AttrVO {
        private Long attrId;
        private String attrName;
        private List<String> attrValue;
    }
 
    @Data
    public static class BreadCrumbsVO {
        private String attrName;
        private String attrValue;
        private String link;
    }

在方法中把参数先备好

//定义参数
Set<SearchResult.BrandVO> brandVOList = new HashSet<>();
Set<SearchResult.TypeVO> typeList = new HashSet<>();
Set<SearchResult.AttrVO> attrList = new HashSet<>();

首先获取最外层的参数

有一点需要更改的 回调参数是Aggregations需要改成Terms才能调取方法

//获取聚合参数
Aggregations brandAggregations = response.getAggregations();
//获取品牌
Terms brandGations = brandAggregations.get("brandAgg");

循环获取参数添加到指定的集合当中,这个获取的就相当于是es当中key的值

for (Terms.Bucket bucket : brandGations.getBuckets()) {
//添加品牌
SearchResult.BrandVO brandVO = new SearchResult.BrandVO();
brandVO.setBrandName(bucket.getKeyAsString());
brandVOList.add(brandVO);
}

 因为我最外层只有一个分组,所以在循环中继续获取数据

这个bucket就相当于es当中获取下一层级因为我设定的brand和type都是一个层的所以都使用bucket来获取参数

get的变量就是相当于es中设置的名称然后我们拿到下面的key的值加入到我们的集合当中

注意一下这是基于上一层for循环中嵌套的

//获取类型
Aggregations aggregations = bucket.getAggregations();
Terms typeGations = aggregations.get("typeAgg");
//添加类型
for (Terms.Bucket typeGationsBucket : typeGations.getBuckets()) {
    SearchResult.TypeVO typeVO = new SearchResult.TypeVO();
    typeVO.setTypeName(typeGationsBucket.getKeyAsString());
    typeList.add(typeVO);
}

接下来就简单了,根据es当中设定好的层级关系一级一级的获取后在添加到我们设定好的集合当中

把其他的层级都一步一步获取出来

//获取spu
Terms ruleGations = aggregations.get("ruleAgg");
//添加spu
for (Terms.Bucket ruleGationsBucket : ruleGations.getBuckets()) {
    SearchResult.AttrVO attrVO = new SearchResult.AttrVO();
    attrVO.setAttrName(ruleGationsBucket.getKeyAsString());
    //获取sku
    Aggregations attrValueAggregations = ruleGationsBucket.getAggregations();
    Terms ruleValueGations = attrValueAggregations.get("ruleValueAgg");
    //添加sku
    for (Terms.Bucket ruleValueGationsBucket : ruleValueGations.getBuckets()) {
        ArrayList<String> attrValueList = new ArrayList<>();
        for (String attrValue : ruleValueGationsBucket.getKeyAsString().split(",")) {
            attrValueList.add(attrValue);
        }
        attrVO.setAttrValue(attrValueList);
    }
    attrList.add(attrVO);
}

 最后在添加到实体类的对象当中

//添加对象中数据
result.getProductInfoList().addAll(productInfoList);
result.getBrands().addAll(brandVOList);
result.getAttrs().addAll(attrList);
result.getTypes().addAll(typeList);

最后查询,就获取到了我们聚合后的值了,在根据业务进行下一步的操作

最后展示一下所有的代码

SearchResult result = new SearchResult();
try {
    //构建对象
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    //构建聚合名称
    TermsAggregationBuilder brandAgg = AggregationBuilders.terms("brandAgg").field("brandName.keyword");
    TermsAggregationBuilder typeAgg = AggregationBuilders.terms("typeAgg").field("typeTwoName.keyword");
    TermsAggregationBuilder ruleAgg = AggregationBuilders.terms("ruleAgg").field("ruleName.keyword");
    TermsAggregationBuilder ruleValueAgg = AggregationBuilders.terms("ruleValueAgg").field("ruleAttrValue.keyword");
    //根据聚合分配层级
    brandAgg.subAggregation(typeAgg);
    brandAgg.subAggregation(ruleAgg);
    ruleAgg.subAggregation(ruleValueAgg);
    //添加最外层聚合
    searchSourceBuilder.aggregation(brandAgg);
    //定义参数
    Set<SearchResult.BrandVO> brandVOList = new HashSet<>();
    Set<SearchResult.TypeVO> typeList = new HashSet<>();
    Set<SearchResult.AttrVO> attrList = new HashSet<>();
    //获取聚合参数
    Aggregations brandAggregations = response.getAggregations();
    //获取品牌
    Terms brandGations = brandAggregations.get("brandAgg");
    for (Terms.Bucket bucket : brandGations.getBuckets()) {
        //添加品牌
        SearchResult.BrandVO brandVO = new SearchResult.BrandVO();
        brandVO.setBrandName(bucket.getKeyAsString());
        brandVOList.add(brandVO);
        //获取类型
        Aggregations aggregations = bucket.getAggregations();
        Terms typeGations = aggregations.get("typeAgg");
        //添加类型
        for (Terms.Bucket typeGationsBucket : typeGations.getBuckets()) {
            SearchResult.TypeVO typeVO = new SearchResult.TypeVO();
            typeVO.setTypeName(typeGationsBucket.getKeyAsString());
            typeList.add(typeVO);
        }
        //获取spu
        Terms ruleGations = aggregations.get("ruleAgg");
        //添加spu
        for (Terms.Bucket ruleGationsBucket : ruleGations.getBuckets()) {
            SearchResult.AttrVO attrVO = new SearchResult.AttrVO();
            attrVO.setAttrName(ruleGationsBucket.getKeyAsString());
            //获取sku
            Aggregations attrValueAggregations = ruleGationsBucket.getAggregations();
            Terms ruleValueGations = attrValueAggregations.get("ruleValueAgg");
            //添加sku
            for (Terms.Bucket ruleValueGationsBucket : ruleValueGations.getBuckets()) {
                ArrayList<String> attrValueList = new ArrayList<>();
                for (String attrValue : ruleValueGationsBucket.getKeyAsString().split(",")) {
                    attrValueList.add(attrValue);
                }
                attrVO.setAttrValue(attrValueList);
            }
            attrList.add(attrVO);
        }
    }
        //添加对象中数据
        result.getProductInfoList().addAll(productInfoList);
        result.getBrands().addAll(brandVOList);
        result.getAttrs().addAll(attrList);
        result.getTypes().addAll(typeList);
    }
}catch (Exception ex){
    log.error("检索ES失败: {}", ex);
}

总结

到此这篇关于ES结合java代码聚合查询的文章就介绍到这了,更多相关ES结合java聚合查询内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯