@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();
}
}
ElasticSearch添加索引创建文档添加数据
最新推荐文章于 2024-07-30 11:41:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)