woody 5 mesiacov pred
rodič
commit
75af82822e

+ 2 - 2
framework-common/src/main/java/com/chelvc/framework/common/model/Pagination.java

@@ -32,7 +32,7 @@ public class Pagination<T> implements Serializable {
     /**
      * 空分页信息
      */
-    private static final Pagination<?> EMPTY_PAGINATION = Pagination.builder().build();
+    private static final Pagination<?> EMPTY = Pagination.builder().build();
 
     /**
      * 总数量
@@ -60,7 +60,7 @@ public class Pagination<T> implements Serializable {
      */
     @SuppressWarnings("unchecked")
     public static <T> Pagination<T> empty() {
-        return (Pagination<T>) EMPTY_PAGINATION;
+        return (Pagination<T>) EMPTY;
     }
 
     /**

+ 71 - 0
framework-common/src/main/java/com/chelvc/framework/common/model/Scrolling.java

@@ -0,0 +1,71 @@
+package com.chelvc.framework.common.model;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.NonNull;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * 数据滚动列表信息
+ *
+ * @param <T> 数据类型
+ * @author Woody
+ * @date 2024/11/12
+ */
+@Data
+@SuperBuilder
+@NoArgsConstructor
+@AllArgsConstructor
+public class Scrolling<T> implements Serializable {
+    /**
+     * 空滚动信息
+     */
+    private static final Scrolling<?> EMPTY = Scrolling.builder().build();
+
+    /**
+     * 滚动标识
+     */
+    private String id;
+
+    /**
+     * 数据总量
+     */
+    @Builder.Default
+    private Long total = 0L;
+
+    /**
+     * 对象列表
+     */
+    @Builder.Default
+    private List<T> objects = Collections.emptyList();
+
+    /**
+     * 获取空滚动信息
+     *
+     * @param <T> 对象类型
+     * @return 滚动信息
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Scrolling<T> empty() {
+        return (Scrolling<T>) EMPTY;
+    }
+
+    /**
+     * 构建数据滚动信息
+     *
+     * @param id      滚动标识
+     * @param total   数据总量
+     * @param objects 数据列表
+     * @param <T>     对象类型
+     * @return 滚动信息
+     */
+    public static <T> Scrolling<T> of(@NonNull String id, long total, @NonNull List<T> objects) {
+        return new Scrolling<T>(id, total, objects);
+    }
+}

+ 12 - 0
framework-elasticsearch/src/main/java/com/chelvc/framework/elasticsearch/DefaultElasticsearchHandler.java

@@ -27,6 +27,8 @@ import co.elastic.clients.elasticsearch.core.IndexRequest;
 import co.elastic.clients.elasticsearch.core.IndexResponse;
 import co.elastic.clients.elasticsearch.core.MgetRequest;
 import co.elastic.clients.elasticsearch.core.MgetResponse;
+import co.elastic.clients.elasticsearch.core.ScrollRequest;
+import co.elastic.clients.elasticsearch.core.ScrollResponse;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.UpdateByQueryRequest;
@@ -345,4 +347,14 @@ public class DefaultElasticsearchHandler implements ElasticsearchHandler {
             throw new RuntimeException(e);
         }
     }
+
+    @Override
+    public <T> ScrollResponse<T> scroll(@NonNull Class<T> model, @NonNull String id, @NonNull String time) {
+        ScrollRequest request = new ScrollRequest.Builder().scrollId(id).scroll(s -> s.time(time)).build();
+        try {
+            return this.client.scroll(request, model);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
 }

+ 24 - 0
framework-elasticsearch/src/main/java/com/chelvc/framework/elasticsearch/ElasticsearchHandler.java

@@ -14,6 +14,7 @@ import co.elastic.clients.elasticsearch.core.DeleteResponse;
 import co.elastic.clients.elasticsearch.core.GetResponse;
 import co.elastic.clients.elasticsearch.core.IndexResponse;
 import co.elastic.clients.elasticsearch.core.MgetResponse;
+import co.elastic.clients.elasticsearch.core.ScrollResponse;
 import co.elastic.clients.elasticsearch.core.SearchRequest;
 import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.UpdateByQueryResponse;
@@ -284,4 +285,27 @@ public interface ElasticsearchHandler {
      * @return 响应结果
      */
     <T> SearchResponse<T> search(Class<T> model, SearchRequest request);
+
+    /**
+     * 滚动获取文档列表
+     *
+     * @param model 数据模型
+     * @param id    滚动标识
+     * @param <T>   数据类型
+     * @return 响应结果
+     */
+    default <T> ScrollResponse<T> scroll(Class<T> model, String id) {
+        return this.scroll(model, id, "1m");
+    }
+
+    /**
+     * 滚动获取文档列表
+     *
+     * @param model 数据模型
+     * @param id    滚动标识
+     * @param time  标识有效时间
+     * @param <T>   数据类型
+     * @return 响应结果
+     */
+    <T> ScrollResponse<T> scroll(Class<T> model, String id, String time);
 }

+ 31 - 2
framework-elasticsearch/src/main/java/com/chelvc/framework/elasticsearch/ElasticsearchUtils.java

@@ -8,9 +8,12 @@ import java.util.stream.Collectors;
 import co.elastic.clients.elasticsearch.core.CountResponse;
 import co.elastic.clients.elasticsearch.core.GetResponse;
 import co.elastic.clients.elasticsearch.core.MgetResponse;
-import co.elastic.clients.elasticsearch.core.SearchResponse;
 import co.elastic.clients.elasticsearch.core.search.Hit;
+import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
+import co.elastic.clients.elasticsearch.core.search.ResponseBody;
+import co.elastic.clients.elasticsearch.core.search.TotalHits;
 import co.elastic.clients.transport.endpoints.BooleanResponse;
+import com.chelvc.framework.common.model.Scrolling;
 import com.chelvc.framework.common.util.ObjectUtils;
 
 /**
@@ -33,6 +36,18 @@ public final class ElasticsearchUtils {
         return response == null ? 0 : response.count();
     }
 
+    /**
+     * 获取数据总量
+     *
+     * @param response 响应对象
+     * @return 数量
+     */
+    public static long total(ResponseBody<?> response) {
+        HitsMetadata<?> metadata = ObjectUtils.ifNull(response, ResponseBody::hits);
+        TotalHits total = ObjectUtils.ifNull(metadata, HitsMetadata::total);
+        return total == null ? 0 : total.value();
+    }
+
     /**
      * 获取布尔值
      *
@@ -76,10 +91,24 @@ public final class ElasticsearchUtils {
      * @param <T>      对象类型
      * @return 对象列表
      */
-    public static <T> List<T> sources(SearchResponse<T> response) {
+    public static <T> List<T> sources(ResponseBody<T> response) {
         if (response == null || response.hits() == null || ObjectUtils.isEmpty(response.hits().hits())) {
             return Collections.emptyList();
         }
         return response.hits().hits().stream().map(Hit::source).filter(Objects::nonNull).collect(Collectors.toList());
     }
+
+    /**
+     * 获取数据滚动信息
+     *
+     * @param response 响应对象
+     * @param <T>      对象类型
+     * @return 滚动信息
+     */
+    public static <T> Scrolling<T> scrolling(ResponseBody<T> response) {
+        if (response == null) {
+            return Scrolling.empty();
+        }
+        return Scrolling.of(response.scrollId(), total(response), sources(response));
+    }
 }