Commit 1cd95281 by 李小惠

Merge branch 'develop' of http://gitlab.sothing.top/843502640/jyzb_platformV2 into develop-lxh

parents 4fb5e13c ef262676
...@@ -18,6 +18,7 @@ public class OrgDto { ...@@ -18,6 +18,7 @@ public class OrgDto {
private Boolean isLeaf; private Boolean isLeaf;
private Integer statusFlag; private Integer statusFlag;
private List<OrgDto> children = new ArrayList<>(); private List<OrgDto> children = new ArrayList<>();
public void addChild(OrgDto child) { public void addChild(OrgDto child) {
children.add(child); children.add(child);
} }
......
...@@ -16,5 +16,6 @@ public class QueryEquipmentTypeReq { ...@@ -16,5 +16,6 @@ public class QueryEquipmentTypeReq {
private String code; private String code;
private String parentId; private String parentId;
private Integer type; private Integer type;
private String typeName;
} }
...@@ -57,7 +57,7 @@ public class PubOrgController { ...@@ -57,7 +57,7 @@ public class PubOrgController {
@PostMapping("/GetCurrentESList") @PostMapping("/GetCurrentESList")
@ApiOperation("根据当前组织机构列表ES查询") @ApiOperation("根据当前组织机构列表ES查询")
public ApiRes<List<OrgDto>> GetCurrentESList(@RequestBody QueryOrgReq req) throws IOException { public ApiRes<List<OrgDto>> GetCurrentESList(@RequestBody QueryOrgReq req) throws IOException {
return ApiRes.success(pubOrgService.getOrgHierarchyByEs(req)); return ApiRes.success(pubOrgService.getOrgTreeByEs(req));
} }
@PostMapping("/ChangeOrgState") @PostMapping("/ChangeOrgState")
@ApiOperation("改变组织机构状态信息") @ApiOperation("改变组织机构状态信息")
......
...@@ -3,11 +3,14 @@ package com.junmp.jyzb.service; ...@@ -3,11 +3,14 @@ package com.junmp.jyzb.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto;
import com.junmp.jyzb.api.bean.dto.OrgDto;
import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq; import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq;
import com.junmp.jyzb.api.bean.query.QueryOrgReq;
import com.junmp.jyzb.api.bean.req.UpdateEquipmentTypeReq; import com.junmp.jyzb.api.bean.req.UpdateEquipmentTypeReq;
import com.junmp.jyzb.entity.EquipmentType; import com.junmp.jyzb.entity.EquipmentType;
import com.junmp.jyzb.utils.ResponseResult; import com.junmp.jyzb.utils.ResponseResult;
import java.io.IOException;
import java.util.List; import java.util.List;
public interface EquipmentTypeService extends IService<EquipmentType> { public interface EquipmentTypeService extends IService<EquipmentType> {
...@@ -21,5 +24,8 @@ public interface EquipmentTypeService extends IService<EquipmentType> { ...@@ -21,5 +24,8 @@ public interface EquipmentTypeService extends IService<EquipmentType> {
EquipmentTypeDto getEquipment(QueryEquipmentTypeReq req ); EquipmentTypeDto getEquipment(QueryEquipmentTypeReq req );
List<EquipmentTypeDto> getLowType(QueryEquipmentTypeReq orgId); List<EquipmentTypeDto> getLowType(QueryEquipmentTypeReq orgId);
ResponseResult setTypeParentIds(); ResponseResult setTypeParentIds();
List<EquipmentTypeDto> getTypeTreeByEs(QueryOrgReq req) throws IOException;
List<EquipmentTreeDto> GetTypeTree(); List<EquipmentTreeDto> GetTypeTree();
} }
...@@ -29,7 +29,7 @@ public interface PubOrgService extends IService<PubOrg> { ...@@ -29,7 +29,7 @@ public interface PubOrgService extends IService<PubOrg> {
OrgDto getOrgHierarchy(QueryOrgReq req); OrgDto getOrgHierarchy(QueryOrgReq req);
List<OrgDto> getOrgHierarchyByEs(QueryOrgReq req) throws IOException; List<OrgDto> getOrgTreeByEs(QueryOrgReq req) throws IOException;
List<OrgDto> getLowOrg(QueryOrgReq req); List<OrgDto> getLowOrg(QueryOrgReq req);
Boolean ChangeState(UpdateOrgReq req); Boolean ChangeState(UpdateOrgReq req);
......
...@@ -7,7 +7,9 @@ import com.junmp.jyzb.Repository.BlogRepository; ...@@ -7,7 +7,9 @@ import com.junmp.jyzb.Repository.BlogRepository;
import com.junmp.jyzb.Repository.TypeRepository; import com.junmp.jyzb.Repository.TypeRepository;
import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTypeDto;
import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto; import com.junmp.jyzb.api.bean.dto.EquipmentTreeDto;
import com.junmp.jyzb.api.bean.dto.OrgDto;
import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq; import com.junmp.jyzb.api.bean.query.QueryEquipmentTypeReq;
import com.junmp.jyzb.api.bean.query.QueryOrgReq;
import com.junmp.jyzb.api.bean.req.UpdateEquipmentTypeReq; import com.junmp.jyzb.api.bean.req.UpdateEquipmentTypeReq;
import com.junmp.jyzb.api.exception.JYZBAppException; import com.junmp.jyzb.api.exception.JYZBAppException;
import com.junmp.jyzb.api.exception.enums.EquipmentTypeExceptionEnum; import com.junmp.jyzb.api.exception.enums.EquipmentTypeExceptionEnum;
...@@ -18,12 +20,14 @@ import com.junmp.jyzb.service.SupplierTypeService; ...@@ -18,12 +20,14 @@ import com.junmp.jyzb.service.SupplierTypeService;
import com.junmp.jyzb.utils.*; import com.junmp.jyzb.utils.*;
import com.junmp.v2.common.exception.base.ServiceException; import com.junmp.v2.common.exception.base.ServiceException;
import com.junmp.v2.common.util.BeanPlusUtil; import com.junmp.v2.common.util.BeanPlusUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import com.junmp.jyzb.mapper.EquipmentTypeMapper; import com.junmp.jyzb.mapper.EquipmentTypeMapper;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -32,8 +36,7 @@ import java.util.stream.Collectors; ...@@ -32,8 +36,7 @@ import java.util.stream.Collectors;
public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, EquipmentType> implements EquipmentTypeService { public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, EquipmentType> implements EquipmentTypeService {
@Resource @Resource
private RedisUtils redisUtils; private RestHighLevelClient client;
@Resource @Resource
private EquipmentTypeMapper equipmentTypeMapper; private EquipmentTypeMapper equipmentTypeMapper;
@Autowired @Autowired
...@@ -268,6 +271,36 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E ...@@ -268,6 +271,36 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E
return new ResponseResult(HttpStatus.SUCCESS, ReturnMsg.PASS); return new ResponseResult(HttpStatus.SUCCESS, ReturnMsg.PASS);
} }
@Override
public List<EquipmentTypeDto> getTypeTreeByEs(QueryOrgReq req) throws IOException {
return null;
}
public List<EquipmentTypeDto> getTypeTreeByEs(QueryEquipmentTypeReq req) throws IOException {
// LambdaQueryWrapper<EquipmentType> wp = this.createWrapper(req);
// ElasticsearchUtil esUtil=new ElasticsearchUtil<>(client);
// // 1. 从 Elasticsearch 中搜索组织机构数据
// List<EquipmentType> searchResults = esUtil.searchEntities("name","type_pinyin",0, 20, req.getTypeName(),EquipmentType.class);
// // 2. 从数据库中获取完整的组织机构数据
// List<EquipmentType> fullOrgData = this.list(wp);
//
// // 3. 构建树状结构
// List<OrgDto> tree = new ArrayList<>();
// for (PubOrg searchResult : searchResults) {
// // 在完整数据中查找匹配的组织机构
// PubOrg fullOrg = findOrgById(fullOrgData, searchResult.getOrgId());
//
// // 将完整数据转换为前端需要的 DTO
// OrgDto orgDto = convertToDto(fullOrg, fullOrgData);
//
// // TODO: 根据需求决定是否加入树状结构
// tree.add(orgDto);
// }
// return tree;
return null;
}
//获取整棵type树 //获取整棵type树
public List<EquipmentTreeDto> GetTypeTree() { public List<EquipmentTreeDto> GetTypeTree() {
List<EquipmentType> list = list(new LambdaQueryWrapper<EquipmentType>() List<EquipmentType> list = list(new LambdaQueryWrapper<EquipmentType>()
......
...@@ -382,6 +382,7 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -382,6 +382,7 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
* @throws IOException * @throws IOException
*/ */
public List<PubOrg> searchOrg(Integer pageNum, Integer pageSize, String keyword) throws IOException { public List<PubOrg> searchOrg(Integer pageNum, Integer pageSize, String keyword) throws IOException {
if (pageNum < 0) pageNum = 0; if (pageNum < 0) pageNum = 0;
SearchRequest request = new SearchRequest("org_pinyin"); SearchRequest request = new SearchRequest("org_pinyin");
...@@ -393,11 +394,11 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -393,11 +394,11 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
// 添加 MatchQueryBuilder 作为 must 条件 // 添加 MatchQueryBuilder 作为 must 条件
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("orgName", keyword).operator(Operator.AND); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("orgName", keyword).operator(Operator.AND);
MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("orgName.pinyin", keyword).operator(Operator.AND); // MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("orgName.pinyin", keyword).operator(Operator.AND);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(matchQueryBuilder); boolQueryBuilder.should(matchQueryBuilder);
boolQueryBuilder.should(matchQueryBuilder2); // boolQueryBuilder.should(matchQueryBuilder2);
// 添加过滤条件,只获取 delFlag 为 1 的数据 // 添加过滤条件,只获取 delFlag 为 1 的数据
builder.postFilter(QueryBuilders.termQuery("delFlag", 1)); builder.postFilter(QueryBuilders.termQuery("delFlag", 1));
...@@ -418,6 +419,84 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -418,6 +419,84 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
return pubOrgList; return pubOrgList;
} }
public List<OrgDto> searchAndBuildTree(Integer pageNum, Integer pageSize, String keyword) throws IOException {
// 先进行搜索,获取前20条数据
QueryOrgReq req=new QueryOrgReq();
List<PubOrg> searchResults = searchOrg(pageNum, pageSize, keyword);
req.setDelFlag(1);
LambdaQueryWrapper<PubOrg> wp = this.createWrapper(req);
// 获取所有组织机构数据
List<PubOrg> allOrgs = this.list(wp);
List<OrgDto> treeResult= buildTree(searchResults,allOrgs);
return treeResult;
}
public List<OrgDto> buildTree(List<PubOrg> searchResults, List<PubOrg> allOrgs) {
Map<Long, OrgDto> orgDtoMap = new HashMap<>();
// 将所有组织机构转换为OrgDto并放入map中
for (PubOrg org : allOrgs) {
OrgDto orgDto = new OrgDto();
// 这里根据你的实际字段进行映射
orgDto.setOrgId(org.getOrgId());
orgDto.setOrgName(org.getOrgName());
orgDto.setOrgCode(org.getOrgCode());
orgDto.setIsDepartment(org.getIsDepartment());
orgDto.setOrgParentId(org.getOrgParentId());
orgDto.setAreaName(org.getAreaName());
orgDto.setLevelFlag(String.valueOf(org.getLevelFlag()));
orgDto.setStatusFlag(org.getStatusFlag());
orgDto.setDName(org.getDName());
// 其他字段的映射...
// 将OrgDto放入map中,以orgId为键
orgDtoMap.put(org.getOrgId(), orgDto);
}
List<OrgDto> tree = new ArrayList<>();
// 遍历所有搜索到的组织机构数据,构建树
for (PubOrg searchResult : searchResults) {
OrgDto orgDto = orgDtoMap.get(searchResult.getOrgId());
// 如果找到对应的OrgDto
if (orgDto != null && !tree.contains(orgDto)) {
// 从当前节点开始,依次向上查找上级节点,直到最顶层
Long parentId = orgDto.getOrgParentId();
// 将当前节点及其相关节点加入树中
while (parentId != null) {
OrgDto parentDto = orgDtoMap.get(parentId);
if (parentDto != null) {
// 将当前节点添加到上级节点的children中,确保不重复添加
if (!parentDto.getChildren().contains(orgDto)) {
parentDto.getChildren().add(orgDto);
}
// 如果上级节点是最顶层,将上级节点添加到树中,确保不重复添加
if (parentDto.getOrgParentId() == -1 && !tree.contains(parentDto)) {
tree.add(parentDto);
}
// 将上级节点设为当前节点,继续向上查找
orgDto = parentDto;
parentId = parentDto.getOrgParentId();
} else {
parentId = null; // 上级节点不存在,跳出循环
}
}
}
}
return tree;
}
// 从 Map 中构建 PubOrg 对象的方法 // 从 Map 中构建 PubOrg 对象的方法
public static PubOrg fromMap(Map<String, Object> map) { public static PubOrg fromMap(Map<String, Object> map) {
PubOrg pubOrg = new PubOrg(); PubOrg pubOrg = new PubOrg();
...@@ -428,37 +507,12 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem ...@@ -428,37 +507,12 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
} }
///通过ES获取数据 ///通过ES获取数据
@Override @Override
public List<OrgDto> getOrgHierarchyByEs(QueryOrgReq req) throws IOException { public List<OrgDto> getOrgTreeByEs(QueryOrgReq req) throws IOException {
req.setDelFlag(1);
LambdaQueryWrapper<PubOrg> wp = this.createWrapper(req);
ElasticsearchUtil esUtil = new ElasticsearchUtil(client);
// 1. 从 Elasticsearch 中搜索组织机构数据
List<PubOrg> searchResults = searchOrg(0, 20, req.getOrgName());
// 2. 从数据库中获取完整的组织机构数据
List<PubOrg> fullOrgData = this.list(wp);
// 3. 构建树状结构
List<OrgDto> tree = new ArrayList<>();
for (PubOrg searchResult : searchResults) {
// 在完整数据中查找匹配的组织机构
PubOrg fullOrg = findOrgById(fullOrgData, searchResult.getOrgId());
// 将完整数据转换为前端需要的 DTO
OrgDto orgDto = convertToDto(fullOrg, fullOrgData);
// TODO: 根据需求决定是否加入树状结构 List<OrgDto> treeDto= searchAndBuildTree(0, 10, req.getOrgName());
tree.add(orgDto); return treeDto;
}
return tree;
}
private PubOrg findOrgById(List<PubOrg> fullOrgData, Long orgId) {
for (PubOrg org : fullOrgData) {
if (org.getOrgId().equals(orgId)) {
return org;
}
}
return null; // 如果找不到匹配的组织机构,可以根据实际需求进行处理
} }
private OrgDto convertToDto(PubOrg org, List<PubOrg> allOrgs) { private OrgDto convertToDto(PubOrg org, List<PubOrg> allOrgs) {
OrgDto orgDto = new OrgDto(); OrgDto orgDto = new OrgDto();
// 这里根据你的实际字段进行映射 // 这里根据你的实际字段进行映射
......
package com.junmp.jyzb.utils; package com.junmp.jyzb.utils;
import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor; import org.elasticsearch.action.search.SearchRequest;
import lombok.Data; import org.elasticsearch.action.search.SearchResponse;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.*;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.builder.SearchSourceBuilder;
import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
@Slf4j public class ElasticsearchUtil<T> {
@Data private final RestHighLevelClient client;
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class ElasticsearchUtil {
@Resource
private RestHighLevelClient client;
/**
* 测试删除索引库
*/
public boolean deleteIndex(String name){
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(name);
boolean exists =false;
try {
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
if (delete.isAcknowledged()){
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return exists;
}
/**
* 测试添加日志记录文档
*/
public boolean createDocument(String indexName,JSONObject jsonObject){
//组装数据
//创建请求
IndexRequest request = new IndexRequest(indexName);
//设置规则
request.timeout(TimeValue.timeValueSeconds(1));
//将数据放入请求对象里面
request.source(jsonObject);
try {
//发送请求 获取响应
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
RestStatus status = response.status();
if (status.equals(RestStatus.OK)){
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/** public ElasticsearchUtil(RestHighLevelClient client) {
* 判断文档是否存在 this.client = client;
* @param indexName 索引名称
* @param id 文档id
* @return
*/
public boolean documentIsExists(String indexName,String id){
GetRequest request = new GetRequest(indexName,id);
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = false;
try {
exists = client.exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.warn("判断文档是否存在异常: {}",e.getMessage());
e.printStackTrace();
}
return exists;
} }
public List<T> searchEntities(String typeName,String index, Integer pageNum, Integer pageSize, String keyword, Class<T> entityClass) throws IOException {
if (pageNum < 0) pageNum = 0;
/** SearchRequest request = new SearchRequest(index);
* 根据id获取文档 SearchSourceBuilder builder = new SearchSourceBuilder();
* @param indexName 索引名称
* @param id 文档id
* @return
*/
public Map<String, Object> getDocument(String indexName, String id){
GetRequest request = new GetRequest(indexName,id);
try {
GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
return documentFields.getSource();
} catch (IOException e) {
log.warn("获取文档失败: {}",e.getMessage());
e.printStackTrace();
}
return null;
}
/** builder.from(pageNum);
* 根据文档id修改文档信息 builder.size(pageSize);
* @param indexName 索引名称
* @param id 文档id MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(typeName, keyword).operator(Operator.AND);
*/ MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery(typeName+".pinyin", keyword).operator(Operator.AND);
public boolean updateDocument(String indexName,String id,JSONObject jsonObject){
UpdateRequest request = new UpdateRequest(indexName,id); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
try { boolQueryBuilder.should(matchQueryBuilder);
request.timeout(TimeValue.timeValueSeconds(1)); boolQueryBuilder.should(matchQueryBuilder2);
request.doc(jsonObject,XContentType.JSON); if (typeName.equals("orgName"))
UpdateResponse update = client.update(request, RequestOptions.DEFAULT); {
if (update.status().equals(RestStatus.OK)){ builder.postFilter(new TermQueryBuilder("delFlag", 1));
return true;
}
} catch (IOException e) {
log.warn("修改文档失败: {}",e.getMessage());
e.printStackTrace();
} }
return false;
}
builder.query(boolQueryBuilder);
builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(builder);
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
/** List<T> entityList = new ArrayList<>();
* 删除文档 for (SearchHit documentFields : searchResponse.getHits().getHits()) {
* @param indexName 索引名称 Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
* @param id 文档id T entity = fromMap(sourceAsMap, entityClass);
*/
public boolean deleteDocument(String indexName,String id){ entityList.add(entity);
DeleteRequest request = new DeleteRequest(indexName,id);
try {
DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
if (delete.status().equals(RestStatus.OK)){
return true;
}
}catch (IOException e){
log.warn("修改文档失败: {}",e.getMessage());
e.printStackTrace();
} }
return false;
return entityList;
} }
/** private T fromMap(Map<String, Object> sourceAsMap, Class<T> entityClass) {
* 批量插入文档数据 ObjectMapper objectMapper = new ObjectMapper(); // 使用 Jackson 库进行转换
* @param indexName 索引 return objectMapper.convertValue(sourceAsMap, entityClass);
* @param list 批量文档
* @return
*/
public boolean bulkDocument(String indexName, List<JSONObject> list){
BulkRequest bulkRequest = new BulkRequest(indexName);
bulkRequest.timeout(TimeValue.timeValueSeconds(2));
try {
list.forEach(item->bulkRequest.add(new IndexRequest().source(item, XContentType.JSON)));
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
if (bulk.status().equals(RestStatus.OK)){
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论