皇冠新体育APP

IT技术之家

SpringBoot集成Elasticsearch7.4 实战(三)_吳名氏

发布了时刻:2023-08-25 16:11:07 大数据 40次 标签:皇冠新体育APP:elasticsearch java spring boot Powered by 金山文档
本篇文章主要讲的是:在Springboot环境下,管理数据...

本篇原创文章主要的讲的是:在Springboot的环境下,工作动态数据

1. 数据管理

1.1. 新增数据

1.1.1. 单实例新增

http手段发布统计信息统计,经典案例中我将统计信息统计文件格试有了规范标准,发布时中需求制定引索名,甚至JSON文件格试统计信息统计,只要尽将在事实时中真正做到基础。 故此用我交互式群体Vo来进行前边交换来的数据表格,再解析视频该Vo,得到要申诉的对象引索。
  • web后台接口方式达到
    
    /**
        * @Description 新增数据
        * @param elasticDataVo
        * @return xyz.wongs.weathertop.base.message.response.ResponseResult
        * @throws
        * @date 2019/11/20 17:10
        */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseResult add(@RequestBody ElasticDataVo elasticDataVo){
        ResponseResult response = getResponseResult();
        try {
            if(!StringUtils.isNotEmpty(elasticDataVo.getIdxName())){
                response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());
                response.setMsg("索引为空,不允许提交");
                response.setStatus(false);
                log.warn("索引为空");
                return response;
            }
            ElasticEntity elasticEntity = new ElasticEntity();
            elasticEntity.setId(elasticDataVo.getElasticEntity().getId());
            elasticEntity.setData(elasticDataVo.getElasticEntity().getData());
    
            baseElasticService.insertOrUpdateOne(elasticDataVo.getIdxName(), elasticEntity);
    
        } catch (Exception e) {
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMsg("服务忙,请稍后再试");
            response.setStatus(false);
            log.error("插入数据异常,metadataVo={},异常信息={}", elasticDataVo.toString(),e.getMessage());
        }
        return response;
    }
  • ElasticDataVo类
    
    package xyz.wongs.weathertop.palant.vo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import xyz.wongs.weathertop.base.entiy.ElasticEntity;
    
    /**
     * @ClassName ElasticDataVo
     * @Description http交互Vo对象
     * @author WCNGS@QQ.COM
     * @Github <a>//github.com/rothschil</a>
     * @date 2019/11/21 9:10
     * @Version 1.0.0
    */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class ElasticDataVo<T> {
    
        /**
         * 索引名
         */
        private String idxName;
        /**
         * 数据存储对象
         */
        private ElasticEntity elasticEntity;
    
    }
    
    
  • ElasticEntity类
    
    package xyz.wongs.weathertop.base.entiy;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import xyz.wongs.weathertop.base.persistence.mybatis.entity.BaseEntity;
    
    import java.util.Map;
    
    /**
     * @ClassName ElasticEntity
     * @Description  数据存储对象
     * @author WCNGS@QQ.COM
     * @Github <a>//github.com/rothschil</a>
     * @date 2019/11/21 9:10
     * @Version 1.0.0
    */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class ElasticEntity<T> {
    
        /**
         * 主键标识,用户ES持久化
         */
        private String id;
    
        /**
         * JSON对象,实际存储数据
         */
        private Map data;
    }
    
  • Postman提交申请

    1.1.2. 批量提交

    1.2. 条件查询

    为了能让能够万能,我的web端也包装没事层Vo,为了能让能够多媒体演示必须 我仅写有一个 match 精度一致的查询系统。
  • web后台数据接口保持
    /**
        * @Description
        * @param queryVo 查询实体对象
        * @return xyz.wongs.weathertop.base.message.response.ResponseResult
        * @throws
        * @date 2019/11/21 9:31
        */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public ResponseResult get(@RequestBody QueryVo queryVo){
    
        ResponseResult response = getResponseResult();
    
        if(!StringUtils.isNotEmpty(queryVo.getIdxName())){
            response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());
            response.setMsg("索引为空,不允许提交");
            response.setStatus(false);
            log.warn("索引为空");
            return response;
        }
    
        try {
            Class<?> clazz = ElasticUtil.getClazz(queryVo.getClassName());
            Map<String,Object> params = queryVo.getQuery().get("match");
            Set<String> keys = params.keySet();
            MatchQueryBuilder queryBuilders=null;
            for(String ke:keys){
                queryBuilders = QueryBuilders.matchQuery(ke, params.get(ke));
            }
            if(null!=queryBuilders){
                SearchSourceBuilder searchSourceBuilder = ElasticUtil.initSearchSourceBuilder(queryBuilders);
                List<?> data = baseElasticService.search(queryVo.getIdxName(),searchSourceBuilder,clazz);
                response.setData(data);
            }
        } catch (Exception e) {
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMsg("服务忙,请稍后再试");
            response.setStatus(false);
            log.error("查询数据异常,metadataVo={},异常信息={}", queryVo.toString(),e.getMessage());
        }
        return response;
    }
    
  • QueryVo类
    
    /** 查询Vo对象
     * @ClassName QueryVo
     * @Description 
     * @author WCNGS@QQ.COM
     * @Github <a>//github.com/rothschil</a>
     * @date 2019/10/25 23:16
     * @Version 1.0.0
    */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class QueryVo {
    
        /**
         * 索引名
         */
        private String idxName;
        /**
         * 需要反射的实体类型,用于对查询结果的封装
         */
        private String className;
        /**
         * 具体条件
         */
        private Map<String,Map<String,Object>> query;
    }
    
  • Postman提交成功

    1.3. 删除数据

    1.3.1. 单实例删除

    /**
        * @Description 删除
        * @param elasticDataVo
        * @return xyz.wongs.weathertop.base.message.response.ResponseResult
        * @throws
        * @date 2019/11/21 9:56
        */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseResult delete(@RequestBody ElasticDataVo elasticDataVo){
        ResponseResult response = getResponseResult();
        try {
            if(!StringUtils.isNotEmpty(elasticDataVo.getIdxName())){
                response.setCode(ResponseCode.PARAM_ERROR_CODE.getCode());
                response.setMsg("索引为空,不允许提交");
                response.setStatus(false);
                log.warn("索引为空");
                return response;
            }
            baseElasticService.deleteOne(elasticDataVo.getIdxName(),elasticDataVo.getElasticEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    
    }

    2.相关章节

    一、SpringBoot集成化Elasticsearch7.4 入门(一) 二、SpringBoot集成型Elasticsearch7.4 实际(二) 三、SpringBootibmsElasticsearch7.4 实际的(三)