Jelajahi Sumber

网点师傅下载调整ES的处理

FengChaoYu 5 hari lalu
induk
melakukan
29537880a2

+ 104 - 0
mall-server-api/src/main/java/com/gree/mall/manager/logic/es/EsLogic.java

@@ -10,12 +10,16 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
 import org.springframework.data.elasticsearch.core.SearchHit;
+import org.springframework.data.elasticsearch.core.SearchScrollHits;
+import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
+import org.springframework.data.elasticsearch.core.query.Query;
 import org.springframework.stereotype.Service;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -132,6 +136,106 @@ public class EsLogic {
         return titles;
     }
 
+//    public List<CommonListES> list(List<String> websitNumbers, String workerNumber, String comId) {
+//        // 1. 标题查询(type=1)
+//        BoolQueryBuilder titleQueryBuilder = QueryBuilders.boolQuery()
+//                .must(QueryBuilders.termQuery("type", "1"));
+//
+//        if (StringUtils.isNotEmpty(comId)) {
+//            titleQueryBuilder.must(QueryBuilders.termQuery("comListId", comId));
+//        }
+//
+//        NativeSearchQuery titleBuild = new NativeSearchQueryBuilder()
+//                .withQuery(titleQueryBuilder)
+//                .build();
+//
+//        // 2. 获取标题数据
+//        List<SearchHit<CommonListES>> searchHits1 = elasticsearchRestTemplate.search(titleBuild, CommonListES.class).getSearchHits();
+//        List<CommonListES> titles = searchHits1.stream().map(SearchHit<CommonListES>::getContent).collect(Collectors.toList());
+//
+//        // 3. 使用Scroll API获取内容数据(type=2)
+//        List<CommonListES> datas = scrollQueryData(websitNumbers, workerNumber, comId);
+//
+//        // 4. 合并结果
+//        titles.addAll(datas);
+//        return titles;
+//    }
+
+    private List<CommonListES> scrollQueryData(List<String> websitNumbers,
+                                               String workerNumber,
+                                               String comId) {
+        // 构建内容查询(type=2)
+        BoolQueryBuilder dataQuery = QueryBuilders.boolQuery()
+                .must(QueryBuilders.termQuery("type", "2"));
+
+        if (StringUtils.isNotEmpty(comId)) {
+            dataQuery.must(QueryBuilders.termQuery("comListId", comId));
+        }
+
+        if (StringUtils.isNotEmpty(workerNumber)) {
+            dataQuery.must(QueryBuilders.matchPhraseQuery("workerNumber", workerNumber));
+        }
 
+        if (CollectionUtils.isNotEmpty(websitNumbers)) {
+            dataQuery.filter(QueryBuilders.termsQuery("websitNumber", websitNumbers));
+        }
 
+        // 创建Scroll查询
+        Query query = new NativeSearchQueryBuilder()
+                .withQuery(dataQuery)
+                .withPageable(PageRequest.of(0, 5000)) // 批次大小设为500
+                .build();
+
+        List<CommonListES> results = new ArrayList<>();
+        SearchScrollHits<CommonListES> scrollHits = null;
+        String scrollId = null;
+        final long SCROLL_TIMEOUT = 60_000; // 60秒超时
+        // 获取索引坐标(需要根据您的实际索引名称替换)
+        IndexCoordinates index = IndexCoordinates.of("common_list_v2");
+
+        try {
+            // 启动滚动查询
+            scrollHits = elasticsearchRestTemplate.searchScrollStart(
+                    SCROLL_TIMEOUT,
+                    query,
+                    CommonListES.class,
+                    index
+            );
+            scrollId = scrollHits.getScrollId();
+
+            // 处理第一批数据
+            addSearchHits(results, scrollHits);
+
+            // 循环处理所有结果
+            while (scrollHits.hasSearchHits() && !scrollHits.getSearchHits().isEmpty()) {
+                // 继续获取下一批
+                scrollHits = elasticsearchRestTemplate.searchScrollContinue(
+                        scrollId,
+                        SCROLL_TIMEOUT,
+                        CommonListES.class,
+                        index
+                );
+                scrollId = scrollHits.getScrollId(); // 更新scrollId
+
+                // 添加到结果集
+                addSearchHits(results, scrollHits);
+            }
+        } finally {
+            // 确保滚动资源被释放
+            if (scrollId != null) {
+                elasticsearchRestTemplate.searchScrollClear(Collections.singletonList(scrollId));
+            }
+        }
+        return results;
+    }
+
+    private void addSearchHits(List<CommonListES> resultList, SearchScrollHits<CommonListES> scrollHits) {
+        if (scrollHits != null && scrollHits.hasSearchHits()) {
+            resultList.addAll(
+                    scrollHits.getSearchHits().stream()
+                            .map(SearchHit::getContent)
+                            .collect(Collectors.toList())
+            );
+        }
+    }
 }