|
@@ -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));
|
|
|
+ }
|
|
|
}
|