From 60f0e5a870de9eeb1245a90bb29dcb0f5eda69e1 Mon Sep 17 00:00:00 2001 From: xpc1024 <252645816@qq.com> Date: Mon, 10 Jul 2023 17:59:07 +0800 Subject: [PATCH] v2.0.0-beta3 --- easy-es-annotation/pom.xml | 2 +- easy-es-boot-starter/pom.xml | 2 +- .../config/EasyEsConfigProperties.java | 3 + .../starter/config/EsAutoConfiguration.java | 8 +- ...utoProcessIndexNotSmoothlyServiceImpl.java | 2 + .../AutoProcessIndexSmoothlyServiceImpl.java | 2 + easy-es-common/pom.xml | 2 +- .../common/enums/BaseEsParamTypeEnum.java | 41 ------- .../easyes/common/enums/NestedEnum.java | 25 ----- easy-es-core/pom.xml | 2 +- .../core/conditions/function/Query.java | 34 ++++++ .../easyes/core/config/GlobalConfig.java | 6 +- .../core/core/AbstractChainWrapper.java | 20 +++- .../easyes/core/core/AbstractWrapper.java | 44 +++++--- .../easyes/core/core/BaseEsMapper.java | 7 ++ .../easyes/core/core/BaseEsMapperImpl.java | 102 +++++++++++------- .../org/dromara/easyes/core/core/Wrapper.java | 16 ++- .../easyes/core/core/WrapperProcessor.java | 14 ++- .../easyes/core/toolkit/IndexUtils.java | 2 + easy-es-extension/pom.xml | 2 +- easy-es-parent/pom.xml | 2 +- easy-es-sample/pom.xml | 2 +- .../org/dromara/easyes/test/all/AllTest.java | 44 +++++--- .../src/test/resources/application.yml | 8 +- pom.xml | 2 +- 25 files changed, 242 insertions(+), 152 deletions(-) delete mode 100644 easy-es-common/src/main/java/org/dromara/easyes/common/enums/BaseEsParamTypeEnum.java delete mode 100644 easy-es-common/src/main/java/org/dromara/easyes/common/enums/NestedEnum.java diff --git a/easy-es-annotation/pom.xml b/easy-es-annotation/pom.xml index b3d100ac..a2ab8645 100644 --- a/easy-es-annotation/pom.xml +++ b/easy-es-annotation/pom.xml @@ -7,7 +7,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 ../easy-es-parent diff --git a/easy-es-boot-starter/pom.xml b/easy-es-boot-starter/pom.xml index 6a48acb5..99fce28c 100644 --- a/easy-es-boot-starter/pom.xml +++ b/easy-es-boot-starter/pom.xml @@ -5,7 +5,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 ../easy-es-parent 4.0.0 diff --git a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EasyEsConfigProperties.java b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EasyEsConfigProperties.java index 8ec2a94d..49ce7aa9 100644 --- a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EasyEsConfigProperties.java +++ b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EasyEsConfigProperties.java @@ -2,6 +2,8 @@ package org.dromara.easyes.starter.config; import org.dromara.easyes.core.config.GlobalConfig; import lombok.Data; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; @@ -15,6 +17,7 @@ import org.springframework.context.annotation.Configuration; @Data @Configuration @ConfigurationProperties(value = "easy-es") +@ConditionalOnExpression("'${easy-es.address:x}'!='x'") @ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true) public class EasyEsConfigProperties { /** diff --git a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EsAutoConfiguration.java b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EsAutoConfiguration.java index 4be82771..d13333c0 100644 --- a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EsAutoConfiguration.java +++ b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/config/EsAutoConfiguration.java @@ -1,18 +1,19 @@ package org.dromara.easyes.starter.config; -import org.dromara.easyes.common.utils.ExceptionUtils; -import org.dromara.easyes.common.utils.RestHighLevelClientBuilder; -import org.dromara.easyes.common.utils.StringUtils; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; +import org.dromara.easyes.common.utils.ExceptionUtils; +import org.dromara.easyes.common.utils.RestHighLevelClientBuilder; +import org.dromara.easyes.common.utils.StringUtils; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -35,6 +36,7 @@ import static org.dromara.easyes.common.constants.BaseEsConstants.DEFAULT_SCHEMA @Configuration @ConditionalOnClass(RestHighLevelClient.class) @EnableConfigurationProperties(EasyEsConfigProperties.class) +@ConditionalOnExpression("'${easy-es.address:x}'!='x'") @ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true) public class EsAutoConfiguration { @Autowired diff --git a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexNotSmoothlyServiceImpl.java b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexNotSmoothlyServiceImpl.java index f8350222..ccc265e8 100644 --- a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexNotSmoothlyServiceImpl.java +++ b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexNotSmoothlyServiceImpl.java @@ -10,6 +10,7 @@ import org.dromara.easyes.core.toolkit.IndexUtils; import org.dromara.easyes.starter.service.AutoProcessIndexService; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; @@ -20,6 +21,7 @@ import org.springframework.stereotype.Service; **/ @Service @ConditionalOnClass(RestHighLevelClient.class) +@ConditionalOnExpression("'${easy-es.address:x}'!='x'") @ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true) public class AutoProcessIndexNotSmoothlyServiceImpl implements AutoProcessIndexService { diff --git a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexSmoothlyServiceImpl.java b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexSmoothlyServiceImpl.java index 89a07240..649fff4b 100644 --- a/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexSmoothlyServiceImpl.java +++ b/easy-es-boot-starter/src/main/java/org/dromara/easyes/starter/service/impl/AutoProcessIndexSmoothlyServiceImpl.java @@ -10,6 +10,7 @@ import org.dromara.easyes.core.toolkit.IndexUtils; import org.dromara.easyes.starter.service.AutoProcessIndexService; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; @@ -23,6 +24,7 @@ import static org.dromara.easyes.common.constants.BaseEsConstants.SO_SUFFIX; **/ @Service @ConditionalOnClass(RestHighLevelClient.class) +@ConditionalOnExpression("'${easy-es.address:x}'!='x'") @ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true) public class AutoProcessIndexSmoothlyServiceImpl implements AutoProcessIndexService { @Override diff --git a/easy-es-common/pom.xml b/easy-es-common/pom.xml index f54b14d8..d7109edf 100644 --- a/easy-es-common/pom.xml +++ b/easy-es-common/pom.xml @@ -6,7 +6,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 ../easy-es-parent diff --git a/easy-es-common/src/main/java/org/dromara/easyes/common/enums/BaseEsParamTypeEnum.java b/easy-es-common/src/main/java/org/dromara/easyes/common/enums/BaseEsParamTypeEnum.java deleted file mode 100644 index e0462f43..00000000 --- a/easy-es-common/src/main/java/org/dromara/easyes/common/enums/BaseEsParamTypeEnum.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.dromara.easyes.common.enums; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * 参数类型 - *

- * Copyright © 2021 xpc1024 All Rights Reserved - **/ -@AllArgsConstructor -public enum BaseEsParamTypeEnum { - /** - * AND开头左括号 ( - */ - AND_LEFT_BRACKET(1), - /** - * AND开头右括号 ) - */ - AND_RIGHT_BRACKET(2), - /** - * OR开头左括号 ( - */ - OR_LEFT_BRACKET(3), - /** - * OR开头右括号 ) - */ - OR_RIGHT_BRACKET(4), - /** - * OR 左右括号都包含的情况 比如: - * wrapper.eq(User::getName, "张三") - * .or() - * .eq(Document::getAge, 18); - */ - OR_ALL(5); - /** - * 类型 - */ - @Getter - private Integer type; -} diff --git a/easy-es-common/src/main/java/org/dromara/easyes/common/enums/NestedEnum.java b/easy-es-common/src/main/java/org/dromara/easyes/common/enums/NestedEnum.java deleted file mode 100644 index e0eb9021..00000000 --- a/easy-es-common/src/main/java/org/dromara/easyes/common/enums/NestedEnum.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.dromara.easyes.common.enums; - -/** - * 查询参数Nested类型枚举 - *

- * Copyright © 2021 xpc1024 All Rights Reserved - **/ -public enum NestedEnum { - /** - * 与条件,相当于mysql中的and,必须满足且返回得分 - */ - MUST, - /** - * 取反的与条件,必须不满足 - */ - MUST_NOT, - /** - * 与条件必须满足,但不返回得分,效率更高 - */ - FILTER, - /** - * 或条件,相当于mysql中的or - */ - SHOULD; -} diff --git a/easy-es-core/pom.xml b/easy-es-core/pom.xml index 7b1e54e9..435dbd82 100644 --- a/easy-es-core/pom.xml +++ b/easy-es-core/pom.xml @@ -7,7 +7,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 ../easy-es-parent diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/conditions/function/Query.java b/easy-es-core/src/main/java/org/dromara/easyes/core/conditions/function/Query.java index f2944d62..14e8f6b7 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/conditions/function/Query.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/conditions/function/Query.java @@ -79,6 +79,21 @@ public interface Query extends Serializable { return notSelect(Arrays.stream(columns).map(FieldUtils::getFieldNameNotConvertId).toArray(String[]::new)); } + /** + * 设置最小得分 低于此分值的文档将不召回 + * + * @param score 最小得分 + * @return wrapper + */ + Children minScore(Float score); + + /** + * 开启计算得分 默认值为关闭状态 + * + * @return wrapper + */ + Children trackScores(); + /** * 设置不查询字段 * @@ -116,4 +131,23 @@ public interface Query extends Serializable { */ Children index(boolean condition, String... indexNames); + /** + * 设置查询偏好 + * + * @param preference 偏好 + * @return wrapper + */ + default Children preference(String preference) { + return preference(true, preference); + } + + /** + * 设置查询偏好 + * + * @param condition 条件 + * @param preference 偏好 + * @return wrapper + */ + Children preference(boolean condition, String preference); + } diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/config/GlobalConfig.java b/easy-es-core/src/main/java/org/dromara/easyes/core/config/GlobalConfig.java index 4c96dab3..9d3f1388 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/config/GlobalConfig.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/config/GlobalConfig.java @@ -1,11 +1,11 @@ package org.dromara.easyes.core.config; +import lombok.Data; import org.dromara.easyes.annotation.rely.FieldStrategy; import org.dromara.easyes.annotation.rely.IdType; import org.dromara.easyes.common.enums.ProcessIndexStrategyEnum; import org.dromara.easyes.common.enums.RefreshPolicy; -import lombok.Data; import org.springframework.boot.context.properties.NestedConfigurationProperty; import static org.dromara.easyes.common.constants.BaseEsConstants.EMPTY_STR; @@ -22,9 +22,9 @@ public class GlobalConfig { */ private boolean printDsl = true; /** - * process index mode Smoothly by default 索引处理模式 默认开启平滑模式 + * process index mode Manual by default 索引处理模式 默认开启手动模式 */ - private ProcessIndexStrategyEnum processIndexMode = ProcessIndexStrategyEnum.SMOOTHLY; + private ProcessIndexStrategyEnum processIndexMode = ProcessIndexStrategyEnum.MANUAL; /** * Rebuild index timeout unit: hour, default: 72 重建索引超时时间 单位小时,默认72 */ diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/core/AbstractChainWrapper.java b/easy-es-core/src/main/java/org/dromara/easyes/core/core/AbstractChainWrapper.java index e8e89d23..e3acb77d 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/core/AbstractChainWrapper.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/core/AbstractChainWrapper.java @@ -1,9 +1,9 @@ package org.dromara.easyes.core.core; +import org.apache.lucene.search.join.ScoreMode; import org.dromara.easyes.annotation.rely.FieldType; import org.dromara.easyes.core.biz.EntityFieldInfo; import org.dromara.easyes.core.biz.OrderByParam; -import org.apache.lucene.search.join.ScoreMode; import org.dromara.easyes.core.conditions.function.*; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoPoint; @@ -678,6 +678,18 @@ public abstract class AbstractChainWrapper { + /** + * 获取mapper中的entityClass + * + * @return entityClass + */ + Class getEntityClass(); + /** * 是否存在索引 * diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/core/BaseEsMapperImpl.java b/easy-es-core/src/main/java/org/dromara/easyes/core/core/BaseEsMapperImpl.java index f6335f91..92f385d9 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/core/BaseEsMapperImpl.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/core/BaseEsMapperImpl.java @@ -1,18 +1,5 @@ package org.dromara.easyes.core.core; -import org.dromara.easyes.annotation.rely.FieldStrategy; -import org.dromara.easyes.annotation.rely.IdType; -import org.dromara.easyes.common.constants.BaseEsConstants; -import org.dromara.easyes.common.enums.EsQueryTypeEnum; -import org.dromara.easyes.common.enums.MethodEnum; -import org.dromara.easyes.common.utils.*; -import org.dromara.easyes.core.biz.*; -import org.dromara.easyes.core.cache.BaseCache; -import org.dromara.easyes.core.cache.GlobalConfigCache; -import org.dromara.easyes.core.toolkit.EntityInfoHelper; -import org.dromara.easyes.core.toolkit.FieldUtils; -import org.dromara.easyes.core.toolkit.IndexUtils; -import org.dromara.easyes.core.toolkit.PageHelper; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializeFilter; @@ -21,6 +8,20 @@ import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import lombok.Setter; import lombok.SneakyThrows; import org.apache.http.util.EntityUtils; +import org.dromara.easyes.annotation.rely.FieldStrategy; +import org.dromara.easyes.annotation.rely.IdType; +import org.dromara.easyes.common.constants.BaseEsConstants; +import org.dromara.easyes.common.enums.EsQueryTypeEnum; +import org.dromara.easyes.common.enums.MethodEnum; +import org.dromara.easyes.common.enums.RefreshPolicy; +import org.dromara.easyes.common.utils.*; +import org.dromara.easyes.core.biz.*; +import org.dromara.easyes.core.cache.BaseCache; +import org.dromara.easyes.core.cache.GlobalConfigCache; +import org.dromara.easyes.core.toolkit.EntityInfoHelper; +import org.dromara.easyes.core.toolkit.FieldUtils; +import org.dromara.easyes.core.toolkit.IndexUtils; +import org.dromara.easyes.core.toolkit.PageHelper; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.action.bulk.BulkItemResponse; @@ -90,6 +91,11 @@ public class BaseEsMapperImpl implements BaseEsMapper { @Setter private Class entityClass; + @Override + public Class getEntityClass() { + return entityClass; + } + @Override public Boolean existsIndex(String indexName) { if (StringUtils.isEmpty(indexName)) { @@ -187,6 +193,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { public String executeDSL(String dsl, String indexName) { Assert.notNull(indexName, "indexName must not null"); Request request = new Request(MethodEnum.GET.name(), indexName + DSL_ENDPOINT); + request.setJsonEntity(dsl); Response response = client.getLowLevelClient().performRequest(request); return EntityUtils.toString(response.getEntity()); } @@ -221,6 +228,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { public String getSource(Wrapper wrapper) { // 获取由本框架生成的es查询参数 用于验证生成语法的正确性 SearchRequest searchRequest = new SearchRequest(getIndexNames(wrapper.indexNames)); + Optional.ofNullable(wrapper.preference).ifPresent(searchRequest::preference); Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); SearchSourceBuilder searchSourceBuilder = WrapperProcessor.buildSearchSourceBuilder(wrapper, entityClass); searchRequest.source(searchSourceBuilder); @@ -302,6 +310,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { // 不去重,直接count获取,效率更高 CountRequest countRequest = new CountRequest(getIndexNames(wrapper.indexNames)); Optional.ofNullable(getRouting()).ifPresent(countRequest::routing); + Optional.ofNullable(wrapper.preference).ifPresent(countRequest::preference); QueryBuilder queryBuilder = Optional.ofNullable(wrapper.searchSourceBuilder) .map(SearchSourceBuilder::query) .orElseGet(() -> WrapperProcessor.initBoolQueryBuilder(wrapper.paramQueue, entityClass)); @@ -379,6 +388,13 @@ public class BaseEsMapperImpl implements BaseEsMapper { @Override public Integer delete(Wrapper wrapper) { DeleteByQueryRequest request = new DeleteByQueryRequest(getIndexNames(wrapper.indexNames)); + EntityInfo entityInfo = EntityInfoHelper.getEntityInfo(entityClass); + Optional.ofNullable(entityInfo) + .flatMap(i->Optional.ofNullable(i.getMaxResultWindow())) + .ifPresent(request::setBatchSize); + if (RefreshPolicy.IMMEDIATE.getValue().equals(getRefreshPolicy())) { + request.setRefresh(true); + } Optional.ofNullable(getRouting()).ifPresent(request::setRouting); BoolQueryBuilder boolQueryBuilder = WrapperProcessor.initBoolQueryBuilder(wrapper.paramQueue, entityClass); request.setQuery(boolQueryBuilder); @@ -474,28 +490,6 @@ public class BaseEsMapperImpl implements BaseEsMapper { return result; } - private List doSelectBatchIds(Collection idList, String indexName) { - // 构造查询参数 - List stringIdList = idList.stream().map(Object::toString).collect(Collectors.toList()); - SearchRequest searchRequest = new SearchRequest(getIndexNames(indexName)); - Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); - sourceBuilder.query(QueryBuilders.termsQuery(DEFAULT_ES_ID_NAME, stringIdList)); - sourceBuilder.size(idList.size()); - searchRequest.source(sourceBuilder); - - // 请求es获取数据 - SearchHit[] searchHitArray = getSearchHitArray(searchRequest); - if (ArrayUtils.isEmpty(searchHitArray)) { - return new ArrayList<>(0); - } - - // 批量解析数据 - return Arrays.stream(searchHitArray) - .map(this::parseOne) - .collect(Collectors.toList()); - } - @Override public T selectOne(Wrapper wrapper) { // 请求es获取数据 @@ -792,6 +786,36 @@ public class BaseEsMapperImpl implements BaseEsMapper { return doBulkRequest(bulkRequest, RequestOptions.DEFAULT); } + + /** + * 执行根据id批量查询 + * + * @param idList id数组 + * @param indexName 索引名 + * @return 数据 + */ + private List doSelectBatchIds(Collection idList, String indexName) { + // 构造查询参数 + List stringIdList = idList.stream().map(Object::toString).collect(Collectors.toList()); + SearchRequest searchRequest = new SearchRequest(getIndexNames(indexName)); + Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); + sourceBuilder.query(QueryBuilders.termsQuery(DEFAULT_ES_ID_NAME, stringIdList)); + sourceBuilder.size(idList.size()); + searchRequest.source(sourceBuilder); + + // 请求es获取数据 + SearchHit[] searchHitArray = getSearchHitArray(searchRequest); + if (ArrayUtils.isEmpty(searchHitArray)) { + return new ArrayList<>(0); + } + + // 批量解析数据 + return Arrays.stream(searchHitArray) + .map(this::parseOne) + .collect(Collectors.toList()); + } + /** * 执行根据id查询 * @@ -829,6 +853,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { // 构建es restHighLevelClient 查询参数 SearchRequest searchRequest = new SearchRequest(getIndexNames(wrapper.indexNames)); Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); + Optional.ofNullable(wrapper.preference).ifPresent(searchRequest::preference); // 用户在wrapper中指定的混合查询条件优先级最高 SearchSourceBuilder searchSourceBuilder = Optional.ofNullable(wrapper.searchSourceBuilder) @@ -882,6 +907,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { // 构建查询条件 SearchRequest searchRequest = new SearchRequest(indexName); Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); + Optional.ofNullable(wrapper.preference).ifPresent(searchRequest::preference); SearchSourceBuilder searchSourceBuilder; if (Objects.isNull(wrapper.searchSourceBuilder)) { searchSourceBuilder = new SearchSourceBuilder(); @@ -894,7 +920,10 @@ public class BaseEsMapperImpl implements BaseEsMapper { } searchSourceBuilder.fetchSource(includes, null); searchSourceBuilder.trackTotalHits(true); - searchSourceBuilder.size(GlobalConfigCache.getGlobalConfig().getDbConfig().getBatchUpdateThreshold()); + int size = Optional.ofNullable(entityInfo) + .map(EntityInfo::getMaxResultWindow) + .orElse(GlobalConfigCache.getGlobalConfig().getDbConfig().getBatchUpdateThreshold()); + searchSourceBuilder.size(size); BoolQueryBuilder boolQueryBuilder = WrapperProcessor.initBoolQueryBuilder(wrapper.paramQueue, entityClass); searchSourceBuilder.query(boolQueryBuilder); } else { @@ -1150,6 +1179,7 @@ public class BaseEsMapperImpl implements BaseEsMapper { private SearchHit[] getSearchHitArray(Wrapper wrapper) { SearchRequest searchRequest = new SearchRequest(getIndexNames(wrapper.indexNames)); Optional.ofNullable(getRouting()).ifPresent(searchRequest::routing); + Optional.ofNullable(wrapper.preference).ifPresent(searchRequest::preference); // 用户在wrapper中指定的混合查询条件优先级最高 SearchSourceBuilder searchSourceBuilder = Objects.isNull(wrapper.searchSourceBuilder) ? diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/core/Wrapper.java b/easy-es-core/src/main/java/org/dromara/easyes/core/core/Wrapper.java index 152f75b2..0749bc89 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/core/Wrapper.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/core/Wrapper.java @@ -1,6 +1,7 @@ package org.dromara.easyes.core.core; +import lombok.SneakyThrows; import org.dromara.easyes.core.biz.*; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -35,6 +36,14 @@ public abstract class Wrapper implements Cloneable { * 不查字段 */ protected String[] exclude; + /** + * 排除_score 小于 min_score 中指定的最小值的文档 + */ + protected Float minScore; + /** + * 自定义排序时(如 脚本里面使用 _score),是否计算分数 + */ + protected Boolean trackScores; /** * 从第多少条开始查询 */ @@ -43,6 +52,10 @@ public abstract class Wrapper implements Cloneable { * 查询多少条记录 */ protected Integer size; + /** + * 当前查询的查询偏好 + */ + protected String preference; /** * 当前操作作用的索引名数组 */ @@ -109,8 +122,9 @@ public abstract class Wrapper implements Cloneable { * * @return Wrapper */ + @SneakyThrows protected Wrapper clone() { - return this.clone(); + return (Wrapper) super.clone(); } } diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/core/WrapperProcessor.java b/easy-es-core/src/main/java/org/dromara/easyes/core/core/WrapperProcessor.java index 51d8691c..aa56c77b 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/core/WrapperProcessor.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/core/WrapperProcessor.java @@ -136,8 +136,7 @@ public class WrapperProcessor { setBool(bool, queryBuilder, param.getPrevQueryType()); break; case QUERY_STRING: - realField = getRealFieldAndSuffix(param.getColumn(), fieldTypeMap, mappingColumnMap); - queryBuilder = QueryBuilders.queryStringQuery(realField).boost(param.getBoost()); + queryBuilder = QueryBuilders.queryStringQuery(param.getColumn()).boost(param.getBoost()); setBool(bool, queryBuilder, param.getPrevQueryType()); break; case PREFIX: @@ -335,12 +334,19 @@ public class WrapperProcessor { // 设置查询或不查询字段 setFetchSource(wrapper, mappingColumnMap, searchSourceBuilder); + // 设置排除_score 小于 min_score 中指定的最小值的文档 + Optional.ofNullable(wrapper.minScore).ifPresent(searchSourceBuilder::minScore); + + // 设置自定义排序时(如 脚本里面使用 _score) 是否计算分数 + Optional.ofNullable(wrapper.trackScores).ifPresent(searchSourceBuilder::trackScores); + // 设置聚合参数 setAggregations(wrapper, mappingColumnMap, searchSourceBuilder); // 设置查询起止参数 Optional.ofNullable(wrapper.from).ifPresent(searchSourceBuilder::from); - MyOptional.ofNullable(wrapper.size).ifPresent(searchSourceBuilder::size, DEFAULT_SIZE); + MyOptional.ofNullable(wrapper.size).ifPresent(searchSourceBuilder::size, + entityInfo.getMaxResultWindow() != null ? entityInfo.getMaxResultWindow() : DEFAULT_SIZE); // 根据全局配置决定是否开启全部查询 if (GlobalConfigCache.getGlobalConfig().getDbConfig().isEnableTrackTotalHits()) { @@ -556,7 +562,7 @@ public class WrapperProcessor { aggregationBuilder = AggregationBuilders.sum(name).field(realField); break; case TERMS: - aggregationBuilder = AggregationBuilders.terms(name).field(realField).size(Integer.MAX_VALUE); + aggregationBuilder = AggregationBuilders.terms(name).field(realField); break; default: throw new UnsupportedOperationException("不支持的聚合类型,参见AggregationTypeEnum"); diff --git a/easy-es-core/src/main/java/org/dromara/easyes/core/toolkit/IndexUtils.java b/easy-es-core/src/main/java/org/dromara/easyes/core/toolkit/IndexUtils.java index 8598f960..b923c51e 100644 --- a/easy-es-core/src/main/java/org/dromara/easyes/core/toolkit/IndexUtils.java +++ b/easy-es-core/src/main/java/org/dromara/easyes/core/toolkit/IndexUtils.java @@ -347,6 +347,8 @@ public class IndexUtils { type = FieldType.DOUBLE.getType(); break; case BIG_DECIMAL: + type = FieldType.SCALED_FLOAT.getType(); + break; case STRING: case CHAR: type = FieldType.KEYWORD_TEXT.getType(); diff --git a/easy-es-extension/pom.xml b/easy-es-extension/pom.xml index e30ce48b..67e17d55 100644 --- a/easy-es-extension/pom.xml +++ b/easy-es-extension/pom.xml @@ -7,7 +7,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 ../easy-es-parent diff --git a/easy-es-parent/pom.xml b/easy-es-parent/pom.xml index 6e4741fe..91efaa51 100644 --- a/easy-es-parent/pom.xml +++ b/easy-es-parent/pom.xml @@ -6,7 +6,7 @@ org.dromara.easy-es easy-es-parent - 2.0.0-beta2 + 2.0.0-beta3 easy-es-parent easy use for elastic search diff --git a/easy-es-sample/pom.xml b/easy-es-sample/pom.xml index 72728e27..505d5320 100644 --- a/easy-es-sample/pom.xml +++ b/easy-es-sample/pom.xml @@ -9,7 +9,7 @@ org.dromara.easy-es easy-es - 2.0.0-beta2 + 2.0.0-beta3 diff --git a/easy-es-test/src/test/java/org/dromara/easyes/test/all/AllTest.java b/easy-es-test/src/test/java/org/dromara/easyes/test/all/AllTest.java index b1983ca2..a464c1ad 100644 --- a/easy-es-test/src/test/java/org/dromara/easyes/test/all/AllTest.java +++ b/easy-es-test/src/test/java/org/dromara/easyes/test/all/AllTest.java @@ -58,6 +58,14 @@ public class AllTest { @Resource private DocumentMapper documentMapper; + @Test + @Order(0) + public void testCreateIndex() { + // 0.前置操作 创建索引 需确保索引托管模式处于manual手动挡,若为自动挡则会冲突. + boolean success = documentMapper.createIndex(); + Assertions.assertTrue(success); + } + // 1.新增 @Test @Order(1) @@ -181,7 +189,7 @@ public class AllTest { @Test @Order(6) public void testDSL() { - String dsl = "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"term\":{\"title.keyword\":{\"value\":\"测试文档2\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}}\"track_total_hits\":2147483647}"; + String dsl = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"title.keyword\":{\"value\":\"测试文档3\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"track_total_hits\":2147483647,\"highlight\":{\"pre_tags\":[\"\"],\"post_tags\":[\"\"],\"fragment_size\":2,\"fields\":{\"content\":{\"type\":\"unified\"}}}}"; String jsonResult = documentMapper.executeDSL(dsl); System.out.println(jsonResult); Assertions.assertNotNull(jsonResult); @@ -191,7 +199,6 @@ public class AllTest { @Order(6) public void testSelectOne() { LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); - wrapper.match(Document::getContent, "内容") .orderByAsc(Document::getStarNum, Document::getEsId) .limit(1); @@ -233,20 +240,20 @@ public class AllTest { @Test @Order(6) - public void testIgnoreCase(){ + public void testIgnoreCase() { LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); - wrapper.eq(Document::getCaseTest,"test"); + wrapper.eq(Document::getCaseTest, "test"); List documents = documentMapper.selectList(wrapper); - Assertions.assertEquals(1,documents.size()); + Assertions.assertEquals(1, documents.size()); } @Test @Order(6) - public void testIp(){ + public void testIp() { LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); - wrapper.eq(Document::getIpAddress,"192.168.0.0/16"); + wrapper.eq(Document::getIpAddress, "192.168.0.0/16"); List documents = documentMapper.selectList(wrapper); - Assertions.assertEquals(documents.size(),1); + Assertions.assertEquals(documents.size(), 1); } @Test @@ -258,6 +265,16 @@ public class AllTest { Assertions.assertEquals(22L, count); } + @Test + @Order(6) + public void testSelectCountDistinct() { + LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); + wrapper.match(Document::getCustomField, "字段"); + wrapper.distinct(Document::getStarNum); + Long count = documentMapper.selectCount(wrapper, true); + Assertions.assertEquals(21L, count); + } + @Test @Order(6) public void testConditionAllEq() { @@ -498,7 +515,9 @@ public class AllTest { // 测试混合查询的另一种方式 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery(FieldUtils.val(Document::getCreator), "老汉")); - searchSourceBuilder.size(BaseEsConstants.DEFAULT_SIZE); + Optional.ofNullable(EntityInfoHelper.getEntityInfo(Document.class)) + .flatMap(i -> Optional.ofNullable(i.getMaxResultWindow())) + .ifPresent(searchSourceBuilder::size); LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); wrapper.setSearchSourceBuilder(searchSourceBuilder); List documents = documentMapper.selectList(wrapper); @@ -541,7 +560,7 @@ public class AllTest { public void testConditionFilter() { LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); wrapper.eq(Document::getStarNum, 10) - .filter(i -> i.eq(Document::getTitle, "测试文档10")); + .filter().eq(Document::getTitle, "测试文档10"); List documents = documentMapper.selectList(wrapper); Assertions.assertEquals(1, documents.size()); } @@ -550,8 +569,8 @@ public class AllTest { @Order(6) public void testConditionNot() { LambdaEsQueryWrapper wrapper = new LambdaEsQueryWrapper<>(); - wrapper.in(Document::getStarNum, 10,11,12,13) - .not(i->i.eq(Document::getTitle,"测试文档10").eq(Document::getTitle,"测试文档11")); + wrapper.in(Document::getStarNum, 10, 11, 12, 13) + .and(i -> i.not().eq(Document::getTitle, "测试文档10").not().eq(Document::getTitle, "测试文档11")); List documents = documentMapper.selectList(wrapper); Assertions.assertEquals(2, documents.size()); } @@ -919,4 +938,5 @@ public class AllTest { .or(i -> i.eq("business_type", 2).in("state", 2, 3)); documentMapper.selectList(wrapper); } + } diff --git a/easy-es-test/src/test/resources/application.yml b/easy-es-test/src/test/resources/application.yml index 45f8fcac..30a033fe 100644 --- a/easy-es-test/src/test/resources/application.yml +++ b/easy-es-test/src/test/resources/application.yml @@ -1,12 +1,12 @@ easy-es: - enable: true +# enable: true address: 10.20.64.228:9200 schema: http - username: elastic - password: WG7WVmuNMtM4GwNYkyWH +# username: elastic +# password: WG7WVmuNMtM4GwNYkyWH keep-alive-millis: 18000 global-config: - process-index-mode: smoothly + process-index-mode: manual async-process-index-blocking: true print-dsl: true db-config: diff --git a/pom.xml b/pom.xml index 2b982b54..41d4f021 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.dromara.easy-es easy-es - 2.0.0-beta2 + 2.0.0-beta3 easy-es easy use for elastic search