diff --git a/easy-es-core/src/main/java/com/xpc/easyes/core/conditions/BaseEsMapperImpl.java b/easy-es-core/src/main/java/com/xpc/easyes/core/conditions/BaseEsMapperImpl.java index 029d37a6..73d9367a 100644 --- a/easy-es-core/src/main/java/com/xpc/easyes/core/conditions/BaseEsMapperImpl.java +++ b/easy-es-core/src/main/java/com/xpc/easyes/core/conditions/BaseEsMapperImpl.java @@ -268,7 +268,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { throw ExceptionUtils.eee("insert failed, result:%s entity:%s", indexResponse.getResult(), entity); } } catch (IOException e) { - throw ExceptionUtils.eee("insert entity:%s exception", e, entity); + throw ExceptionUtils.eee("insert entity:%s exception", e, entity.toString()); } } @@ -302,7 +302,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { return ONE; } } catch (IOException e) { - throw ExceptionUtils.eee("deleteById exception:%s, id:%s", e, id); + throw ExceptionUtils.eee("deleteById exception, id:%s", e, id.toString()); } return BaseEsConstants.ZERO; } @@ -364,7 +364,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { return ONE; } } catch (IOException e) { - throw ExceptionUtils.eee("updateById exception,entity:%s", e, entity); + throw ExceptionUtils.eee("updateById exception,entity:%s", e, entity.toString()); } return BaseEsConstants.ZERO; @@ -476,7 +476,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { public T selectOne(LambdaEsQueryWrapper wrapper) { long count = this.selectCount(wrapper); if (count > ONE && wrapper.size > ONE) { - throw ExceptionUtils.eee("fond more than one result: %d , please use limit function to limit 1", count); + throw ExceptionUtils.eee("found more than one result: %d , please use limit function to limit 1", count); } // 请求es获取数据 @@ -654,7 +654,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { try { searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); } catch (IOException e) { - throw ExceptionUtils.eee("getSearchHitArray exception,searchRequest:%s", e, searchRequest); + throw ExceptionUtils.eee("getSearchHitArray exception,searchRequest:%s", e, searchRequest.toString()); } return parseSearchHitArray(searchResponse); } @@ -675,7 +675,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { try { response = client.search(searchRequest, RequestOptions.DEFAULT); } catch (IOException e) { - throw ExceptionUtils.eee("getSearchHitArray IOException, searchRequest:%s", e, searchRequest); + throw ExceptionUtils.eee("getSearchHitArray IOException, searchRequest:%s", e, searchRequest.toString()); } return parseSearchHitArray(response); } @@ -744,7 +744,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { }); } } catch (Exception e) { - throw ExceptionUtils.eee("buildJsonIndexSource exception, entity:%s", e, entity); + throw ExceptionUtils.eee("buildJsonIndexSource exception, entity:%s", e, entity.toString()); } }); @@ -825,7 +825,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { } } } catch (IOException e) { - throw ExceptionUtils.eee("bulkRequest exception, msg:%s,cause:%s", e.getMessage(), e.getCause()); + throw ExceptionUtils.eee("bulkRequest exception", e); } return totalSuccess; } @@ -840,7 +840,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { return Optional.ofNullable(searchResponse) .map(SearchResponse::getHits) .map(SearchHits::getHits) - .orElseThrow(() -> ExceptionUtils.eee("parseSearchHitArray exception, response:%s", searchResponse)); + .orElseThrow(() -> ExceptionUtils.eee("parseSearchHitArray exception")); } /** diff --git a/easy-es-core/src/main/java/com/xpc/easyes/core/config/GlobalConfig.java b/easy-es-core/src/main/java/com/xpc/easyes/core/config/GlobalConfig.java index 43151a69..a85f21db 100644 --- a/easy-es-core/src/main/java/com/xpc/easyes/core/config/GlobalConfig.java +++ b/easy-es-core/src/main/java/com/xpc/easyes/core/config/GlobalConfig.java @@ -23,6 +23,10 @@ public class GlobalConfig { * process index mode Smoothly by default 索引处理模式 默认开启平滑模式 */ private ProcessIndexStrategyEnum processIndexMode = ProcessIndexStrategyEnum.SMOOTHLY; + /** + * process index blocking main thread true by default 异步处理索引是否阻塞主线程 默认阻塞 + */ + private boolean asyncProcessIndexBlocking = true; /** * is distributed environment true by default 是否分布式环境 默认为是 */ diff --git a/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/EsQueryTypeUtil.java b/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/EsQueryTypeUtil.java index d7d21e03..5135cde5 100644 --- a/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/EsQueryTypeUtil.java +++ b/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/EsQueryTypeUtil.java @@ -37,7 +37,7 @@ public class EsQueryTypeUtil { setQueryBuilder(boolQueryBuilder, attachType, termQueryBuilder); } else if (Objects.equals(queryType, TERMS_QUERY.getType())) { // 此处处理由or转入shouldList的in参数 - TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(field, value).boost(boost); + TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(field, (Collection)value).boost(boost); setQueryBuilder(boolQueryBuilder, attachType, termsQueryBuilder); } else if (Objects.equals(queryType, MATCH_PHASE.getType())) { // 封装模糊分词查询参数(分词必须按原关键词顺序) diff --git a/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/IndexUtils.java b/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/IndexUtils.java index 41e547fb..69e9e172 100644 --- a/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/IndexUtils.java +++ b/easy-es-core/src/main/java/com/xpc/easyes/core/toolkit/IndexUtils.java @@ -56,7 +56,7 @@ public class IndexUtils { try { return client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { - throw ExceptionUtils.eee("existsIndex exception", indexName, e); + throw ExceptionUtils.eee("existsIndex exception indexName: %s", e, indexName); } } @@ -95,7 +95,7 @@ public class IndexUtils { CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); return createIndexResponse.isAcknowledged(); } catch (IOException e) { - throw ExceptionUtils.eee("create index exception ", createIndexRequest, e); + throw ExceptionUtils.eee("create index exception createIndexRequest: %s ", e, createIndexRequest.toString()); } } @@ -131,7 +131,7 @@ public class IndexUtils { try { getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT); } catch (IOException e) { - throw ExceptionUtils.eee("getIndex exception", indexName, e); + throw ExceptionUtils.eee("getIndex exception indexName: %s", e, indexName); } return parseGetIndexResponse(getIndexResponse, indexName); } @@ -152,7 +152,7 @@ public class IndexUtils { try { client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT); } catch (IOException e) { - e.printStackTrace(); + LogUtils.warn("addDefaultAlias exception", e.toString()); } } @@ -180,7 +180,7 @@ public class IndexUtils { } return Boolean.FALSE; } catch (IOException e) { - throw ExceptionUtils.eee("reindex exception", oldIndexName, releaseIndexName, e); + throw ExceptionUtils.eee("reindex exception oldIndexName:%s, releaseIndexName: %s", e, oldIndexName, releaseIndexName); } } @@ -349,7 +349,7 @@ public class IndexUtils { RequestOptions.DEFAULT); return acknowledgedResponse.isAcknowledged(); } catch (IOException e) { - throw ExceptionUtils.eee("changeAlias exception", oldIndexName, releaseIndexName, e); + throw ExceptionUtils.eee("changeAlias exception oldIndexName: %s, releaseIndexName: %s", e, oldIndexName, releaseIndexName); } } @@ -367,7 +367,7 @@ public class IndexUtils { AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); return acknowledgedResponse.isAcknowledged(); } catch (IOException e) { - throw ExceptionUtils.eee("deleteIndex exception", indexName, e); + throw ExceptionUtils.eee("deleteIndex exception indexName: %s", e, indexName); } } @@ -498,7 +498,7 @@ public class IndexUtils { * @param client RestHighLevelClient */ public static void supplyAsync(BiFunction, RestHighLevelClient, Boolean> biFunction, Class entityClass, RestHighLevelClient client) { - CompletableFuture.supplyAsync(() -> { + CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { GlobalConfig globalConfig = GlobalConfigCache.getGlobalConfig(); if (!globalConfig.isDistributed()) { // 非分布式项目, 直接处理 @@ -524,7 +524,14 @@ public class IndexUtils { } else { LogUtils.warn("===> Unfortunately, auto process index by Easy-Es failed, please check your configuration"); } - }).join(); + }); + + // 是否开启阻塞 默认开启 运行测试模块时建议开启阻塞,否则测试用例跑完后,主线程退出,但异步线程可能还没跑完,可能出现死锁 + // 生产环境或迁移数据量比较大的情况下,可以配置开启非阻塞,这样服务启动更快 + GlobalConfig globalConfig = GlobalConfigCache.getGlobalConfig(); + if (globalConfig.isAsyncProcessIndexBlocking()) { + completableFuture.join(); + } } } diff --git a/easy-es-sample/src/main/resources/application.yml b/easy-es-sample/src/main/resources/application.yml index 59a6dfe5..2caef358 100644 --- a/easy-es-sample/src/main/resources/application.yml +++ b/easy-es-sample/src/main/resources/application.yml @@ -6,9 +6,11 @@ easy-es: password: WG7WVmuNMtM4GwNYkyWH global-config: process-index-mode: smoothly + async-process-index-blocking: true print-dsl: true db-config: map-underscore-to-camel-case: false # table-prefix: dev_ id-type: customize field-strategy: not_empty +