From a86979b89aa8ee7045c39e71ad2d24e4ea8d02ff Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Wed, 17 Jun 2026 16:11:13 +0800 Subject: [PATCH] =?UTF-8?q?elasticsearch=E7=A7=BB=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/util/elasticsearch/EsUtils.java | 1284 ++++++++--------- 1 file changed, 641 insertions(+), 643 deletions(-) diff --git a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/util/elasticsearch/EsUtils.java b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/util/elasticsearch/EsUtils.java index 5b30f1983..cdf0f7516 100644 --- a/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/util/elasticsearch/EsUtils.java +++ b/cf-module-prod-executor/cf-module-prod-executor-biz/src/main/java/com/cf/imes/module/executor/util/elasticsearch/EsUtils.java @@ -1,643 +1,641 @@ -package com.cf.imes.module.executor.util.elasticsearch; - - -import cn.hutool.core.collection.CollUtil; -import co.elastic.clients.elasticsearch.ElasticsearchClient; -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.Time; -import co.elastic.clients.elasticsearch._types.query_dsl.Query; -import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest; -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.search.Hit; -import co.elastic.clients.elasticsearch.indices.RefreshRequest; -import co.elastic.clients.json.JsonData; -import co.elastic.clients.json.JsonpMappingException; -import com.cf.imes.framework.common.exception.ServiceException; -import com.cf.imes.framework.common.util.json.JsonUtils; -import com.cf.imes.framework.es.core.dal.ESDocument; -import com.cf.imes.framework.es.core.service.ESDocumentService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; - -import jakarta.annotation.Resource; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_DATA_ERROR; -import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.ES_DATA_ERROR; -import static com.cf.imes.module.executor.enums.ErrorCodeConstants.ORDER_DATA_ERROR; - -/** - * @author ES 工具类 - */ -@Component -@Slf4j -public class EsUtils { - - private final ElasticsearchClient elasticsearchClient; - - private static final String DOCUMENT_DELETE_ERROR = "ES文档删除失败:{},排单号/订单号为:{}"; - - - - @Resource - private ESDocumentService esDocumentService; - - - public EsUtils(ElasticsearchClient elasticsearchClient) { - this.elasticsearchClient = elasticsearchClient; - } - - - /** - * ES 文档查询 - * @param filedName 需要查询的文档字段 - * @param value 需要查询的值 - * @param index 索引 - * @param targetClass 索引文档的实体类 - */ - public List getEsDocument(String filedName, Long value, String index, Class targetClass) { - - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - - builder.query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - public List getEsDocument(String filedName, Long value,Integer size, String index, Class targetClass) { - - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - builder.size(size); - - builder.query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - } catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - /** - * ES 文档查询 - * @param filedName 需要查询的文档字段 - * @param values 需要查询的值(集合类型) - * @param index 索引对象 - */ - public List getEsDocument(String filedName, List values, String index, Class targetClass) { - - List fieldValues = values.stream().map(FieldValue::of).toList(); - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - - builder.query(q -> q.bool(b->b.filter(f->f.terms(t->t.field(filedName).terms(te->te.value(fieldValues)))))); - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - public List getEsDocument(String filedName, List values,Integer size, String index, Class targetClass) { - - List fieldValues = values.stream().map(FieldValue::of).toList(); - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - builder.size(size); - - builder.query(q-> q.bool(b-> b.filter(f-> f.terms(t-> t.field(filedName).terms(v->v.value(fieldValues)))))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - - public List getEsDocument(String filedName1,String filedName2, Long value1,Long value2, String index, Class targetClass) { - - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - - - builder.query(q->q.bool(b->b.filter(f->{ - f.term(t->t.field(filedName1).value(value1)); - f.term(t->t.field(filedName2).value(value2)); - return f; - }))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - - public List getEsDocument(String filedName1,String filedName2, Long value1,Long value2,Integer size, String index, Class targetClass) { - - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - builder.size(size); - - builder.query(q->q.bool(b->b.filter(f->{ - f.term(t->t.field(filedName1).value(value1)); - f.term(t->t.field(filedName2).value(value2)); - return f; - }))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - public List getEsDocument(String filedName1,String filedName2, Long value1,List value2,Integer size, String index, Class targetClass) { - - List fieldValues2 = value2.stream().map(FieldValue::of).toList(); - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - builder.size(size); - - builder.query(q->q.bool(b->b.filter(f->{ - f.term(t->t.field(filedName1).value(value1)); - f.terms(t->t.field(filedName2).terms(e->e.value(fieldValues2))); - return f; - }))); - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - - - - public List getEsDocument(String filedName1,String filedName2, List value1,List value2,Integer size, String index, Class targetClass) { - - List fieldValues1 = value1.stream().map(FieldValue::of).toList(); - List fieldValues2 = value2.stream().map(FieldValue::of).toList(); - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - builder.size(size); - - builder.query(q->q.bool(b->b.filter(f->{ - f.terms(t->t.field(filedName1).terms(e->e.value(fieldValues1))); - f.terms(t->t.field(filedName2).terms(e->e.value(fieldValues2))); - return f; - }))); - - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - - } - - - - - - // 范围查询(包括最大值和最小值),仅限于造型数据查询使用 - public List getEsDocumentByRange(String filedName, Long value, String index, int minSortId, int maxSortId, Class targetClass) { - - SearchRequest.Builder builder = new SearchRequest.Builder(); - builder.index(index); - int size = maxSortId - minSortId; - builder.size( size <= 0 ? 100 : size+2 ); - - builder.query(q->q.bool(b->b.filter(f->{ - f.term(t->t.field(filedName).value(value)); - f.range(r -> r.field("sortId").gte(JsonData.of(minSortId)).lte(JsonData.of(maxSortId))); - return f; - }))); - - - try { - SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); - List> hits = search.hits().hits(); - if (CollUtil.isNotEmpty(hits)) { - return hits.stream().map(Hit::source).toList(); - } - return new ArrayList<>(); - }catch (JsonpMappingException e){ - log.error(e.getMessage(), e); - throw new ServiceException(ORDER_DATA_ERROR); - } catch (IOException | ElasticsearchException e) { - log.error(e.getMessage(), e); - throw new ServiceException(DATA_DATA_ERROR); - } - } - - - - - - - - - // searchAfter 查询造型数据 - public List getEsDocumentBySearchAfter(String filedName, - Long value, - String index, - int size, - int querySortId, - Class targetClass){ - - List results = new ArrayList<>(); - - try { - - - SearchRequest.Builder searchBuilder = new SearchRequest.Builder() - .index(index) - .size(size) - .sort(s -> s.field(f -> f.field("sortId").order(SortOrder.Asc))) - .query(q -> q.bool(b -> b - .filter(m -> m.term(t -> t.field(filedName).value(value))) - )); - - while (true) { - - searchBuilder.searchAfter(FieldValue.of(querySortId)); - - SearchResponse response = elasticsearchClient.search(searchBuilder.build(), targetClass); - - List> hits = response.hits().hits(); - if (hits.isEmpty()) { - break; - } - - for (Hit hit : hits) { - results.add(hit.source()); - } - - querySortId += size; - - } - - return results; - - } catch (JsonpMappingException e){ - log.error(e.getMessage()); - throw new ServiceException(ORDER_DATA_ERROR); - }catch (IOException | ElasticsearchException e) { - log.error(e.getMessage()); - throw new ServiceException(DATA_DATA_ERROR); - } - - } - - - - - - - - // scroll 游标查询,目前仅用于查询造型数据 - public List getEsDocumentByScroll(String filedName, Long value,Integer size, String index, Class targetClass){ - - // scroll 保持游标有效时间 - final String scrollTime = "1m"; - - List result = new ArrayList<>(); - - - String scrollId = null; - - try { - - // 执行第一次搜索,开启 scroll - SearchResponse searchResponse = elasticsearchClient.search(s -> s - .index(index) - .scroll(Time.of(t -> t.time(scrollTime))) - .size(size) - .query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))), -// .source(sou->sou.filter(f->f.includes("orderId","plateId","plateModelData","sortId"))), - targetClass - ); - - - scrollId = searchResponse.scrollId(); - List> hits = searchResponse.hits().hits(); - hits.forEach(hit -> result.add(hit.source())); - - // 循环 scroll 拉取剩余数据 - while (!hits.isEmpty()) { - String finalScrollId = scrollId; - ScrollResponse scrollResponse = elasticsearchClient.scroll(s -> s - .scrollId(finalScrollId) - .scroll(Time.of(t -> t.time(scrollTime))), targetClass); - - scrollId = scrollResponse.scrollId(); - hits = scrollResponse.hits().hits(); - - if (!hits.isEmpty()) { - hits.forEach(hit -> result.add(hit.source())); - } - } - - return result; - - }catch (IOException | ElasticsearchException e){ - log.error(e.getMessage()); - throw new ServiceException(DATA_DATA_ERROR); - }finally { - // 清理 scroll - clearScroll(scrollId); - } - - } - - - - - // scroll 游标查询,目前仅用于查询造型数据 - public List getEsDocumentByScroll(String filedName, List values,Integer size, String index, Class targetClass){ - - // scroll 保持游标有效时间 - final String scrollTime = "1m"; - - List result = new ArrayList<>(); - - List fieldValues = values.stream().map(FieldValue::of).toList(); - - String scrollId = null; - - try { - - // 执行第一次搜索,开启 scroll - SearchResponse searchResponse = elasticsearchClient.search(s -> s - .index(index) - .scroll(Time.of(t -> t.time(scrollTime))) - .size(size) - .query(q->q.bool(b->b.filter(f->f.terms(te->te.field(filedName).terms(v->v.value(fieldValues)))))), - targetClass - ); - - - scrollId = searchResponse.scrollId(); - List> hits = searchResponse.hits().hits(); - hits.forEach(hit -> result.add(hit.source())); - - // 循环 scroll 拉取剩余数据 - while (!hits.isEmpty()) { - String finalScrollId = scrollId; - ScrollResponse scrollResponse = elasticsearchClient.scroll(s -> s - .scrollId(finalScrollId) - .scroll(Time.of(t -> t.time(scrollTime))), targetClass); - - scrollId = scrollResponse.scrollId(); - hits = scrollResponse.hits().hits(); - - if (!hits.isEmpty()) { - hits.forEach(hit -> result.add(hit.source())); - } - } - - return result; - - }catch (IOException | ElasticsearchException e){ - log.error(e.getMessage()); - throw new ServiceException(DATA_DATA_ERROR); - }finally { - // 清理 scroll - clearScroll(scrollId); - } - - } - - - - - private void clearScroll(String scrollId) { - try { - if (scrollId != null && !scrollId.isEmpty()) { - elasticsearchClient.clearScroll(c -> c.scrollId(scrollId)); - } - } catch (Exception e) { - log.warn("Failed to clear scroll: {}", e.getMessage()); - } - } - - - - - - - - - - - // ES 文档删除 - public void deleteEsDocument(String filedName,Long value, String index) { - - DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); - builder.index(index); - - builder.query(q -> q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); - - try { - elasticsearchClient.deleteByQuery(builder.build()); - } catch (IOException | ElasticsearchException e) { - log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value); - throw new ServiceException(ES_DATA_ERROR); - } - } - - public void deleteEsDocument(String fieldName1,String fieldName2, Long value1,Long value2, String index) { - DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); - builder.index(index); - - builder.query(q -> q.bool(b -> b.filter( - Query.of(q1 -> q1.term(t -> t.field(fieldName1).value(value1))), - Query.of(q2 -> q2.term(t -> t.field(fieldName2).value(value2))) - ))); - - try { - elasticsearchClient.deleteByQuery(builder.build()); - } catch (IOException | ElasticsearchException e) { - log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value1); - throw new ServiceException(ES_DATA_ERROR); - } - } - - - - public void deleteEsDocument(String filedName1,String filedName2, Long value1,List values2, String index) { - - List fieldValues = values2.stream().map(FieldValue::of).toList(); - - DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); - builder.index(index); - - builder.query(q-> q.bool(b-> b.filter( - Query.of(q1->q1.term(t->t.field(filedName1).value(value1))), - Query.of(q2-> q2.terms(t->t.field(filedName2).terms(v2-> v2.value(fieldValues)))) - ))); - - try { - elasticsearchClient.deleteByQuery(builder.build()); - } catch (IOException | ElasticsearchException e) { - log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value1); - throw new ServiceException(ES_DATA_ERROR); - } - } - - - - public void deleteEsDocument(String filedName,List values, String index) { - - List fieldValues = values.stream().map(FieldValue::of).toList(); - DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); - builder.index(index); - - builder.query(q -> q.bool(b->b.filter(f->f.terms(t->t.field(filedName).terms(te->te.value(fieldValues)))))); - - try { - elasticsearchClient.deleteByQuery(builder.build()); - } catch (IOException | ElasticsearchException e) { - log.error(DOCUMENT_DELETE_ERROR, e.getMessage(), JsonUtils.toJsonString(values)); - throw new ServiceException(ES_DATA_ERROR); - } - } - - - - /** - * ES 文档保存 - * - * @param targetClass - */ - public void saveEsDocument(String index,List targetClass) { - try { - - esDocumentService.bulkCreate(index, targetClass); - - } catch (IOException | ElasticsearchException e) { - e.printStackTrace(); - log.error(e.getMessage()); - throw new ServiceException(DATA_DATA_ERROR); - } - - } - - - - - - // ES 文档数据刷新 - public void refresh( String index) { - - try { - // 创建刷新请求 - RefreshRequest refreshRequest = new RefreshRequest.Builder().index(index).build(); - - elasticsearchClient.indices().refresh(refreshRequest); - - } catch (IOException | ElasticsearchException e) { - log.error("ES文档数据刷新失败:"+e.getMessage()); - throw new ServiceException(ES_DATA_ERROR); - } - } - - - -} +//package com.cf.imes.module.executor.util.elasticsearch; +// +// +//import cn.hutool.core.collection.CollUtil; +//import co.elastic.clients.elasticsearch.ElasticsearchClient; +//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.Time; +//import co.elastic.clients.elasticsearch._types.query_dsl.Query; +//import co.elastic.clients.elasticsearch.core.DeleteByQueryRequest; +//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.search.Hit; +//import co.elastic.clients.elasticsearch.indices.RefreshRequest; +//import co.elastic.clients.json.JsonData; +//import co.elastic.clients.json.JsonpMappingException; +//import com.cf.imes.framework.common.exception.ServiceException; +//import com.cf.imes.framework.common.util.json.JsonUtils; +//import com.cf.imes.framework.es.core.dal.ESDocument; +//import com.cf.imes.framework.es.core.service.ESDocumentService; +//import lombok.extern.slf4j.Slf4j; +// +//import jakarta.annotation.Resource; +//import java.io.IOException; +//import java.util.ArrayList; +//import java.util.List; +// +//import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_DATA_ERROR; +//import static com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants.ES_DATA_ERROR; +//import static com.cf.imes.module.executor.enums.ErrorCodeConstants.ORDER_DATA_ERROR; +// +///** +// * @author ES 工具类 +// */ +//@Slf4j +//public class EsUtils { +// +// private final ElasticsearchClient elasticsearchClient; +// +// private static final String DOCUMENT_DELETE_ERROR = "ES文档删除失败:{},排单号/订单号为:{}"; +// +// +// +// @Resource +// private ESDocumentService esDocumentService; +// +// +// public EsUtils(ElasticsearchClient elasticsearchClient) { +// this.elasticsearchClient = elasticsearchClient; +// } +// +// +// /** +// * ES 文档查询 +// * @param filedName 需要查询的文档字段 +// * @param value 需要查询的值 +// * @param index 索引 +// * @param targetClass 索引文档的实体类 +// */ +// public List getEsDocument(String filedName, Long value, String index, Class targetClass) { +// +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// +// builder.query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// public List getEsDocument(String filedName, Long value,Integer size, String index, Class targetClass) { +// +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// builder.size(size); +// +// builder.query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// } catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// /** +// * ES 文档查询 +// * @param filedName 需要查询的文档字段 +// * @param values 需要查询的值(集合类型) +// * @param index 索引对象 +// */ +// public List getEsDocument(String filedName, List values, String index, Class targetClass) { +// +// List fieldValues = values.stream().map(FieldValue::of).toList(); +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// +// builder.query(q -> q.bool(b->b.filter(f->f.terms(t->t.field(filedName).terms(te->te.value(fieldValues)))))); +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// public List getEsDocument(String filedName, List values,Integer size, String index, Class targetClass) { +// +// List fieldValues = values.stream().map(FieldValue::of).toList(); +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// builder.size(size); +// +// builder.query(q-> q.bool(b-> b.filter(f-> f.terms(t-> t.field(filedName).terms(v->v.value(fieldValues)))))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// +// public List getEsDocument(String filedName1,String filedName2, Long value1,Long value2, String index, Class targetClass) { +// +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// +// +// builder.query(q->q.bool(b->b.filter(f->{ +// f.term(t->t.field(filedName1).value(value1)); +// f.term(t->t.field(filedName2).value(value2)); +// return f; +// }))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// +// public List getEsDocument(String filedName1,String filedName2, Long value1,Long value2,Integer size, String index, Class targetClass) { +// +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// builder.size(size); +// +// builder.query(q->q.bool(b->b.filter(f->{ +// f.term(t->t.field(filedName1).value(value1)); +// f.term(t->t.field(filedName2).value(value2)); +// return f; +// }))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// public List getEsDocument(String filedName1,String filedName2, Long value1,List value2,Integer size, String index, Class targetClass) { +// +// List fieldValues2 = value2.stream().map(FieldValue::of).toList(); +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// builder.size(size); +// +// builder.query(q->q.bool(b->b.filter(f->{ +// f.term(t->t.field(filedName1).value(value1)); +// f.terms(t->t.field(filedName2).terms(e->e.value(fieldValues2))); +// return f; +// }))); +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// +// +// +// public List getEsDocument(String filedName1,String filedName2, List value1,List value2,Integer size, String index, Class targetClass) { +// +// List fieldValues1 = value1.stream().map(FieldValue::of).toList(); +// List fieldValues2 = value2.stream().map(FieldValue::of).toList(); +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// builder.size(size); +// +// builder.query(q->q.bool(b->b.filter(f->{ +// f.terms(t->t.field(filedName1).terms(e->e.value(fieldValues1))); +// f.terms(t->t.field(filedName2).terms(e->e.value(fieldValues2))); +// return f; +// }))); +// +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// +// } +// +// +// +// +// +// // 范围查询(包括最大值和最小值),仅限于造型数据查询使用 +// public List getEsDocumentByRange(String filedName, Long value, String index, int minSortId, int maxSortId, Class targetClass) { +// +// SearchRequest.Builder builder = new SearchRequest.Builder(); +// builder.index(index); +// int size = maxSortId - minSortId; +// builder.size( size <= 0 ? 100 : size+2 ); +// +// builder.query(q->q.bool(b->b.filter(f->{ +// f.term(t->t.field(filedName).value(value)); +// f.range(r -> r.field("sortId").gte(JsonData.of(minSortId)).lte(JsonData.of(maxSortId))); +// return f; +// }))); +// +// +// try { +// SearchResponse search = elasticsearchClient.search(builder.build(), targetClass); +// List> hits = search.hits().hits(); +// if (CollUtil.isNotEmpty(hits)) { +// return hits.stream().map(Hit::source).toList(); +// } +// return new ArrayList<>(); +// }catch (JsonpMappingException e){ +// log.error(e.getMessage(), e); +// throw new ServiceException(ORDER_DATA_ERROR); +// } catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage(), e); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// } +// +// +// +// +// +// +// +// +// // searchAfter 查询造型数据 +// public List getEsDocumentBySearchAfter(String filedName, +// Long value, +// String index, +// int size, +// int querySortId, +// Class targetClass){ +// +// List results = new ArrayList<>(); +// +// try { +// +// +// SearchRequest.Builder searchBuilder = new SearchRequest.Builder() +// .index(index) +// .size(size) +// .sort(s -> s.field(f -> f.field("sortId").order(SortOrder.Asc))) +// .query(q -> q.bool(b -> b +// .filter(m -> m.term(t -> t.field(filedName).value(value))) +// )); +// +// while (true) { +// +// searchBuilder.searchAfter(FieldValue.of(querySortId)); +// +// SearchResponse response = elasticsearchClient.search(searchBuilder.build(), targetClass); +// +// List> hits = response.hits().hits(); +// if (hits.isEmpty()) { +// break; +// } +// +// for (Hit hit : hits) { +// results.add(hit.source()); +// } +// +// querySortId += size; +// +// } +// +// return results; +// +// } catch (JsonpMappingException e){ +// log.error(e.getMessage()); +// throw new ServiceException(ORDER_DATA_ERROR); +// }catch (IOException | ElasticsearchException e) { +// log.error(e.getMessage()); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// +// } +// +// +// +// +// +// +// +// // scroll 游标查询,目前仅用于查询造型数据 +// public List getEsDocumentByScroll(String filedName, Long value,Integer size, String index, Class targetClass){ +// +// // scroll 保持游标有效时间 +// final String scrollTime = "1m"; +// +// List result = new ArrayList<>(); +// +// +// String scrollId = null; +// +// try { +// +// // 执行第一次搜索,开启 scroll +// SearchResponse searchResponse = elasticsearchClient.search(s -> s +// .index(index) +// .scroll(Time.of(t -> t.time(scrollTime))) +// .size(size) +// .query(q->q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))), +//// .source(sou->sou.filter(f->f.includes("orderId","plateId","plateModelData","sortId"))), +// targetClass +// ); +// +// +// scrollId = searchResponse.scrollId(); +// List> hits = searchResponse.hits().hits(); +// hits.forEach(hit -> result.add(hit.source())); +// +// // 循环 scroll 拉取剩余数据 +// while (!hits.isEmpty()) { +// String finalScrollId = scrollId; +// ScrollResponse scrollResponse = elasticsearchClient.scroll(s -> s +// .scrollId(finalScrollId) +// .scroll(Time.of(t -> t.time(scrollTime))), targetClass); +// +// scrollId = scrollResponse.scrollId(); +// hits = scrollResponse.hits().hits(); +// +// if (!hits.isEmpty()) { +// hits.forEach(hit -> result.add(hit.source())); +// } +// } +// +// return result; +// +// }catch (IOException | ElasticsearchException e){ +// log.error(e.getMessage()); +// throw new ServiceException(DATA_DATA_ERROR); +// }finally { +// // 清理 scroll +// clearScroll(scrollId); +// } +// +// } +// +// +// +// +// // scroll 游标查询,目前仅用于查询造型数据 +// public List getEsDocumentByScroll(String filedName, List values,Integer size, String index, Class targetClass){ +// +// // scroll 保持游标有效时间 +// final String scrollTime = "1m"; +// +// List result = new ArrayList<>(); +// +// List fieldValues = values.stream().map(FieldValue::of).toList(); +// +// String scrollId = null; +// +// try { +// +// // 执行第一次搜索,开启 scroll +// SearchResponse searchResponse = elasticsearchClient.search(s -> s +// .index(index) +// .scroll(Time.of(t -> t.time(scrollTime))) +// .size(size) +// .query(q->q.bool(b->b.filter(f->f.terms(te->te.field(filedName).terms(v->v.value(fieldValues)))))), +// targetClass +// ); +// +// +// scrollId = searchResponse.scrollId(); +// List> hits = searchResponse.hits().hits(); +// hits.forEach(hit -> result.add(hit.source())); +// +// // 循环 scroll 拉取剩余数据 +// while (!hits.isEmpty()) { +// String finalScrollId = scrollId; +// ScrollResponse scrollResponse = elasticsearchClient.scroll(s -> s +// .scrollId(finalScrollId) +// .scroll(Time.of(t -> t.time(scrollTime))), targetClass); +// +// scrollId = scrollResponse.scrollId(); +// hits = scrollResponse.hits().hits(); +// +// if (!hits.isEmpty()) { +// hits.forEach(hit -> result.add(hit.source())); +// } +// } +// +// return result; +// +// }catch (IOException | ElasticsearchException e){ +// log.error(e.getMessage()); +// throw new ServiceException(DATA_DATA_ERROR); +// }finally { +// // 清理 scroll +// clearScroll(scrollId); +// } +// +// } +// +// +// +// +// private void clearScroll(String scrollId) { +// try { +// if (scrollId != null && !scrollId.isEmpty()) { +// elasticsearchClient.clearScroll(c -> c.scrollId(scrollId)); +// } +// } catch (Exception e) { +// log.warn("Failed to clear scroll: {}", e.getMessage()); +// } +// } +// +// +// +// +// +// +// +// +// +// +// // ES 文档删除 +// public void deleteEsDocument(String filedName,Long value, String index) { +// +// DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); +// builder.index(index); +// +// builder.query(q -> q.bool(b->b.filter(f->f.term(t->t.field(filedName).value(value))))); +// +// try { +// elasticsearchClient.deleteByQuery(builder.build()); +// } catch (IOException | ElasticsearchException e) { +// log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value); +// throw new ServiceException(ES_DATA_ERROR); +// } +// } +// +// public void deleteEsDocument(String fieldName1,String fieldName2, Long value1,Long value2, String index) { +// DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); +// builder.index(index); +// +// builder.query(q -> q.bool(b -> b.filter( +// Query.of(q1 -> q1.term(t -> t.field(fieldName1).value(value1))), +// Query.of(q2 -> q2.term(t -> t.field(fieldName2).value(value2))) +// ))); +// +// try { +// elasticsearchClient.deleteByQuery(builder.build()); +// } catch (IOException | ElasticsearchException e) { +// log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value1); +// throw new ServiceException(ES_DATA_ERROR); +// } +// } +// +// +// +// public void deleteEsDocument(String filedName1,String filedName2, Long value1,List values2, String index) { +// +// List fieldValues = values2.stream().map(FieldValue::of).toList(); +// +// DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); +// builder.index(index); +// +// builder.query(q-> q.bool(b-> b.filter( +// Query.of(q1->q1.term(t->t.field(filedName1).value(value1))), +// Query.of(q2-> q2.terms(t->t.field(filedName2).terms(v2-> v2.value(fieldValues)))) +// ))); +// +// try { +// elasticsearchClient.deleteByQuery(builder.build()); +// } catch (IOException | ElasticsearchException e) { +// log.error(DOCUMENT_DELETE_ERROR, e.getMessage(),value1); +// throw new ServiceException(ES_DATA_ERROR); +// } +// } +// +// +// +// public void deleteEsDocument(String filedName,List values, String index) { +// +// List fieldValues = values.stream().map(FieldValue::of).toList(); +// DeleteByQueryRequest.Builder builder = new DeleteByQueryRequest.Builder(); +// builder.index(index); +// +// builder.query(q -> q.bool(b->b.filter(f->f.terms(t->t.field(filedName).terms(te->te.value(fieldValues)))))); +// +// try { +// elasticsearchClient.deleteByQuery(builder.build()); +// } catch (IOException | ElasticsearchException e) { +// log.error(DOCUMENT_DELETE_ERROR, e.getMessage(), JsonUtils.toJsonString(values)); +// throw new ServiceException(ES_DATA_ERROR); +// } +// } +// +// +// +// /** +// * ES 文档保存 +// * +// * @param targetClass +// */ +// public void saveEsDocument(String index,List targetClass) { +// try { +// +// esDocumentService.bulkCreate(index, targetClass); +// +// } catch (IOException | ElasticsearchException e) { +// e.printStackTrace(); +// log.error(e.getMessage()); +// throw new ServiceException(DATA_DATA_ERROR); +// } +// +// } +// +// +// +// +// +// // ES 文档数据刷新 +// public void refresh( String index) { +// +// try { +// // 创建刷新请求 +// RefreshRequest refreshRequest = new RefreshRequest.Builder().index(index).build(); +// +// elasticsearchClient.indices().refresh(refreshRequest); +// +// } catch (IOException | ElasticsearchException e) { +// log.error("ES文档数据刷新失败:"+e.getMessage()); +// throw new ServiceException(ES_DATA_ERROR); +// } +// } +// +// +// +//}