mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
未排单管理-创建排单列表es查询优化
This commit is contained in:
+17
-10
@@ -9,6 +9,8 @@ import co.elastic.clients.elasticsearch._types.ElasticsearchException;
|
||||
import co.elastic.clients.elasticsearch._types.FieldValue;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
||||
import co.elastic.clients.elasticsearch.core.CountRequest;
|
||||
import co.elastic.clients.elasticsearch.core.CountResponse;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest;
|
||||
@@ -825,9 +827,8 @@ public class OptimizePlanServiceImpl implements OptimizePlanService {
|
||||
List<OrderModelDO> results = new ArrayList<>();
|
||||
try {
|
||||
// 查询总数
|
||||
SearchRequest totalRequest = new SearchRequest.Builder()
|
||||
CountRequest countRequest = new CountRequest.Builder()
|
||||
.index(ORDER_PLATE_MODEL.getIndex())
|
||||
.size(0) // 不返回文档,仅统计总数
|
||||
.query(q -> q.bool(b -> b
|
||||
.filter(f -> f.terms(t -> t
|
||||
.field("orderId")
|
||||
@@ -835,21 +836,27 @@ public class OptimizePlanServiceImpl implements OptimizePlanService {
|
||||
))
|
||||
))
|
||||
.build();
|
||||
SearchResponse<Void> totalResponse = elasticsearchClient.search(totalRequest, Void.class);
|
||||
long total = totalResponse.hits().total().value();
|
||||
CountResponse totalResponse = elasticsearchClient.count(countRequest);
|
||||
long total = totalResponse.count();
|
||||
|
||||
// 分页查询
|
||||
int pageNum = PageUtil.totalPage(total, 5000);
|
||||
int pageNum = PageUtil.totalPage(total, 2000);
|
||||
|
||||
FieldValue searchAfterSortId = FieldValue.of(1);
|
||||
for (int page = 1; page <= pageNum; page++) {
|
||||
SearchRequest.Builder searchBuilder = new SearchRequest.Builder()
|
||||
.index(ORDER_PLATE_MODEL.getIndex())
|
||||
.size(5000)
|
||||
.sort(s -> s.field(f -> f.field("sortId").order(SortOrder.Asc)))
|
||||
.size(2000)
|
||||
.sort(s -> s.field(f -> f.field("id.keyword").order(SortOrder.Asc)))
|
||||
.query(q -> q.bool(b -> b
|
||||
.filter(f -> f.terms(t -> t.field("orderId").terms(e -> e.value(orderIds.stream().map(FieldValue::of).toList()))))
|
||||
));
|
||||
))
|
||||
.source(s -> s
|
||||
.filter(f -> f
|
||||
.includes("holeDetail", "sideHoleDetail", "contourDetail", "sideDetail", "id")
|
||||
)
|
||||
)
|
||||
.trackTotalHits(t -> t.enabled(false));
|
||||
|
||||
searchBuilder.searchAfter(searchAfterSortId);
|
||||
|
||||
@@ -861,11 +868,11 @@ public class OptimizePlanServiceImpl implements OptimizePlanService {
|
||||
results.add(hit.source());
|
||||
}
|
||||
|
||||
// 取最后一个命中文档的 sortId 作为下一页的 search_after 值
|
||||
// 取最后一个命中文档的 id 作为下一页的 search_after 值
|
||||
if (!hits.isEmpty()) {
|
||||
OrderModelDO lastDoc = hits.get(hits.size() - 1).source();
|
||||
if (lastDoc != null) {
|
||||
searchAfterSortId = FieldValue.of(lastDoc.getSortId());
|
||||
searchAfterSortId = FieldValue.of(lastDoc.getId());
|
||||
} else {
|
||||
// 没有更多数据了
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user