!71 动态多数据源支持

Merge pull request !71 from liyy/feature
This commit is contained in:
elasticsearch 2024-01-26 01:51:37 +00:00 committed by Gitee
commit 575bc78292
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
65 changed files with 553 additions and 275 deletions

View File

@ -5,14 +5,14 @@
</p> </p>
<p align="center"> <p align="center">
为简化开发工作、提高生产效率而生 您的Star是我继续前进的动力如果喜欢EE请右上角帮忙点亮星星⭐!
</p> </p>
<p align="center"> <p align="center">
<a href="https://search.maven.org/search?q=g:io.github.xpc1024%20a:easy-*"> <a href="https://search.maven.org/search?q=g:io.github.xpc1024%20a:easy-*">
<img alt="maven" src="https://img.shields.io/github/v/release/xpc1024/easy-es?include_prereleases&logo=xpc&style=plastic"> <img alt="maven" src="https://img.shields.io/github/v/release/xpc1024/easy-es?include_prereleases&logo=xpc&style=plastic">
</a> </a>
<a href="https://www.murphysec.com/dr/1hRHmuH0DE3RYL9uE0" alt="OSCS Status"><img src="https://www.oscs1024.com/platform/badge/dromara/easy-es.git.svg?size=small"/></a> <a href="https://www.murphysec.com/dr/htY0sMYDQaDn4X8iXp" alt="OSCS Status"><img src="https://www.oscs1024.com/platform/badge/dromara/easy-es.git.svg?size=small"/></a>
<a href="https://www.apache.org/licenses/LICENSE-2.0"> <a href="https://www.apache.org/licenses/LICENSE-2.0">
<img alt="code style" src="https://img.shields.io/badge/license-Apache%202-4EB1BA.svg?style=flat-square"> <img alt="code style" src="https://img.shields.io/badge/license-Apache%202-4EB1BA.svg?style=flat-square">
</a> </a>
@ -44,7 +44,7 @@ Easy-Es是一款简化ElasticSearch搜索引擎操作的开源框架,全自动
底层采用Es官方提供的RestHighLevelClient,保证其原生性能及拓展性. 底层采用Es官方提供的RestHighLevelClient,保证其原生性能及拓展性.
技术讨论 QQ 群 247637156 群内可免费领取 颈椎保护 | 增肌 | 减脂 等健身计划 技术讨论 QQ 群 729148550 群内可在群文件中免费领取 颈椎保护 | 增肌 | 减脂 等健身计划 无套路
微信群请先添加作者微信,由作者拉入 (亦可咨询健身问题,作者是健身教练) 微信群请先添加作者微信,由作者拉入 (亦可咨询健身问题,作者是健身教练)
@ -144,15 +144,6 @@ compile group: 'cn.easy-es', name: 'easy-es-boot-starter', version: 'Latest Vers
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0) [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)
# 捐赠 | Donate
---
[捐赠记录,感谢你们的支持!](https://easy-es.cn/pages/b52ac5/)
> 您的支持是鼓励我们前行的动力,无论金额多少都足够表达您这份心意。
> 如果您愿意捐赠本项目,推荐直接在右下方通过Gitee直接捐赠.
# 关注我 | About Me # 关注我 | About Me
--- ---
@ -163,3 +154,21 @@ QQ | 微信:252645816
# 知识星球 | Planet Of Knowledge # 知识星球 | Planet Of Knowledge
--- ---
<img alt="zsxq" src="https://iknow.hs.net/9038b7ab-c0d9-4a87-9492-e839907a8978.png"> <img alt="zsxq" src="https://iknow.hs.net/9038b7ab-c0d9-4a87-9492-e839907a8978.png">
# 捐赠 | Donate
---
[捐赠记录,感谢你们的支持!](https://easy-es.cn/pages/b52ac5/)
> 您的支持是鼓励我们前行的动力,无论金额多少都足够表达您这份心意。
> 如果您愿意捐赠本项目,推荐直接在右下方通过Gitee直接捐赠.
# 赞助商 | Sponsor
---
> 如果您想支持我们,奈何囊中羞涩,没事,您可以花30秒借花献佛,点击下方链接进入注册,则该赞助商会代您捐赠一笔小钱给社区开发者们买包辣条。
<a href="http://apifox.cn/a103easyse">
<img alt="ad" src="https://iknow.hs.net/a26897a9-d408-4985-9ed6-b3180ea6ed98.png">
</a>

View File

@ -12,7 +12,7 @@
<a href="https://search.maven.org/search?q=g:io.github.xpc1024%20a:easy-*"> <a href="https://search.maven.org/search?q=g:io.github.xpc1024%20a:easy-*">
<img alt="maven" src="https://img.shields.io/github/v/release/xpc1024/easy-es?include_prereleases&logo=xpc&style=plastic"> <img alt="maven" src="https://img.shields.io/github/v/release/xpc1024/easy-es?include_prereleases&logo=xpc&style=plastic">
</a> </a>
<a href="https://www.murphysec.com/dr/1hRHmuH0DE3RYL9uE0" alt="OSCS Status"><img src="https://www.oscs1024.com/platform/badge/dromara/easy-es.git.svg?size=small"/></a> <a href="https://www.murphysec.com/dr/htY0sMYDQaDn4X8iXp" alt="OSCS Status"><img src="https://www.oscs1024.com/platform/badge/dromara/easy-es.git.svg?size=small"/></a>
<a href="https://www.apache.org/licenses/LICENSE-2.0"> <a href="https://www.apache.org/licenses/LICENSE-2.0">
<img alt="code style" src="https://img.shields.io/badge/license-Apache%202-4EB1BA.svg?style=flat-square"> <img alt="code style" src="https://img.shields.io/badge/license-Apache%202-4EB1BA.svg?style=flat-square">
</a> </a>
@ -137,41 +137,44 @@ String indexName = "document";
--- ---
![Architecture](https://iknow.hs.net/27fb40b8-22d4-45c2-92e0-1471112d5102.jpg) ![Architecture](https://iknow.hs.net/27fb40b8-22d4-45c2-92e0-1471112d5102.jpg)
## Syntax comparison with MySQL ## MySQL Easy-Es and Es syntax comparison table
--- ---
| MySQL | Easy-Es |
| ---- | ---- |
| and | and |
| or | or |
| = | eq |
| != | ne|
| &gt; | gt |
| >= | ge |
| &lt; | lt |
| <= | le |
| like '%field%' | like |
| not like '%field%' |notLike|
| like '%field' | likeLeft|
| like 'field%' | likeRight |
| between | between |
| notBetween | notBetween |
| is null | isNull |
| is notNull | isNotNull |
| in | in |
| not in | notIn |
| group by | groupBy |
| order by | orderBy |
|min |min |
|max |max |
|avg |avg |
|sum |sum |
|sum |sum |
| - | orderByAsc |
| - | orderByDesc |
| - | match |
|- |highLight |
| ... | ... |
| MySQL | Easy-Es | Es-DSL/Es java api|
| --- | --- |--- |
| and | and |must|
| or | or | should|
| = | eq | term|
| != | ne | boolQueryBuilder.mustNot(queryBuilder)|
| > | gt | QueryBuilders.rangeQuery('es field').gt()|
| >= | ge | .rangeQuery('es field').gte()|
| < | lt | .rangeQuery('es field').lt() |
| <= | le | .rangeQuery('es field').lte()|
| like '%field%' | like | QueryBuilders.wildcardQuery(field,*value*)|
| not like '%field%' | notLike | must not wildcardQuery(field,*value*)|
| like '%field' | likeLeft | QueryBuilders.wildcardQuery(field,*value)|
| like 'field%' | likeRight | QueryBuilders.wildcardQuery(field,value*)|
| between | between | QueryBuilders.rangeQuery('es field').from(xx).to(xx) |
| notBetween | notBetween | must not QueryBuilders.rangeQuery('es field').from(xx).to(xx)|
| is null | isNull | must not QueryBuilders.existsQuery(field) |
| is notNull | isNotNull | QueryBuilders.existsQuery(field)|
| in | in | QueryBuilders.termsQuery(" xx es field", xx)|
| not in | notIn | must not QueryBuilders.termsQuery(" xx es field", xx)|
| group by | groupBy | AggregationBuilders.terms()|
| order by | orderBy | fieldSortBuilder.order(ASC/DESC)|
| min | min | AggregationBuilders.min|
| max | max |AggregationBuilders.max|
| avg | avg |AggregationBuilders.avg|
| sum | sum |AggregationBuilders.sum|
| order by xxx asc | orderByAsc | fieldSortBuilder.order(SortOrder.ASC)|
| order by xxx desc | orderByDesc |fieldSortBuilder.order(SortOrder.DESC)|
| - | match |matchQuery|
| - | matchPhrase |QueryBuilders.matchPhraseQuery|
| - | matchPrefix |QueryBuilders.matchPhrasePrefixQuery|
| - | queryStringQuery |QueryBuilders.queryStringQuery|
| select * | matchAllQuery |QueryBuilders.matchAllQuery()|
| - | highLight |HighlightBuilder.Field |
| ... | ... | ...|
--- ---
## Donate ## Donate

BIN
dist/img/640x240.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 598 KiB

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<relativePath>../easy-es-parent</relativePath> <relativePath>../easy-es-parent</relativePath>
</parent> </parent>
@ -18,10 +18,4 @@
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
</properties> </properties>
<dependencies>
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-common</artifactId>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,15 @@
package cn.easyes.annotation;
import java.lang.annotation.*;
/**
* @author lyy
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EsDS {
String value();
}

View File

@ -1,14 +1,15 @@
package cn.easyes.annotation; package cn.easyes.annotation;
import cn.easyes.common.enums.HighLightTypeEnum; import cn.easyes.annotation.rely.HighLightTypeEnum;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import static cn.easyes.common.constants.BaseEsConstants.*; import static cn.easyes.annotation.rely.AnnotationConstants.*;
/** /**
* 字段注解 * 字段注解
@ -45,8 +46,10 @@ public @interface HighLight {
* @return 高亮后置标签 * @return 高亮后置标签
*/ */
String postTag() default HIGH_LIGHT_POST_TAG; String postTag() default HIGH_LIGHT_POST_TAG;
/** /**
* 使用的高亮模式 * 使用的高亮模式
*
* @return 高亮模式 * @return 高亮模式
*/ */
HighLightTypeEnum highLightType() default HighLightTypeEnum.UNIFIED; HighLightTypeEnum highLightType() default HighLightTypeEnum.UNIFIED;

View File

@ -1,10 +1,7 @@
package cn.easyes.annotation; package cn.easyes.annotation;
import cn.easyes.common.constants.Analyzer;
import cn.easyes.common.enums.FieldStrategy; import cn.easyes.annotation.rely.*;
import cn.easyes.common.enums.FieldType;
import cn.easyes.common.params.DefaultNestedClass;
import cn.easyes.common.params.JoinField;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;

View File

@ -1,6 +1,6 @@
package cn.easyes.annotation; package cn.easyes.annotation;
import cn.easyes.common.enums.IdType; import cn.easyes.annotation.rely.IdType;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;

View File

@ -1,14 +1,14 @@
package cn.easyes.annotation; package cn.easyes.annotation;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.annotation.rely.DefaultChildClass;
import cn.easyes.common.params.DefaultChildClass;
import cn.easyes.common.params.JoinField;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import static cn.easyes.annotation.rely.AnnotationConstants.*;
/** /**
* 索引注解 * 索引注解
* <p> * <p>
@ -29,21 +29,28 @@ public @interface IndexName {
* *
* @return 默认为1 * @return 默认为1
*/ */
int shardsNum() default BaseEsConstants.DEFAULT_SHARDS; int shardsNum() default DEFAULT_SHARDS;
/** /**
* 副本数 * 副本数
* *
* @return 默认为1 * @return 默认为1
*/ */
int replicasNum() default BaseEsConstants.DEFAULT_REPLICAS; int replicasNum() default DEFAULT_REPLICAS;
/**
* 默认最大返回数
*
* @return 默认1w条
*/
int maxResultWindow() default DEFAULT_MAX_RESULT_WINDOW;
/** /**
* 索引别名 * 索引别名
* *
* @return 别名 * @return 别名
*/ */
String aliasName() default BaseEsConstants.DEFAULT_ALIAS; String aliasName() default DEFAULT_ALIAS;
/** /**
* 是否保持使用全局的 tablePrefix 的值 * 是否保持使用全局的 tablePrefix 的值
@ -67,5 +74,4 @@ public @interface IndexName {
* @return 默认子类 * @return 默认子类
*/ */
Class<?> childClass() default DefaultChildClass.class; Class<?> childClass() default DefaultChildClass.class;
} }

View File

@ -1,4 +1,4 @@
package cn.easyes.common.constants; package cn.easyes.annotation.rely;
/** /**
* 分词器 如果不包含用户所需分词器,可自行指定传入字符串 * 分词器 如果不包含用户所需分词器,可自行指定传入字符串

View File

@ -0,0 +1,37 @@
package cn.easyes.annotation.rely;
/**
* 注解依赖常量
* <p>
* Copyright © 2022 xpc1024 All Rights Reserved
**/
public interface AnnotationConstants {
/**
* 高亮默认前缀标签
*/
String HIGH_LIGHT_PRE_TAG = "<em>";
/**
* 高亮默认后缀标签
*/
String HIGH_LIGHT_POST_TAG = "</em>";
/**
* 高亮截取默认长度
*/
int DEFAULT_FRAGMENT_SIZE = 100;
/**
* 默认分片数
*/
int DEFAULT_SHARDS = 1;
/**
* 默认副本数
*/
int DEFAULT_REPLICAS = 1;
/**
* 默认最大返回数
*/
int DEFAULT_MAX_RESULT_WINDOW = 10000;
/**
* 默认索引别名
*/
String DEFAULT_ALIAS = "ee_default_alias";
}

View File

@ -1,4 +1,4 @@
package cn.easyes.common.params; package cn.easyes.annotation.rely;
/** /**
* 父子类型-默认子类 * 父子类型-默认子类

View File

@ -1,4 +1,4 @@
package cn.easyes.common.params; package cn.easyes.annotation.rely;
/** /**
* 默认嵌套类 * 默认嵌套类

View File

@ -1,4 +1,4 @@
package cn.easyes.common.enums; package cn.easyes.annotation.rely;
/** /**
* 字段策略枚举 * 字段策略枚举

View File

@ -1,14 +1,10 @@
package cn.easyes.common.enums; package cn.easyes.annotation.rely;
import lombok.AllArgsConstructor;
import lombok.Getter;
/** /**
* Es支持的数据类型枚举 * Es支持的数据类型枚举
* <p> * <p>
* Copyright © 2021 xpc1024 All Rights Reserved * Copyright © 2021 xpc1024 All Rights Reserved
**/ **/
@AllArgsConstructor
public enum FieldType { public enum FieldType {
/** /**
* none Required inside the framework, do not use 框架内部需要,切勿使用,若不慎使用则会被当做keyword类型 * none Required inside the framework, do not use 框架内部需要,切勿使用,若不慎使用则会被当做keyword类型
@ -52,8 +48,14 @@ public enum FieldType {
TOKEN("token"), TOKEN("token"),
ATTACHMENT("attachment"), ATTACHMENT("attachment"),
PERCOLATOR("percolator"); PERCOLATOR("percolator");
@Getter
private String type; private String type;
FieldType(String type) {
this.type = type;
}
public String getType() {
return type;
}
} }

View File

@ -1,15 +1,11 @@
package cn.easyes.common.enums; package cn.easyes.annotation.rely;
import lombok.AllArgsConstructor;
import lombok.Getter;
/** /**
* 高亮类型枚举 * 高亮类型枚举
* <p> * <p>
* Copyright © 2021 xpc1024 All Rights Reserved * Copyright © 2021 xpc1024 All Rights Reserved
**/ **/
@Getter
@AllArgsConstructor
public enum HighLightTypeEnum { public enum HighLightTypeEnum {
/** /**
* unified通用高亮策略,缺省配置时,默认采用此策略 * unified通用高亮策略,缺省配置时,默认采用此策略
@ -34,4 +30,12 @@ public enum HighLightTypeEnum {
* 聚合类型英文名 * 聚合类型英文名
*/ */
private final String value; private final String value;
HighLightTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
} }

View File

@ -1,4 +1,4 @@
package cn.easyes.common.enums; package cn.easyes.annotation.rely;
/** /**
* 主键类型枚举 * 主键类型枚举

View File

@ -0,0 +1,41 @@
package cn.easyes.annotation.rely;
/**
* 父子类型统一关系字段,推荐直接使用此类,不要重复造轮子
* <p>
* Copyright © 2022 xpc1024 All Rights Reserved
**/
public class JoinField {
/**
* 字段名
*/
private String name;
/**
* 父文档id
*/
private String parent;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
@Override
public String toString() {
return "JoinField{" +
"name='" + name + '\'' +
", parent='" + parent + '\'' +
'}';
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<version>1.0.3</version> <version>1.1.1</version>
<relativePath>../easy-es-parent</relativePath> <relativePath>../easy-es-parent</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -0,0 +1,21 @@
package cn.easyes.starter.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.HashMap;
import java.util.Map;
/**
* @author lyy
* @date 2023年03月14日 15:57
*/
@Data
@ConfigurationProperties(prefix = "easy-es.dynamic")
public class DynamicEsProperties {
/**
* 配置多动态数据源key datasource id
*/
private Map<String, EasyEsConfigProperties> datasource = new HashMap<>();
}

View File

@ -2,6 +2,7 @@ package cn.easyes.starter.config;
import cn.easyes.common.utils.ExceptionUtils; import cn.easyes.common.utils.ExceptionUtils;
import cn.easyes.common.utils.RestHighLevelClientBuilder; import cn.easyes.common.utils.RestHighLevelClientBuilder;
import cn.easyes.common.utils.RestHighLevelClientUtils;
import cn.easyes.common.utils.StringUtils; import cn.easyes.common.utils.StringUtils;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
@ -31,12 +32,17 @@ import static cn.easyes.common.constants.BaseEsConstants.DEFAULT_SCHEMA;
**/ **/
@Configuration @Configuration
@ConditionalOnClass(RestHighLevelClient.class) @ConditionalOnClass(RestHighLevelClient.class)
@EnableConfigurationProperties(EasyEsConfigProperties.class) @EnableConfigurationProperties(value = {DynamicEsProperties.class,EasyEsConfigProperties.class})
@ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true)
public class EsAutoConfiguration { public class EsAutoConfiguration {
@Autowired @Autowired
private EasyEsConfigProperties easyEsConfigProperties; private EasyEsConfigProperties easyEsConfigProperties;
@Autowired
private DynamicEsProperties dynamicEsProperties;
private final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
/** /**
* 装配RestHighLevelClient * 装配RestHighLevelClient
* *
@ -45,6 +51,10 @@ public class EsAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RestHighLevelClient restHighLevelClient() { public RestHighLevelClient restHighLevelClient() {
return restHighLevelClient(easyEsConfigProperties);
}
private RestHighLevelClient restHighLevelClient(EasyEsConfigProperties easyEsConfigProperties){
// 处理地址 // 处理地址
String address = easyEsConfigProperties.getAddress(); String address = easyEsConfigProperties.getAddress();
if (StringUtils.isEmpty(address)) { if (StringUtils.isEmpty(address)) {
@ -94,7 +104,8 @@ public class EsAutoConfiguration {
Integer connectTimeOut = easyEsConfigProperties.getConnectTimeout(); Integer connectTimeOut = easyEsConfigProperties.getConnectTimeout();
Integer socketTimeOut = easyEsConfigProperties.getSocketTimeout(); Integer socketTimeOut = easyEsConfigProperties.getSocketTimeout();
Integer connectionRequestTimeOut = easyEsConfigProperties.getConnectionRequestTimeout(); Integer connectionRequestTimeOut = easyEsConfigProperties.getConnectionRequestTimeout();
boolean needSetRequestConfig = Objects.nonNull(connectTimeOut) || Objects.nonNull(connectionRequestTimeOut); boolean needSetRequestConfig = Objects.nonNull(connectTimeOut) || Objects.nonNull(socketTimeOut) ||
Objects.nonNull(connectionRequestTimeOut);
if (needSetRequestConfig) { if (needSetRequestConfig) {
builder.setRequestConfigCallback(requestConfigBuilder -> { builder.setRequestConfigCallback(requestConfigBuilder -> {
Optional.ofNullable(connectTimeOut).ifPresent(requestConfigBuilder::setConnectTimeout); Optional.ofNullable(connectTimeOut).ifPresent(requestConfigBuilder::setConnectTimeout);
@ -108,4 +119,19 @@ public class EsAutoConfiguration {
return RestHighLevelClientBuilder.build(builder); return RestHighLevelClientBuilder.build(builder);
} }
@Bean
public RestHighLevelClientUtils restHighLevelClientUtils(){
RestHighLevelClientUtils restHighLevelClientUtils = new RestHighLevelClientUtils();
Map<String, EasyEsConfigProperties> datasourceMap = dynamicEsProperties.getDatasource();
Map<String, RestHighLevelClient> restHighLevelClientMap = new HashMap<>();
for (String key : datasourceMap.keySet()) {
EasyEsConfigProperties easyEsConfigProperties = datasourceMap.get(key);
restHighLevelClientMap.put(key,restHighLevelClient(easyEsConfigProperties));
RestHighLevelClientUtils.registerRestHighLevelClient(key, restHighLevelClient(easyEsConfigProperties));
}
return restHighLevelClientUtils;
}
} }

View File

@ -1,9 +1,11 @@
package cn.easyes.starter.register; package cn.easyes.starter.register;
import cn.easyes.annotation.EsDS;
import cn.easyes.annotation.Intercepts; import cn.easyes.annotation.Intercepts;
import cn.easyes.annotation.rely.DefaultChildClass;
import cn.easyes.common.enums.ProcessIndexStrategyEnum; import cn.easyes.common.enums.ProcessIndexStrategyEnum;
import cn.easyes.common.params.DefaultChildClass;
import cn.easyes.common.utils.LogUtils; import cn.easyes.common.utils.LogUtils;
import cn.easyes.common.utils.RestHighLevelClientUtils;
import cn.easyes.common.utils.TypeUtils; import cn.easyes.common.utils.TypeUtils;
import cn.easyes.core.biz.EntityInfo; import cn.easyes.core.biz.EntityInfo;
import cn.easyes.core.cache.BaseCache; import cn.easyes.core.cache.BaseCache;
@ -26,6 +28,8 @@ import java.lang.reflect.Proxy;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static cn.easyes.common.utils.RestHighLevelClientUtils.DEFAULT_DS;
/** /**
* 代理类 * 代理类
* <p> * <p>
@ -35,7 +39,7 @@ public class MapperFactoryBean<T> implements FactoryBean<T> {
private Class<T> mapperInterface; private Class<T> mapperInterface;
@Autowired @Autowired
private RestHighLevelClient client; private RestHighLevelClientUtils restHighLevelClientUtils;
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@ -63,6 +67,12 @@ public class MapperFactoryBean<T> implements FactoryBean<T> {
// 初始化缓存 // 初始化缓存
GlobalConfigCache.setGlobalConfig(esConfigProperties.getGlobalConfig()); GlobalConfigCache.setGlobalConfig(esConfigProperties.getGlobalConfig());
//获取动态数据源
EsDS annotation = mapperInterface.getAnnotation(EsDS.class);
RestHighLevelClient client = restHighLevelClientUtils.getClient(annotation != null? annotation.value() : DEFAULT_DS);
BaseCache.initCache(mapperInterface, entityClass, client);
BaseCache.initCache(mapperInterface, entityClass, client); BaseCache.initCache(mapperInterface, entityClass, client);
// 创建代理 // 创建代理

View File

@ -54,8 +54,13 @@ public class MapperScannerRegister implements ImportBeanDefinitionRegistrar, Res
" |___| \\__,_| /__/_ _|__/ _____ |___| /__/_\n" + " |___| \\__,_| /__/_ _|__/ _____ |___| /__/_\n" +
"_|\"\"\"\"\"|_|\"\"\"\"\"|_|\"\"\"\"\"|_| \"\"\"\"|_| |_|\"\"\"\"\"|_|\"\"\"\"\"|\n" + "_|\"\"\"\"\"|_|\"\"\"\"\"|_|\"\"\"\"\"|_| \"\"\"\"|_| |_|\"\"\"\"\"|_|\"\"\"\"\"|\n" +
"\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\n" + "\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\"`-0-0-'\n" +
"----------添加老汉微信:252645816,早日成为肌肉猛男----------->"); "------------------------------------------------------>"
System.out.println(":: easy-es :: (v:" + versionStr + ")"); );
System.out.println(":: version :: " + versionStr + " >");
System.out.println(":: home :: https://easy-es.cn/ >");
System.out.println(":: community :: https://dromara.org/ >");
System.out.println(":: wechat :: 252645816, add and become muscle man! >");
System.out.println("------------------------------------------------------>");
} }
AnnotationAttributes mapperScanAttrs = AnnotationAttributes AnnotationAttributes mapperScanAttrs = AnnotationAttributes

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<relativePath>../easy-es-parent</relativePath> <relativePath>../easy-es-parent</relativePath>
</parent> </parent>

View File

@ -34,18 +34,7 @@ public interface BaseEsConstants {
* 数字-1 * 数字-1
*/ */
Integer MINUS_ONE = -1; Integer MINUS_ONE = -1;
/**
* 默认索引别名
*/
String DEFAULT_ALIAS = "ee_default_alias";
/**
* 高亮默认前缀标签
*/
String HIGH_LIGHT_PRE_TAG = "<em>";
/**
* 高亮默认后缀标签
*/
String HIGH_LIGHT_POST_TAG = "</em>";
/** /**
* 默认的当前页码 * 默认的当前页码
*/ */
@ -102,6 +91,10 @@ public interface BaseEsConstants {
* 副本数量字段 * 副本数量字段
*/ */
String REPLICAS_FIELD = "index.number_of_replicas"; String REPLICAS_FIELD = "index.number_of_replicas";
/**
* 最大返回个数字段
*/
String MAX_RESULT_WINDOW_FIELD = "index.max_result_window";
/** /**
* 索引特性 * 索引特性
*/ */
@ -194,14 +187,7 @@ public interface BaseEsConstants {
* 初始任务执行延迟 * 初始任务执行延迟
*/ */
int INITIAL_DELAY = 30; int INITIAL_DELAY = 30;
/**
* 默认分片数
*/
int DEFAULT_SHARDS = 1;
/**
* 默认副本数
*/
int DEFAULT_REPLICAS = 1;
/** /**
* 被折叠的重复数据数量的key * 被折叠的重复数据数量的key
*/ */
@ -230,13 +216,13 @@ public interface BaseEsConstants {
* 索引权重key * 索引权重key
*/ */
String BOOST_KEY = "boost"; String BOOST_KEY = "boost";
/**
* 高亮截取默认长度
*/
int DEFAULT_FRAGMENT_SIZE = 100;
/** /**
* 针对text进行聚合 * 针对text进行聚合
*/ */
String FIELD_DATA = "fielddata"; String FIELD_DATA = "fielddata";
/**
* 未知的版本号
*/
String UNKNOWN = "unknown";
} }

View File

@ -1,20 +0,0 @@
package cn.easyes.common.params;
import lombok.Data;
/**
* 父子类型统一关系字段,推荐直接使用此类,不要重复造轮子
* <p>
* Copyright © 2022 xpc1024 All Rights Reserved
**/
@Data
public class JoinField {
/**
* 字段名
*/
private String name;
/**
* 父文档id
*/
private String parent;
}

View File

@ -8,6 +8,8 @@ import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import static cn.easyes.common.constants.BaseEsConstants.UNKNOWN;
/** /**
* ee 版本工具类 * ee 版本工具类
* *
@ -31,7 +33,7 @@ public class EEVersionUtils {
*/ */
public static <T> String getJarVersion(Class<T> objectClass) { public static <T> String getJarVersion(Class<T> objectClass) {
return Optional.ofNullable(objectClass.getPackage().getImplementationVersion()). return Optional.ofNullable(objectClass.getPackage().getImplementationVersion()).
orElse("unknown"); orElse(UNKNOWN);
} }
/** /**

View File

@ -5,15 +5,21 @@ import lombok.NoArgsConstructor;
import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import static cn.easyes.common.constants.BaseEsConstants.UNKNOWN;
/** /**
* elasticsearch 构造器 * elasticsearch 构造器
*/ */
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RestHighLevelClientBuilder { public class RestHighLevelClientBuilder {
/** /**
* 支持的版本 目前支持版本为7.xx 推荐7.14.0 * 支持的版本 目前支持版本为7.14.0 稳定无漏洞版
*/ */
private final static String supportedVersion = "7"; private final static String SUPPORTED_JAR_VERSION = "7.14.0";
/**
* 支持的客户端版本 目前支持7.xx 推荐7.14.0
*/
private final static String SUPPORTED_CLIENT_VERSION = "7";
/** /**
* 构建RestHighLevelClient * 构建RestHighLevelClient
@ -37,19 +43,18 @@ public class RestHighLevelClientBuilder {
// 校验jar包版本是否为推荐使用版本 // 校验jar包版本是否为推荐使用版本
String jarVersion = EEVersionUtils.getJarVersion(restHighLevelClient.getClass()); String jarVersion = EEVersionUtils.getJarVersion(restHighLevelClient.getClass());
LogUtils.formatInfo("Elasticsearch jar version:%s", jarVersion); LogUtils.formatInfo("Elasticsearch jar version:%s", jarVersion);
if (!jarVersion.startsWith(supportedVersion)) { if (!jarVersion.equals(SUPPORTED_JAR_VERSION) && !UNKNOWN.equals(jarVersion)) {
// 这里抛出异常原因是ee强制依赖于jar包版本jar包版本不对会导致ee异常 LogUtils.formatError("Easy-Es supported elasticsearch and restHighLevelClient jar version is:%s ,Please resolve the dependency conflict!", SUPPORTED_JAR_VERSION);
throw ExceptionUtils.eee("Easy-Es supported elasticsearch jar version is:%s.xx", supportedVersion);
} }
String clientVersion = EEVersionUtils.getClientVersion(restHighLevelClient); String clientVersion = EEVersionUtils.getClientVersion(restHighLevelClient);
LogUtils.formatInfo("Elasticsearch client version:%s", clientVersion); LogUtils.formatInfo("Elasticsearch client version:%s", clientVersion);
if (!clientVersion.startsWith(supportedVersion)) { if (!clientVersion.startsWith(SUPPORTED_CLIENT_VERSION)) {
// 这里校验客户端为非强制客户端版本非推荐版本对应提醒即可es会报错提醒 // 这里校验客户端为非强制客户端版本非推荐版本对应提醒即可es会报错提醒
LogUtils.formatWarn("Easy-Es supported elasticsearch client version is:%s.xx", supportedVersion); LogUtils.formatWarn("Easy-Es supported elasticsearch client version is:%s.xx", SUPPORTED_CLIENT_VERSION);
} }
if (!jarVersion.equals(clientVersion)) { if (!jarVersion.equals(clientVersion)) {
// 提示jar包与客户端版本不对应es官方推荐jar包版本对应客户端版本 // 提示jar包与客户端版本不对应es官方推荐jar包版本对应客户端版本
LogUtils.formatWarn("Elasticsearch clientVersion:%s not equals jarVersion:%s", clientVersion, jarVersion); LogUtils.formatWarn("Elasticsearch clientVersion:%s not equals jarVersion:%s, It does not affect your use, but we still recommend keeping it consistent!", clientVersion, jarVersion);
} }
} }
} }

View File

@ -0,0 +1,41 @@
package cn.easyes.common.utils;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author lyy
* @date 2023年03月14日 16:05
*/
public class RestHighLevelClientUtils {
public static final String DEFAULT_DS = "DEFAULT_DS";
private static Map<String, RestHighLevelClient> restHighLevelClientMap = new ConcurrentHashMap<>();
public RestHighLevelClientUtils() {
}
public static RestHighLevelClient getRestHighLevelClient(String restHighLevelClientId) {
if(DEFAULT_DS.equals(restHighLevelClientId)){
return restHighLevelClientMap.values().stream().findFirst().get();
}
RestHighLevelClient restHighLevelClient = restHighLevelClientMap.get(restHighLevelClientId);
if(restHighLevelClient == null){
ExceptionUtils.eee("找不到RestHighLevelClient,restHighLevelClientId:",restHighLevelClientId);
}
return restHighLevelClient;
}
public RestHighLevelClient getClient(String restHighLevelClientId) {
return RestHighLevelClientUtils.getRestHighLevelClient(restHighLevelClientId);
}
public static void registerRestHighLevelClient(String restHighLevelClientId, RestHighLevelClient restHighLevelClient) {
RestHighLevelClientUtils.restHighLevelClientMap.put(restHighLevelClientId, restHighLevelClient);
}
}

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<relativePath>../easy-es-parent</relativePath> <relativePath>../easy-es-parent</relativePath>
</parent> </parent>

View File

@ -34,6 +34,10 @@ public class CreateIndexParam {
* 副本数 * 副本数
*/ */
private Integer replicasNum; private Integer replicasNum;
/**
* 最大返回数
*/
private Integer maxResultWindow;
/** /**
* 索引字段及类型分词器等信息 * 索引字段及类型分词器等信息
*/ */

View File

@ -1,8 +1,8 @@
package cn.easyes.core.biz; package cn.easyes.core.biz;
import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexField;
import cn.easyes.common.enums.FieldStrategy; import cn.easyes.annotation.rely.FieldStrategy;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import cn.easyes.core.config.GlobalConfig; import cn.easyes.core.config.GlobalConfig;
import com.alibaba.fastjson.serializer.NameFilter; import com.alibaba.fastjson.serializer.NameFilter;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -36,7 +36,7 @@ public class EntityFieldInfo {
*/ */
private String mappingColumn; private String mappingColumn;
/** /**
* 在es中的存储类型 * 在es中的存储类型
*/ */
private FieldType fieldType; private FieldType fieldType;
/** /**

View File

@ -1,8 +1,8 @@
package cn.easyes.core.biz; package cn.easyes.core.biz;
import cn.easyes.annotation.rely.IdType;
import cn.easyes.annotation.rely.JoinField;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.common.constants.BaseEsConstants;
import cn.easyes.common.enums.IdType;
import cn.easyes.common.params.JoinField;
import com.alibaba.fastjson.PropertyNamingStrategy; import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor; import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import com.alibaba.fastjson.serializer.SerializeFilter; import com.alibaba.fastjson.serializer.SerializeFilter;
@ -14,6 +14,8 @@ import java.util.*;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cn.easyes.annotation.rely.AnnotationConstants.DEFAULT_MAX_RESULT_WINDOW;
/** /**
* 实体类信息 * 实体类信息
* <p> * <p>
@ -42,6 +44,10 @@ public class EntityInfo {
* 重试成功的索引名 * 重试成功的索引名
*/ */
private String retrySuccessIndexName; private String retrySuccessIndexName;
/**
* 最大返回数
*/
private Integer maxResultWindow = DEFAULT_MAX_RESULT_WINDOW;
/** /**
* 表映射结果集 * 表映射结果集
*/ */

View File

@ -26,6 +26,10 @@ public class EsIndexInfo {
* 副本数 * 副本数
*/ */
private Integer replicasNum; private Integer replicasNum;
/**
* 最大返回数
*/
private Integer maxResultWindow;
/** /**
* 索引字段信息 * 索引字段信息
*/ */

View File

@ -13,7 +13,7 @@ import java.util.List;
**/ **/
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class PageInfo<T> extends PageSerializable<T> { public class EsPageInfo<T> extends PageSerializable<T> {
/** /**
* 当前页 * 当前页
*/ */
@ -72,7 +72,7 @@ public class PageInfo<T> extends PageSerializable<T> {
/** /**
* 所有导航页号 * 所有导航页号
*/ */
private int[] navigatepageNums; private int[] navigatePageNums;
/** /**
* 导航条上的第一页 * 导航条上的第一页
*/ */
@ -87,7 +87,7 @@ public class PageInfo<T> extends PageSerializable<T> {
* *
* @param list 数据 * @param list 数据
*/ */
public PageInfo(List<T> list) { public EsPageInfo(List<T> list) {
this(list, 8); this(list, 8);
} }
@ -97,7 +97,7 @@ public class PageInfo<T> extends PageSerializable<T> {
* @param list 数据 * @param list 数据
* @param navigatePages 导航页 * @param navigatePages 导航页
*/ */
public PageInfo(List<T> list, int navigatePages) { public EsPageInfo(List<T> list, int navigatePages) {
super(list); super(list);
this.pageNum = 1; this.pageNum = 1;
this.pageSize = list.size(); this.pageSize = list.size();
@ -108,34 +108,28 @@ public class PageInfo<T> extends PageSerializable<T> {
this.endRow = list.size() > 0 ? list.size() - 1 : 0; this.endRow = list.size() > 0 ? list.size() - 1 : 0;
this.navigatePages = navigatePages; this.navigatePages = navigatePages;
//计算导航页
calcNavigatepageNums();
//计算前后页第一页最后一页
calcPage();
//判断页面边界
judgePageBoudary();
} }
public static <T> PageInfo<T> of(List<T> list) { public static <T> EsPageInfo<T> of(List<T> list) {
return new PageInfo<T>(list); return new EsPageInfo<T>(list);
} }
public static <T> PageInfo<T> of(List<T> list, int navigatePages) { public static <T> EsPageInfo<T> of(List<T> list, int navigatePages) {
return new PageInfo<T>(list, navigatePages); return new EsPageInfo<T>(list, navigatePages);
} }
/** /**
* 计算导航页 * 计算导航页
*/ */
private void calcNavigatepageNums() { public void calcNavigatePageNums() {
//当总页数小于或等于导航页码数时 //当总页数小于或等于导航页码数时
if (pages <= navigatePages) { if (pages <= navigatePages) {
navigatepageNums = new int[pages]; navigatePageNums = new int[pages];
for (int i = 0; i < pages; i++) { for (int i = 0; i < pages; i++) {
navigatepageNums[i] = i + 1; navigatePageNums[i] = i + 1;
} }
} else { //当总页数大于导航页码数时 } else { //当总页数大于导航页码数时
navigatepageNums = new int[navigatePages]; navigatePageNums = new int[navigatePages];
int startNum = pageNum - navigatePages / 2; int startNum = pageNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2; int endNum = pageNum + navigatePages / 2;
@ -143,18 +137,18 @@ public class PageInfo<T> extends PageSerializable<T> {
startNum = 1; startNum = 1;
//(最前navigatePages页 //(最前navigatePages页
for (int i = 0; i < navigatePages; i++) { for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++; navigatePageNums[i] = startNum++;
} }
} else if (endNum > pages) { } else if (endNum > pages) {
endNum = pages; endNum = pages;
//最后navigatePages页 //最后navigatePages页
for (int i = navigatePages - 1; i >= 0; i--) { for (int i = navigatePages - 1; i >= 0; i--) {
navigatepageNums[i] = endNum--; navigatePageNums[i] = endNum--;
} }
} else { } else {
//所有中间页 //所有中间页
for (int i = 0; i < navigatePages; i++) { for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++; navigatePageNums[i] = startNum++;
} }
} }
} }
@ -163,10 +157,10 @@ public class PageInfo<T> extends PageSerializable<T> {
/** /**
* 计算前后页第一页最后一页 * 计算前后页第一页最后一页
*/ */
private void calcPage() { public void calcPage() {
if (navigatepageNums != null && navigatepageNums.length > 0) { if (navigatePageNums != null && navigatePageNums.length > 0) {
navigateFirstPage = navigatepageNums[0]; navigateFirstPage = navigatePageNums[0];
navigateLastPage = navigatepageNums[navigatepageNums.length - 1]; navigateLastPage = navigatePageNums[navigatePageNums.length - 1];
if (pageNum > 1) { if (pageNum > 1) {
prePage = pageNum - 1; prePage = pageNum - 1;
} }
@ -179,10 +173,9 @@ public class PageInfo<T> extends PageSerializable<T> {
/** /**
* 判定页面边界 * 判定页面边界
*/ */
private void judgePageBoudary() { public void judgePageBoundary() {
isFirstPage = pageNum == 1; isFirstPage = pageNum == 1;
isLastPage = pageNum == pages || pages == 0; isLastPage = pageNum == pages || pages == 0;
;
hasPreviousPage = pageNum > 1; hasPreviousPage = pageNum > 1;
hasNextPage = pageNum < pages; hasNextPage = pageNum < pages;
} }
@ -203,7 +196,7 @@ public class PageInfo<T> extends PageSerializable<T> {
", hasPreviousPage=" + hasPreviousPage + ", hasPreviousPage=" + hasPreviousPage +
", hasNextPage=" + hasNextPage + ", hasNextPage=" + hasNextPage +
", navigatePages=" + navigatePages + ", navigatePages=" + navigatePages +
", navigatepageNums=" + Arrays.toString(navigatepageNums) + ", navigatepageNums=" + Arrays.toString(navigatePageNums) +
", navigateFirstPage=" + navigateFirstPage + ", navigateFirstPage=" + navigateFirstPage +
", navigateLastPage=" + navigateLastPage + ", navigateLastPage=" + navigateLastPage +
", total=" + total + ", total=" + total +

View File

@ -1,6 +1,6 @@
package cn.easyes.core.biz; package cn.easyes.core.biz;
import cn.easyes.common.enums.HighLightTypeEnum; import cn.easyes.annotation.rely.HighLightTypeEnum;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;

View File

@ -1,9 +1,9 @@
package cn.easyes.core.conditions; package cn.easyes.core.conditions;
import cn.easyes.annotation.rely.FieldStrategy;
import cn.easyes.annotation.rely.IdType;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.common.constants.BaseEsConstants;
import cn.easyes.common.enums.EsQueryTypeEnum; import cn.easyes.common.enums.EsQueryTypeEnum;
import cn.easyes.common.enums.FieldStrategy;
import cn.easyes.common.enums.IdType;
import cn.easyes.common.utils.*; import cn.easyes.common.utils.*;
import cn.easyes.core.biz.*; import cn.easyes.core.biz.*;
import cn.easyes.core.cache.BaseCache; import cn.easyes.core.cache.BaseCache;
@ -38,6 +38,7 @@ import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest; import org.elasticsearch.index.reindex.DeleteByQueryRequest;
@ -177,7 +178,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
} }
@Override @Override
public PageInfo<T> pageQuery(LambdaEsQueryWrapper<T> wrapper, Integer pageNum, Integer pageSize) { public EsPageInfo<T> pageQuery(LambdaEsQueryWrapper<T> wrapper, Integer pageNum, Integer pageSize) {
// 兼容分页参数 // 兼容分页参数
pageNum = pageNum == null || pageNum <= BaseEsConstants.ZERO ? BaseEsConstants.PAGE_NUM : pageNum; pageNum = pageNum == null || pageNum <= BaseEsConstants.ZERO ? BaseEsConstants.PAGE_NUM : pageNum;
pageSize = pageSize == null || pageSize <= BaseEsConstants.ZERO ? BaseEsConstants.PAGE_SIZE : pageSize; pageSize = pageSize == null || pageSize <= BaseEsConstants.ZERO ? BaseEsConstants.PAGE_SIZE : pageSize;
@ -190,7 +191,8 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
// 解析数据 // 解析数据
SearchHit[] searchHits = parseSearchHitArray(response); SearchHit[] searchHits = parseSearchHitArray(response);
List<T> dataList = Arrays.stream(searchHits).map(searchHit -> parseOne(searchHit, wrapper)) List<T> dataList = Arrays.stream(searchHits)
.map(searchHit -> parseOne(searchHit, wrapper))
.collect(Collectors.toList()); .collect(Collectors.toList());
long count = parseCount(response, Objects.nonNull(wrapper.distinctField)); long count = parseCount(response, Objects.nonNull(wrapper.distinctField));
return PageHelper.getPageInfo(dataList, count, pageNum, pageSize); return PageHelper.getPageInfo(dataList, count, pageNum, pageSize);
@ -247,9 +249,11 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
} else { } else {
// 不去重,直接count获取,效率更高 // 不去重,直接count获取,效率更高
CountRequest countRequest = new CountRequest(getIndexNames(wrapper.indexNames)); CountRequest countRequest = new CountRequest(getIndexNames(wrapper.indexNames));
BoolQueryBuilder boolQueryBuilder = WrapperProcessor.initBoolQueryBuilder(wrapper.baseEsParamList, QueryBuilder queryBuilder = Optional.ofNullable(wrapper.searchSourceBuilder)
wrapper.enableMust2Filter, entityClass); .map(SearchSourceBuilder::query)
countRequest.query(boolQueryBuilder); .orElseGet(() -> WrapperProcessor.initBoolQueryBuilder(wrapper.baseEsParamList,
wrapper.enableMust2Filter, entityClass));
countRequest.query(queryBuilder);
CountResponse count; CountResponse count;
try { try {
printCountDSL(countRequest); printCountDSL(countRequest);
@ -495,6 +499,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
// 设置分片个副本信息 // 设置分片个副本信息
Optional.ofNullable(wrapper.shardsNum).ifPresent(createIndexParam::setShardsNum); Optional.ofNullable(wrapper.shardsNum).ifPresent(createIndexParam::setShardsNum);
Optional.ofNullable(wrapper.replicasNum).ifPresent(createIndexParam::setReplicasNum); Optional.ofNullable(wrapper.replicasNum).ifPresent(createIndexParam::setReplicasNum);
Optional.ofNullable(wrapper.maxResultWindow).ifPresent(createIndexParam::setMaxResultWindow);
// 设置用户自定义的settings // 设置用户自定义的settings
Optional.ofNullable(wrapper.settings).ifPresent(createIndexParam::setSettings); Optional.ofNullable(wrapper.settings).ifPresent(createIndexParam::setSettings);
@ -760,9 +765,15 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
private SearchResponse getSearchResponse(LambdaEsQueryWrapper<T> wrapper, Object[] searchAfter) { private SearchResponse getSearchResponse(LambdaEsQueryWrapper<T> wrapper, Object[] searchAfter) {
// 构建es restHighLevelClient 查询参数 // 构建es restHighLevelClient 查询参数
SearchRequest searchRequest = new SearchRequest(getIndexNames(wrapper.indexNames)); SearchRequest searchRequest = new SearchRequest(getIndexNames(wrapper.indexNames));
// 用户在wrapper中指定的混合查询条件优先级最高 // 用户在wrapper中指定的混合查询条件优先级最高
SearchSourceBuilder searchSourceBuilder = Objects.isNull(wrapper.searchSourceBuilder) ? SearchSourceBuilder searchSourceBuilder = Optional.ofNullable(wrapper.searchSourceBuilder)
WrapperProcessor.buildSearchSourceBuilder(wrapper, entityClass) : wrapper.searchSourceBuilder; .map(builder -> {
// 兼容混合查询时用户在分页中自定义的分页参数
Optional.ofNullable(wrapper.from).ifPresent(builder::from);
Optional.ofNullable(wrapper.size).ifPresent(builder::size);
return builder;
}).orElseGet(() -> WrapperProcessor.buildSearchSourceBuilder(wrapper, entityClass));
searchRequest.source(searchSourceBuilder); searchRequest.source(searchSourceBuilder);
Optional.ofNullable(searchAfter).ifPresent(searchSourceBuilder::searchAfter); Optional.ofNullable(searchAfter).ifPresent(searchSourceBuilder::searchAfter);
printDSL(searchRequest); printDSL(searchRequest);

View File

@ -1,6 +1,7 @@
package cn.easyes.core.conditions; package cn.easyes.core.conditions;
import cn.easyes.common.enums.FieldType;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.params.SFunction; import cn.easyes.common.params.SFunction;
import cn.easyes.common.utils.ArrayUtils; import cn.easyes.common.utils.ArrayUtils;
import cn.easyes.common.utils.StringUtils; import cn.easyes.common.utils.StringUtils;
@ -9,10 +10,7 @@ import cn.easyes.core.conditions.interfaces.Index;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/** /**
* 索引Lambda表达式 * 索引Lambda表达式
@ -33,6 +31,10 @@ public class LambdaEsIndexWrapper<T> extends Wrapper<T> implements Index<LambdaE
* 副本数 * 副本数
*/ */
protected Integer replicasNum; protected Integer replicasNum;
/**
* 最大返回数
*/
protected Integer maxResultWindow;
/** /**
* 用户手动指定的索引mapping信息,优先级最高 * 用户手动指定的索引mapping信息,优先级最高
*/ */
@ -82,6 +84,11 @@ public class LambdaEsIndexWrapper<T> extends Wrapper<T> implements Index<LambdaE
return typedThis; return typedThis;
} }
@Override
public LambdaEsIndexWrapper<T> maxResultWindow(Integer maxResultWindow) {
Optional.ofNullable(maxResultWindow).ifPresent(max -> this.maxResultWindow = maxResultWindow);
return typedThis;
}
@Override @Override
public LambdaEsIndexWrapper<T> settings(Integer shards, Integer replicas) { public LambdaEsIndexWrapper<T> settings(Integer shards, Integer replicas) {

View File

@ -28,7 +28,7 @@ public class LambdaEsUpdateWrapper<T> extends AbstractLambdaUpdateWrapper<T, Lam
protected SearchSourceBuilder searchSourceBuilder; protected SearchSourceBuilder searchSourceBuilder;
/** /**
* 不建议直接 new 该实例使用 Wrappers.lambdaQuery(entity) * 不建议直接 new 该实例使用 EsWrappers.lambdaQuery(entity)
*/ */
public LambdaEsUpdateWrapper() { public LambdaEsUpdateWrapper() {
this(null); this(null);

View File

@ -222,12 +222,9 @@ public class WrapperProcessor {
Optional.ofNullable(wrapper.from).ifPresent(searchSourceBuilder::from); Optional.ofNullable(wrapper.from).ifPresent(searchSourceBuilder::from);
MyOptional.ofNullable(wrapper.size).ifPresent(searchSourceBuilder::size, DEFAULT_SIZE); MyOptional.ofNullable(wrapper.size).ifPresent(searchSourceBuilder::size, DEFAULT_SIZE);
if (searchSourceBuilder.size() > DEFAULT_SIZE) {
// 查询超过一万条, trackTotalHists自动开启
searchSourceBuilder.trackTotalHits(true);
} else {
// 根据全局配置决定是否开启 // 根据全局配置决定是否开启
searchSourceBuilder.trackTotalHits(GlobalConfigCache.getGlobalConfig().getDbConfig().isEnableTrackTotalHits()); if (GlobalConfigCache.getGlobalConfig().getDbConfig().isEnableTrackTotalHits()) {
searchSourceBuilder.trackTotalHits(Boolean.TRUE);
} }
return searchSourceBuilder; return searchSourceBuilder;

View File

@ -1,6 +1,6 @@
package cn.easyes.core.conditions.interfaces; package cn.easyes.core.conditions.interfaces;
import cn.easyes.core.biz.PageInfo; import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.biz.SAPageInfo; import cn.easyes.core.biz.SAPageInfo;
import cn.easyes.core.conditions.LambdaEsIndexWrapper; import cn.easyes.core.conditions.LambdaEsIndexWrapper;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
@ -137,7 +137,7 @@ public interface BaseEsMapper<T> {
* @param pageSize 每页条数 * @param pageSize 每页条数
* @return 指定的返回类型 * @return 指定的返回类型
*/ */
PageInfo<T> pageQuery(LambdaEsQueryWrapper<T> wrapper, Integer pageNum, Integer pageSize); EsPageInfo<T> pageQuery(LambdaEsQueryWrapper<T> wrapper, Integer pageNum, Integer pageSize);
/** /**
* searchAfter类型分页 * searchAfter类型分页

View File

@ -536,7 +536,7 @@ public interface Func<Children, R> extends Serializable {
} }
default Children avg(boolean enablePipeline, String column) { default Children avg(boolean enablePipeline, String column) {
return avg(true, enablePipeline, column, column); return avg(true, enablePipeline, column);
} }
default Children avg(String... columns) { default Children avg(String... columns) {
@ -570,8 +570,8 @@ public interface Func<Children, R> extends Serializable {
return min(true, enablePipeline, column); return min(true, enablePipeline, column);
} }
default Children min(R... column) { default Children min(R... columns) {
return min(true, true, column); return min(true, true, columns);
} }
default Children min(boolean enablePipeline, R... columns) { default Children min(boolean enablePipeline, R... columns) {

View File

@ -1,6 +1,6 @@
package cn.easyes.core.conditions.interfaces; package cn.easyes.core.conditions.interfaces;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import cn.easyes.core.toolkit.FieldUtils; import cn.easyes.core.toolkit.FieldUtils;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -21,6 +21,14 @@ public interface Index<Children, R> extends Serializable {
*/ */
Children indexName(String... indexNames); Children indexName(String... indexNames);
/**
* 设置最大返回数
*
* @param maxResultWindow 最大返回数
* @return 泛型
*/
Children maxResultWindow(Integer maxResultWindow);
/** /**
* 设置索引的分片数和副本数 * 设置索引的分片数和副本数
* *

View File

@ -1,7 +1,8 @@
package cn.easyes.core.config; package cn.easyes.core.config;
import cn.easyes.common.enums.FieldStrategy;
import cn.easyes.common.enums.IdType; import cn.easyes.annotation.rely.FieldStrategy;
import cn.easyes.annotation.rely.IdType;
import cn.easyes.common.enums.ProcessIndexStrategyEnum; import cn.easyes.common.enums.ProcessIndexStrategyEnum;
import cn.easyes.common.enums.RefreshPolicy; import cn.easyes.common.enums.RefreshPolicy;
import lombok.Data; import lombok.Data;

View File

@ -1,9 +1,9 @@
package cn.easyes.core.toolkit; package cn.easyes.core.toolkit;
import cn.easyes.annotation.*; import cn.easyes.annotation.*;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.DefaultNestedClass;
import cn.easyes.common.enums.IdType; import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.params.DefaultNestedClass; import cn.easyes.annotation.rely.IdType;
import cn.easyes.common.utils.ClassUtils; import cn.easyes.common.utils.ClassUtils;
import cn.easyes.common.utils.FastJsonUtils; import cn.easyes.common.utils.FastJsonUtils;
import cn.easyes.common.utils.ReflectionKit; import cn.easyes.common.utils.ReflectionKit;
@ -589,6 +589,7 @@ public class EntityInfoHelper {
} else { } else {
indexName = tableName; indexName = tableName;
} }
entityInfo.setMaxResultWindow(table.maxResultWindow());
entityInfo.setAliasName(table.aliasName()); entityInfo.setAliasName(table.aliasName());
entityInfo.setShardsNum(table.shardsNum()); entityInfo.setShardsNum(table.shardsNum());
entityInfo.setReplicasNum(table.replicasNum()); entityInfo.setReplicasNum(table.replicasNum());

View File

@ -1,11 +1,12 @@
package cn.easyes.core.toolkit; package cn.easyes.core.toolkit;
import cn.easyes.common.constants.Analyzer;
import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.DefaultChildClass;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.common.constants.BaseEsConstants;
import cn.easyes.common.enums.FieldType;
import cn.easyes.common.enums.JdkDataTypeEnum; import cn.easyes.common.enums.JdkDataTypeEnum;
import cn.easyes.common.enums.ProcessIndexStrategyEnum; import cn.easyes.common.enums.ProcessIndexStrategyEnum;
import cn.easyes.common.params.DefaultChildClass;
import cn.easyes.common.utils.*; import cn.easyes.common.utils.*;
import cn.easyes.core.biz.*; import cn.easyes.core.biz.*;
import cn.easyes.core.cache.GlobalConfigCache; import cn.easyes.core.cache.GlobalConfigCache;
@ -45,6 +46,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import static cn.easyes.annotation.rely.AnnotationConstants.DEFAULT_ALIAS;
import static cn.easyes.common.constants.BaseEsConstants.*; import static cn.easyes.common.constants.BaseEsConstants.*;
@ -106,6 +108,9 @@ public class IndexUtils {
Settings.Builder settings = Settings.builder(); Settings.Builder settings = Settings.builder();
Optional.ofNullable(indexParam.getShardsNum()).ifPresent(shards -> settings.put(BaseEsConstants.SHARDS_FIELD, shards)); Optional.ofNullable(indexParam.getShardsNum()).ifPresent(shards -> settings.put(BaseEsConstants.SHARDS_FIELD, shards));
Optional.ofNullable(indexParam.getReplicasNum()).ifPresent(replicas -> settings.put(BaseEsConstants.REPLICAS_FIELD, replicas)); Optional.ofNullable(indexParam.getReplicasNum()).ifPresent(replicas -> settings.put(BaseEsConstants.REPLICAS_FIELD, replicas));
// 最大返回个数
Optional.ofNullable(indexParam.getMaxResultWindow()).ifPresent(maxResultWindow -> settings.put(MAX_RESULT_WINDOW_FIELD, maxResultWindow));
createIndexRequest.settings(settings); createIndexRequest.settings(settings);
} else { } else {
// 用户自定义settings // 用户自定义settings
@ -190,18 +195,31 @@ public class IndexUtils {
* @param indexName 索引名 * @param indexName 索引名
*/ */
public static void addDefaultAlias(RestHighLevelClient client, String indexName) { public static void addDefaultAlias(RestHighLevelClient client, String indexName) {
addAliases(client, indexName, DEFAULT_ALIAS);
}
/**
* 添加别名
*
* @param client RestHighLevelClient
* @param indexName 索引名
* @param aliases 别名数组可以是单个
* @return 是否添加成功
*/
public static Boolean addAliases(RestHighLevelClient client, String indexName, String... aliases) {
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
IndicesAliasesRequest.AliasActions aliasActions = IndicesAliasesRequest.AliasActions aliasActions =
new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD); new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
aliasActions.index(indexName); aliasActions.index(indexName);
aliasActions.alias(BaseEsConstants.DEFAULT_ALIAS); aliasActions.aliases(aliases);
indicesAliasesRequest.addAliasAction(aliasActions); indicesAliasesRequest.addAliasAction(aliasActions);
try { try {
client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT); client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
} catch (IOException e) { } catch (IOException e) {
LogUtils.warn("addDefaultAlias exception", e.toString()); LogUtils.warn("addDefaultAlias exception", e.toString());
return Boolean.FALSE;
} }
return Boolean.TRUE;
} }
/** /**
@ -248,12 +266,12 @@ public class IndexUtils {
.flatMap(aliases -> Optional.ofNullable(aliases.get(indexName))) .flatMap(aliases -> Optional.ofNullable(aliases.get(indexName)))
.ifPresent(aliasMetadataList -> .ifPresent(aliasMetadataList ->
aliasMetadataList.forEach(aliasMetadata -> { aliasMetadataList.forEach(aliasMetadata -> {
if (BaseEsConstants.DEFAULT_ALIAS.equals(aliasMetadata.alias())) { if (DEFAULT_ALIAS.equals(aliasMetadata.alias())) {
esIndexInfo.setHasDefaultAlias(Boolean.TRUE); esIndexInfo.setHasDefaultAlias(Boolean.TRUE);
} }
})); }));
// 设置分片及副本数 // 设置分片副本最大返回数等
Optional.ofNullable(getIndexResponse.getSettings()) Optional.ofNullable(getIndexResponse.getSettings())
.flatMap(settingsMap -> Optional.ofNullable(settingsMap.get(indexName))) .flatMap(settingsMap -> Optional.ofNullable(settingsMap.get(indexName)))
.ifPresent(p -> { .ifPresent(p -> {
@ -263,6 +281,9 @@ public class IndexUtils {
String replicasNumStr = p.get(BaseEsConstants.REPLICAS_NUM_KEY); String replicasNumStr = p.get(BaseEsConstants.REPLICAS_NUM_KEY);
Optional.ofNullable(replicasNumStr) Optional.ofNullable(replicasNumStr)
.ifPresent(r -> esIndexInfo.setReplicasNum(Integer.parseInt(r))); .ifPresent(r -> esIndexInfo.setReplicasNum(Integer.parseInt(r)));
String maxResultWindowStr = p.get(MAX_RESULT_WINDOW_FIELD);
Optional.ofNullable(maxResultWindowStr)
.ifPresent(m -> esIndexInfo.setMaxResultWindow(Integer.parseInt(maxResultWindowStr)));
}); });
// 设置mapping信息 // 设置mapping信息
@ -341,7 +362,7 @@ public class IndexUtils {
* @return 索引mapping * @return 索引mapping
*/ */
public static Map<String, Object> initMapping(EntityInfo entityInfo, List<EsIndexParam> indexParamList) { public static Map<String, Object> initMapping(EntityInfo entityInfo, List<EsIndexParam> indexParamList) {
Map<String, Object> mapping = new HashMap<>(1); Map<String, Object> mapping = new HashMap<>(2);
if (CollectionUtils.isEmpty(indexParamList)) { if (CollectionUtils.isEmpty(indexParamList)) {
return mapping; return mapping;
} }
@ -434,9 +455,9 @@ public class IndexUtils {
*/ */
public static boolean changeAliasAtomic(RestHighLevelClient client, String oldIndexName, String releaseIndexName) { public static boolean changeAliasAtomic(RestHighLevelClient client, String oldIndexName, String releaseIndexName) {
IndicesAliasesRequest.AliasActions addIndexAction = new IndicesAliasesRequest.AliasActions( IndicesAliasesRequest.AliasActions addIndexAction = new IndicesAliasesRequest.AliasActions(
IndicesAliasesRequest.AliasActions.Type.ADD).index(releaseIndexName).alias(BaseEsConstants.DEFAULT_ALIAS); IndicesAliasesRequest.AliasActions.Type.ADD).index(releaseIndexName).alias(DEFAULT_ALIAS);
IndicesAliasesRequest.AliasActions removeAction = new IndicesAliasesRequest.AliasActions( IndicesAliasesRequest.AliasActions removeAction = new IndicesAliasesRequest.AliasActions(
IndicesAliasesRequest.AliasActions.Type.REMOVE).index(oldIndexName).alias(BaseEsConstants.DEFAULT_ALIAS); IndicesAliasesRequest.AliasActions.Type.REMOVE).index(oldIndexName).alias(DEFAULT_ALIAS);
IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
indicesAliasesRequest.addAliasAction(addIndexAction); indicesAliasesRequest.addAliasAction(addIndexAction);
@ -485,6 +506,7 @@ public class IndexUtils {
createIndexParam.setShardsNum(entityInfo.getShardsNum()); createIndexParam.setShardsNum(entityInfo.getShardsNum());
createIndexParam.setReplicasNum(entityInfo.getReplicasNum()); createIndexParam.setReplicasNum(entityInfo.getReplicasNum());
createIndexParam.setIndexName(entityInfo.getIndexName()); createIndexParam.setIndexName(entityInfo.getIndexName());
createIndexParam.setMaxResultWindow(entityInfo.getMaxResultWindow());
// 如果有设置新索引名称,则用新索引名覆盖原索引名进行创建 // 如果有设置新索引名称,则用新索引名覆盖原索引名进行创建
Optional.ofNullable(entityInfo.getReleaseIndexName()).ifPresent(createIndexParam::setIndexName); Optional.ofNullable(entityInfo.getReleaseIndexName()).ifPresent(createIndexParam::setIndexName);
@ -565,6 +587,9 @@ public class IndexUtils {
if (!entityInfo.getReplicasNum().equals(esIndexInfo.getReplicasNum())) { if (!entityInfo.getReplicasNum().equals(esIndexInfo.getReplicasNum())) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if (!entityInfo.getMaxResultWindow().equals(esIndexInfo.getMaxResultWindow())) {
return Boolean.TRUE;
}
// 根据当前实体类及自定义注解配置, 生成最新的Mapping信息 // 根据当前实体类及自定义注解配置, 生成最新的Mapping信息
List<EsIndexParam> esIndexParamList = IndexUtils.initIndexParam(entityInfo, entityInfo.getFieldList(), false); List<EsIndexParam> esIndexParamList = IndexUtils.initIndexParam(entityInfo, entityInfo.getFieldList(), false);

View File

@ -1,6 +1,6 @@
package cn.easyes.core.toolkit; package cn.easyes.core.toolkit;
import cn.easyes.core.biz.PageInfo; import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.biz.SAPageInfo; import cn.easyes.core.biz.SAPageInfo;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -24,12 +24,18 @@ public class PageHelper {
* @param <T> 数据类型 * @param <T> 数据类型
* @return 分页信息 * @return 分页信息
*/ */
public static <T> PageInfo<T> getPageInfo(List<T> list, Long total, Integer pageNum, Integer pageSize) { public static <T> EsPageInfo<T> getPageInfo(List<T> list, Long total, Integer pageNum, Integer pageSize) {
PageInfo<T> pageInfo = new PageInfo<>(list); EsPageInfo<T> pageInfo = new EsPageInfo<>(list);
pageInfo.setTotal(total); pageInfo.setTotal(total);
pageInfo.setPageNum(pageNum); pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize); pageInfo.setPageSize(pageSize);
pageInfo.setPages((int) (total % pageSize > 0 ? total / pageSize + 1 : total / pageSize)); pageInfo.setPages((int) (total % pageSize > 0 ? total / pageSize + 1 : total / pageSize));
// 计算导航页
pageInfo.calcNavigatePageNums();
// 计算前后页,第一页,最后一页
pageInfo.calcPage();
// 判断页面边界
pageInfo.judgePageBoundary();
return pageInfo; return pageInfo;
} }

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<relativePath>../easy-es-parent</relativePath> <relativePath>../easy-es-parent</relativePath>
</parent> </parent>
@ -23,6 +23,10 @@
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-annotation</artifactId> <artifactId>easy-es-annotation</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-common</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -6,7 +6,7 @@
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-parent</artifactId> <artifactId>easy-es-parent</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<name>easy-es-parent</name> <name>easy-es-parent</name>
<description>easy use for elastic search</description> <description>easy use for elastic search</description>

View File

@ -9,7 +9,7 @@
<parent> <parent>
<artifactId>easy-es</artifactId> <artifactId>easy-es</artifactId>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<version>1.0.3</version> <version>1.1.1</version>
</parent> </parent>
<properties> <properties>
@ -21,6 +21,7 @@
<dependency> <dependency>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId> <artifactId>easy-es-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -12,7 +12,7 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
/** /**l
* 演示springboot项目实际使用 * 演示springboot项目实际使用
* <p> * <p>
* Copyright © 2021 xpc1024 All Rights Reserved * Copyright © 2021 xpc1024 All Rights Reserved

View File

@ -4,10 +4,10 @@ import cn.easyes.annotation.HighLight;
import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId; import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName; import cn.easyes.annotation.IndexName;
import cn.easyes.common.constants.Analyzer; import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.common.enums.FieldStrategy; import cn.easyes.annotation.rely.FieldStrategy;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.enums.IdType; import cn.easyes.annotation.rely.IdType;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@ -18,7 +18,7 @@ import lombok.experimental.Accessors;
**/ **/
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
@IndexName(value = "easyes_document", shardsNum = 3, replicasNum = 2, keepGlobalPrefix = true) @IndexName(value = "easyes_document", shardsNum = 3, replicasNum = 2, keepGlobalPrefix = true,maxResultWindow = 100)
public class Document { public class Document {
/** /**
* es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型) * es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型)

View File

@ -5,15 +5,37 @@ easy-es:
username: elastic username: elastic
password: WG7WVmuNMtM4GwNYkyWH password: WG7WVmuNMtM4GwNYkyWH
keep-alive-millis: 18000 keep-alive-millis: 18000
# address: 10.20.64.228:9200
# schema: http
# username: elastic
# password: WG7WVmuNMtM4GwNYkyWH
# keep-alive-millis: 18000
global-config: global-config:
process-index-mode: smoothly process-index-mode: manual
async-process-index-blocking: true distributed: false
print-dsl: false # async-process-index-blocking: true
db-config: # print-dsl: false
map-underscore-to-camel-case: true # db-config:
# table-prefix: dev_ # map-underscore-to-camel-case: true
id-type: customize # # table-prefix: dev_
field-strategy: not_empty # id-type: customize
refresh-policy: immediate # field-strategy: not_empty
enable-track-total-hits: true # refresh-policy: immediate
enable-must2-filter: false # enable-track-total-hits: true
# enable-must2-filter: false
dynamic:
datasource:
db1:
address: #填你的es连接地址
username: 'username1'
password: 'pwd'
db2:
address: #填你的es连接地址
username: 'username2'
password: 'pwd2'
management:
health:
elasticsearch:
enabled: false

View File

@ -7,7 +7,7 @@
<parent> <parent>
<artifactId>easy-es</artifactId> <artifactId>easy-es</artifactId>
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<version>1.0.3</version> <version>1.1.1</version>
</parent> </parent>
<artifactId>easy-es-test</artifactId> <artifactId>easy-es-test</artifactId>

View File

@ -1,12 +1,14 @@
package cn.easyes.test.entity; package cn.easyes.test.entity;
import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexName; import cn.easyes.annotation.IndexName;
import cn.easyes.common.constants.Analyzer; import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.params.JoinField; import cn.easyes.annotation.rely.JoinField;
import lombok.Data; import lombok.Data;
/** /**
* es 评论 数据模型 Document的子文档,Document是其父文档 * es 评论 数据模型 Document的子文档,Document是其父文档
* <p> * <p>

View File

@ -1,12 +1,7 @@
package cn.easyes.test.entity; package cn.easyes.test.entity;
import cn.easyes.annotation.*; import cn.easyes.annotation.*;
import cn.easyes.common.constants.Analyzer; import cn.easyes.annotation.rely.*;
import cn.easyes.common.enums.FieldStrategy;
import cn.easyes.common.enums.FieldType;
import cn.easyes.common.enums.HighLightTypeEnum;
import cn.easyes.common.enums.IdType;
import cn.easyes.common.params.JoinField;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;

View File

@ -1,7 +1,7 @@
package cn.easyes.test.entity; package cn.easyes.test.entity;
import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexField;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -26,7 +26,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class Faq { public class Faq {
@IndexField(value = "faq_name",fieldType = FieldType.TEXT) @IndexField(value = "faq_name", fieldType = FieldType.TEXT)
private String faqName; private String faqName;
private String faqAnswer; private String faqAnswer;
} }

View File

@ -1,9 +1,9 @@
package cn.easyes.test.entity; package cn.easyes.test.entity;
import cn.easyes.annotation.IndexField; import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexName;
import cn.easyes.common.constants.Analyzer; import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.FieldType;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;

View File

@ -1,6 +1,6 @@
package cn.easyes.test.agg; package cn.easyes.test.agg;
import cn.easyes.core.biz.PageInfo; import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.test.TestEasyEsApplication; import cn.easyes.test.TestEasyEsApplication;
import cn.easyes.test.entity.Document; import cn.easyes.test.entity.Document;
@ -29,7 +29,7 @@ public class AggTest {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getTitle, "老汉") wrapper.eq(Document::getTitle, "老汉")
.distinct(Document::getSubTitle); .distinct(Document::getSubTitle);
PageInfo<Document> pageInfo = documentMapper.pageQuery(wrapper, 1, 10); EsPageInfo<Document> pageInfo = documentMapper.pageQuery(wrapper, 1, 10);
System.out.println(pageInfo); System.out.println(pageInfo);
} }

View File

@ -1,8 +1,8 @@
package cn.easyes.test.all; package cn.easyes.test.all;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.common.constants.BaseEsConstants;
import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.biz.OrderByParam; import cn.easyes.core.biz.OrderByParam;
import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.biz.SAPageInfo; import cn.easyes.core.biz.SAPageInfo;
import cn.easyes.core.cache.GlobalConfigCache; import cn.easyes.core.cache.GlobalConfigCache;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
@ -526,7 +526,7 @@ public class AllTest {
public void testPageQuery() { public void testPageQuery() {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getCreator, "老汉"); wrapper.match(Document::getCreator, "老汉");
PageInfo<Document> pageInfo = documentMapper.pageQuery(wrapper, 1, 5); EsPageInfo<Document> pageInfo = documentMapper.pageQuery(wrapper, 1, 5);
Assertions.assertEquals(5, pageInfo.getSize()); Assertions.assertEquals(5, pageInfo.getSize());
Assertions.assertEquals(22, pageInfo.getTotal()); Assertions.assertEquals(22, pageInfo.getTotal());
} }

View File

@ -1,7 +1,7 @@
package cn.easyes.test.high; package cn.easyes.test.high;
import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.biz.OrderByParam; import cn.easyes.core.biz.OrderByParam;
import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.biz.SAPageInfo; import cn.easyes.core.biz.SAPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.core.toolkit.EsWrappers; import cn.easyes.core.toolkit.EsWrappers;
@ -122,7 +122,7 @@ public class HighTest {
// 浅分页,适合数据量少于1w的情况 // 浅分页,适合数据量少于1w的情况
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>(); LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(Document::getTitle, "老汉"); wrapper.match(Document::getTitle, "老汉");
PageInfo<Document> documentPageInfo = documentMapper.pageQuery(wrapper, 1, 10); EsPageInfo<Document> documentPageInfo = documentMapper.pageQuery(wrapper, 2, 10);
System.out.println(documentPageInfo); System.out.println(documentPageInfo);
} }

View File

@ -1,12 +1,15 @@
package cn.easyes.test.index; package cn.easyes.test.index;
import cn.easyes.common.constants.Analyzer;
import cn.easyes.common.enums.FieldType; import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.core.conditions.LambdaEsIndexWrapper; import cn.easyes.core.conditions.LambdaEsIndexWrapper;
import cn.easyes.test.TestEasyEsApplication; import cn.easyes.test.TestEasyEsApplication;
import cn.easyes.test.entity.Document; import cn.easyes.test.entity.Document;
import cn.easyes.test.mapper.DocumentMapper; import cn.easyes.test.mapper.DocumentMapper;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse; import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

View File

@ -1,6 +1,6 @@
package cn.easyes.test.join; package cn.easyes.test.join;
import cn.easyes.common.params.JoinField; import cn.easyes.annotation.rely.JoinField;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.core.conditions.LambdaEsUpdateWrapper; import cn.easyes.core.conditions.LambdaEsUpdateWrapper;
import cn.easyes.core.toolkit.FieldUtils; import cn.easyes.core.toolkit.FieldUtils;

View File

@ -1,7 +1,7 @@
package cn.easyes.test.other; package cn.easyes.test.other;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.common.constants.BaseEsConstants; import cn.easyes.common.constants.BaseEsConstants;
import cn.easyes.common.enums.FieldType;
import cn.easyes.core.conditions.LambdaEsIndexWrapper; import cn.easyes.core.conditions.LambdaEsIndexWrapper;
import cn.easyes.core.conditions.LambdaEsQueryWrapper; import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.core.toolkit.EntityInfoHelper; import cn.easyes.core.toolkit.EntityInfoHelper;

View File

@ -6,7 +6,7 @@
<groupId>cn.easy-es</groupId> <groupId>cn.easy-es</groupId>
<artifactId>easy-es</artifactId> <artifactId>easy-es</artifactId>
<version>1.0.3</version> <version>1.1.1</version>
<name>easy-es</name> <name>easy-es</name>
<description>easy use for elastic search</description> <description>easy use for elastic search</description>
@ -19,7 +19,8 @@
<properties> <properties>
<junit.version>5.4.0</junit.version> <junit.version>5.4.0</junit.version>
<spring-boot.version>2.6.10</spring-boot.version> <!-- <spring-boot.version>2.6.10</spring-boot.version>-->
<spring-boot.version>2.7.0</spring-boot.version>
<commons-lang3>3.9</commons-lang3> <commons-lang3>3.9</commons-lang3>
</properties> </properties>
<dependencyManagement> <dependencyManagement>