ElasticSearch添加索引创建文档添加数据

 
    @GetMapping("/addDocDetectionManagement/{institutionId}")
    public Boolean addDocDetectionManagement(@PathVariable("institutionId") Long institutionId){
        List<TestOrderVo> testOrderVos = enterpriseController.listByInstitutionId(institutionId);
        System.out.println("testOrderVos = " + testOrderVos);

        //对象集合转换
        List<TestOrderModel> testOrderModels = testOrderVos.stream().map(testOrder -> {
            TestOrderModel testOrderModel = new TestOrderModel();
            BeanUtils.copyProperties(testOrder,testOrderModel);
            return testOrderModel;
        }).collect(Collectors.toList());
        System.out.println("testOrderModels = " + testOrderModels);
        AtomicBoolean success = new AtomicBoolean(false);
        Boolean hasFailures;
        //检查索引是否存在
        Boolean indexExist = elasticService.isIndexExist(ElasticConstant.Test_Order);

        if(!indexExist){
            //创建索引
            Map<String, Object> acode = new HashMap<>();
            acode.put("type", "text");
            Map<String, Object> tester = new HashMap<>();
            tester.put("type", "keyword");
            Map<String, Object> orderNo = new HashMap<>();
            orderNo.put("type", "text");
            Map<String, Object> deviceId = new HashMap<>();
            deviceId.put("type", "text");
            Map<String, Object> testResult = new HashMap<>();
            testResult.put("type", "text");
            Map<String, Object> updateTime = new HashMap<>();
            updateTime.put("type", "text");
            Map<String, Object> projectName = new HashMap<>();
            projectName.put("type", "text");
            Map<String, Object> properties = new HashMap<>();
            properties.put("acode", acode);
            properties.put("name", tester);
            properties.put("orderNo", orderNo);
            properties.put("deviceId", deviceId);
            properties.put("testResult", testResult);
            properties.put("updateTime", updateTime);
            properties.put("projectName", projectName);
            Map<String, Object> mapping = new HashMap<>();
            mapping.put("properties", properties);
            elasticService.createIndex(ElasticConstant.Test_Order, mapping);
            //插入到es
            hasFailures = elasticService.batchInsert(ElasticConstant.Test_Order, testOrderModels);
            return !hasFailures;
        }else{
            //根据已有索引,添加文档
            testOrderModels.forEach(testOrder -> {
                Map<String, Object> sourceMap = BeanUtil.beanToMap(testOrder);
                RestStatus restStatus = elasticService.addDocument(ElasticConstant.Test_Order, String.valueOf(testOrder.getId()), sourceMap);
                success.set(restStatus.getStatus() == 201);
            });
            return success.get();
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值