elasticsearch移除

This commit is contained in:
gaoqr
2026-06-17 16:10:58 +08:00
parent 3b41c42951
commit 7c224fe197
47 changed files with 1087 additions and 1910 deletions
@@ -54,17 +54,6 @@
<artifactId>cf-spring-boot-starter-mybatis</artifactId>
</dependency>
<dependency>
<groupId>com.cf.imes</groupId>
<artifactId>cf-spring-boot-starter-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
</exclusion>
</exclusions>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
@@ -205,10 +194,6 @@
<groupId>com.cf.imes</groupId>
<artifactId>cf-spring-boot-starter-mq</artifactId>
</include>
<include>
<groupId>com.cf.imes</groupId>
<artifactId>cf-spring-boot-starter-elasticsearch</artifactId>
</include>
<include>
<groupId>com.cf.imes</groupId>
<artifactId>cf-module-prod-executor-api</artifactId>
@@ -256,7 +241,6 @@
cf-spring-boot-starter-security,
cf-spring-boot-starter-web,
cf-spring-boot-starter-mq,
cf-spring-boot-starter-elasticsearch,
cf-module-prod-executor-api,
cf-spring-boot-starter-biz-id,
cf-spring-boot-starter-test
@@ -1,7 +1,6 @@
package com.cf.imes.module.plan.dal.dataobject.ordermodel;
import com.alibaba.fastjson.JSONObject;
import com.cf.imes.framework.es.core.dal.ESDocument;
import com.cf.imes.module.plan.dal.elasticsearch.BasePosition;
import com.cf.imes.module.plan.dal.elasticsearch.HoleDetail;
import com.cf.imes.module.plan.dal.elasticsearch.ModelDetail;
@@ -27,7 +26,7 @@ import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderModelDO extends ESDocument {
public class OrderModelDO {
@Schema(description = "订单id")
private Long orderId;
@@ -1,7 +1,5 @@
package com.cf.imes.module.plan.dal.dataobject.ordermodel;
import com.cf.imes.framework.es.core.dal.ESDocument;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -15,7 +13,7 @@ import lombok.NoArgsConstructor;
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlateModel extends ESDocument {
public class OrderPlateModel {
@Schema(description = "订单id")
private Long orderId;
@@ -1,6 +1,5 @@
package com.cf.imes.module.plan.dal.elasticsearch;
import com.cf.imes.framework.es.core.dal.ESDocument;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -12,7 +11,7 @@ import lombok.NoArgsConstructor;
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderPartsRemark extends ESDocument {
public class OrderPartsRemark {
@Schema(description = "配件 ID")
private Long partsId;
@@ -1,220 +1,219 @@
package com.cf.imes.module.plan.service.order;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest;
import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.es.core.dal.ESDocument;
import com.cf.imes.module.plan.dal.dataobject.ordermodel.OrderModelDO;
import com.cf.imes.module.plan.dal.elasticsearch.OrderPartsRemark;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* @author Beal
* 订单入库处理器
*/
@Slf4j
@Component
public class OrderInputProcessor {
@Resource
private ElasticsearchClient elasticsearchClient;
@Resource
private ElasticsearchAsyncClient elasticsearchAsyncClient;
private Snowflake snowflake = IdUtil.getSnowflake();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String ORDER_PLATE_MODEL = "imes_order_plate_model";
public static final String ORDER_PARTS_REMARK_MODEL = "imes_order_parts_remark_model";
private static final String FIELD_ORDER_ID = "orderId";
private static final String FIELD_BATCH_ID = "batchId";
private static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 跳过 null 字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
}
/**
* 保存订单造型数据
*
* @param orderModelDOs
*/
public void batchSaveModel(List<OrderModelDO> orderModelDOs) {
try {
BulkResponse bulkResponse = bulkCreate(ORDER_PLATE_MODEL, orderModelDOs);
boolean errors = bulkResponse.errors();
if (errors) {
log.error(bulkResponse.items().toString());
throw new ServiceException(500, "未知异常");
}
} catch (IOException | ElasticsearchException e) {
log.error(e.getMessage());
throw new RuntimeException(e);
}
}
/**
* 异步保存订单造型数据
*
* @param orderModelDOs
*/
public void batchAsyncSaveModel(List<OrderModelDO> orderModelDOs) {
try {
bulkAsyncCreate(ORDER_PLATE_MODEL, orderModelDOs);
} catch (IOException | ElasticsearchException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
/**
* 按批次删除订单OrderModels
*
* @param orderId 订单id
* @param batchId 批次id
* @param index
*/
public void deleteByOrderIdBatch(Long orderId, Long batchId, String index) {
DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder();
builder.index(index);
builder.query(q -> q.bool(b -> b
.must(must -> must.term(t -> t.field(FIELD_ORDER_ID).value(orderId)))
.must(must -> must.term(t -> t.field(FIELD_BATCH_ID).value(batchId)))
));
try {
DeleteByQueryRequest deleteBuild = builder.build();
// es刚插入有可能无法马上查询删除(默认1s的可见时间),线程每1s删除一次,再删除两次
DeleteByQueryResponse response = elasticsearchClient.deleteByQuery(deleteBuild);
if (response.deleted() == 0) {
for (int i = 0; i < 2; i++) {
response = elasticsearchClient.deleteByQuery(deleteBuild);
// 删除成功,不再继续
if (response.deleted() > 0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error("====================【线程中断异常】====================");
}
}
}
log.info("Deleted {} documents with orderId {}", response.deleted(), orderId);
} catch (IOException | ElasticsearchException e) {
log.error("Error deleting documents with orderId {}: {}", orderId, e.getMessage());
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
}
/**
* 保存订单配件备注数据
*
* @param orderPartsRemarks
*/
public void batchSaveOrderPartsModel(List<OrderPartsRemark> orderPartsRemarks) {
try {
BulkResponse bulkResponse = bulkCreate(ORDER_PARTS_REMARK_MODEL, orderPartsRemarks);
boolean errors = bulkResponse.errors();
if (errors) {
log.error(bulkResponse.items().toString());
throw new ServiceException(500, "未知异常");
}
} catch (IOException | ElasticsearchException e) {
e.printStackTrace();
log.error(e.getMessage());
throw new RuntimeException(e);
}
}
public <T> BulkResponse bulkCreate(String idxName, List<? extends ESDocument> documents) throws IOException, ElasticsearchException {
BulkRequest.Builder br = new BulkRequest.Builder();
Date date = new Date();
documents.forEach(esDocument -> {
if (CharSequenceUtil.isBlank(esDocument.getId())) {
esDocument.setId(snowflake.nextIdStr());
}
esDocument.setCreateTime(simpleDateFormat.format(date));
esDocument.setUpdateTime(simpleDateFormat.format(date));
br.operations(op -> op.index(idx -> idx
.index(idxName)
.id(esDocument.getId())
.document(esDocument)));
});
return elasticsearchClient.bulk(br.build());
}
public void bulkAsyncCreate(String idxName, List<OrderModelDO> documents) throws IOException, ElasticsearchException {
BulkRequest.Builder br = new BulkRequest.Builder();
Date date = new Date();
// 深拷贝documents
List<OrderModelDO> deepCopy = objectMapper.readValue(
objectMapper.writeValueAsString(documents),
new TypeReference<>() {
}
);
deepCopy.forEach(esDocument -> {
if (CharSequenceUtil.isBlank(esDocument.getId())) {
esDocument.setId(snowflake.nextIdStr());
}
esDocument.setCreateTime(simpleDateFormat.format(date));
esDocument.setUpdateTime(simpleDateFormat.format(date));
br.operations(op -> op.index(idx -> idx
.index(idxName)
.id(esDocument.getId())
.document(esDocument)));
});
elasticsearchAsyncClient.bulk(br.build())
.whenComplete((response, exception) -> {
deepCopy.clear();
if (exception != null) {
log.error("ES bulk async failed for index: {}, size: {}, error: {}",
idxName, documents.size(), exception.getMessage(), exception);
} else if (response.errors()) {
log.warn("ES bulk async partially failed for index: {}, size: {}",
idxName, documents.size());
} else {
log.debug("ES bulk async success for index: {}, size: {}",
idxName, documents.size());
}
});
}
}
//package com.cf.imes.module.plan.service.order;
//
//import cn.hutool.core.lang.Snowflake;
//import cn.hutool.core.text.CharSequenceUtil;
//import cn.hutool.core.util.IdUtil;
//import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
//import co.elastic.clients.elasticsearch.ElasticsearchClient;
//import co.elastic.clients.elasticsearch._types.ElasticsearchException;
//import co.elastic.clients.elasticsearch.core.BulkRequest;
//import co.elastic.clients.elasticsearch.core.BulkResponse;
//import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest;
//import co.elastic.clients.elasticsearch.core.DeleteByQueryResponse;
//import com.cf.imes.framework.common.exception.ServiceException;
//import com.cf.imes.framework.es.core.dal.ESDocument;
//import com.cf.imes.module.plan.dal.dataobject.ordermodel.OrderModelDO;
//import com.cf.imes.module.plan.dal.elasticsearch.OrderPartsRemark;
//import com.fasterxml.jackson.annotation.JsonInclude;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import jakarta.annotation.Resource;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
//
///**
// * @author Beal
// * 订单入库处理器
// */
//@Slf4j
//public class OrderInputProcessor {
//
// @Resource
// private ElasticsearchClient elasticsearchClient;
//
// @Resource
// private ElasticsearchAsyncClient elasticsearchAsyncClient;
//
// private Snowflake snowflake = IdUtil.getSnowflake();
//
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
// public static final String ORDER_PLATE_MODEL = "imes_order_plate_model";
//
// public static final String ORDER_PARTS_REMARK_MODEL = "imes_order_parts_remark_model";
//
// private static final String FIELD_ORDER_ID = "orderId";
//
// private static final String FIELD_BATCH_ID = "batchId";
//
// private static ObjectMapper objectMapper = new ObjectMapper();
//
// static {
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 跳过 null 字段
// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
// objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
// }
//
// /**
// * 保存订单造型数据
// *
// * @param orderModelDOs
// */
// public void batchSaveModel(List<OrderModelDO> orderModelDOs) {
// try {
// BulkResponse bulkResponse = bulkCreate(ORDER_PLATE_MODEL, orderModelDOs);
// boolean errors = bulkResponse.errors();
// if (errors) {
// log.error(bulkResponse.items().toString());
// throw new ServiceException(500, "未知异常");
// }
// } catch (IOException | ElasticsearchException e) {
// log.error(e.getMessage());
// throw new RuntimeException(e);
// }
//
// }
//
// /**
// * 异步保存订单造型数据
// *
// * @param orderModelDOs
// */
// public void batchAsyncSaveModel(List<OrderModelDO> orderModelDOs) {
// try {
// bulkAsyncCreate(ORDER_PLATE_MODEL, orderModelDOs);
// } catch (IOException | ElasticsearchException e) {
// log.error(e.getMessage(), e);
// throw new RuntimeException(e);
// }
//
// }
//
// /**
// * 按批次删除订单OrderModels
// *
// * @param orderId 订单id
// * @param batchId 批次id
// * @param index
// */
// public void deleteByOrderIdBatch(Long orderId, Long batchId, String index) {
// DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder();
// builder.index(index);
// builder.query(q -> q.bool(b -> b
// .must(must -> must.term(t -> t.field(FIELD_ORDER_ID).value(orderId)))
// .must(must -> must.term(t -> t.field(FIELD_BATCH_ID).value(batchId)))
// ));
//
// try {
// DeleteByQueryRequest deleteBuild = builder.build();
// // es刚插入有可能无法马上查询删除(默认1s的可见时间),线程每1s删除一次,再删除两次
// DeleteByQueryResponse response = elasticsearchClient.deleteByQuery(deleteBuild);
// if (response.deleted() == 0) {
// for (int i = 0; i < 2; i++) {
// response = elasticsearchClient.deleteByQuery(deleteBuild);
// // 删除成功,不再继续
// if (response.deleted() > 0) {
// break;
// }
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// log.error("====================【线程中断异常】====================");
// }
// }
// }
// log.info("Deleted {} documents with orderId {}", response.deleted(), orderId);
// } catch (IOException | ElasticsearchException e) {
// log.error("Error deleting documents with orderId {}: {}", orderId, e.getMessage());
// throw new ServiceException(INTERNAL_SERVER_ERROR);
// }
// }
//
// /**
// * 保存订单配件备注数据
// *
// * @param orderPartsRemarks
// */
// public void batchSaveOrderPartsModel(List<OrderPartsRemark> orderPartsRemarks) {
// try {
// BulkResponse bulkResponse = bulkCreate(ORDER_PARTS_REMARK_MODEL, orderPartsRemarks);
// boolean errors = bulkResponse.errors();
// if (errors) {
// log.error(bulkResponse.items().toString());
// throw new ServiceException(500, "未知异常");
// }
// } catch (IOException | ElasticsearchException e) {
// e.printStackTrace();
// log.error(e.getMessage());
// throw new RuntimeException(e);
// }
//
// }
//
// public <T> BulkResponse bulkCreate(String idxName, List<? extends ESDocument> documents) throws IOException, ElasticsearchException {
// BulkRequest.Builder br = new BulkRequest.Builder();
// Date date = new Date();
// documents.forEach(esDocument -> {
// if (CharSequenceUtil.isBlank(esDocument.getId())) {
// esDocument.setId(snowflake.nextIdStr());
// }
// esDocument.setCreateTime(simpleDateFormat.format(date));
//
// esDocument.setUpdateTime(simpleDateFormat.format(date));
// br.operations(op -> op.index(idx -> idx
// .index(idxName)
// .id(esDocument.getId())
// .document(esDocument)));
// });
// return elasticsearchClient.bulk(br.build());
// }
//
// public void bulkAsyncCreate(String idxName, List<OrderModelDO> documents) throws IOException, ElasticsearchException {
// BulkRequest.Builder br = new BulkRequest.Builder();
// Date date = new Date();
// // 深拷贝documents
// List<OrderModelDO> deepCopy = objectMapper.readValue(
// objectMapper.writeValueAsString(documents),
// new TypeReference<>() {
// }
// );
//
// deepCopy.forEach(esDocument -> {
// if (CharSequenceUtil.isBlank(esDocument.getId())) {
// esDocument.setId(snowflake.nextIdStr());
// }
// esDocument.setCreateTime(simpleDateFormat.format(date));
//
// esDocument.setUpdateTime(simpleDateFormat.format(date));
// br.operations(op -> op.index(idx -> idx
// .index(idxName)
// .id(esDocument.getId())
// .document(esDocument)));
// });
//
// elasticsearchAsyncClient.bulk(br.build())
// .whenComplete((response, exception) -> {
// deepCopy.clear();
//
// if (exception != null) {
// log.error("ES bulk async failed for index: {}, size: {}, error: {}",
// idxName, documents.size(), exception.getMessage(), exception);
// } else if (response.errors()) {
// log.warn("ES bulk async partially failed for index: {}, size: {}",
// idxName, documents.size());
// } else {
// log.debug("ES bulk async success for index: {}, size: {}",
// idxName, documents.size());
// }
// });
// }
//}