签入版本
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.xiaomayi</groupId>
|
||||
<artifactId>xiaomayi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>xiaomayi-elasticsearch</artifactId>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
<dependencies>
|
||||
<!-- 核心依赖模块 -->
|
||||
<dependency>
|
||||
<groupId>com.xiaomayi</groupId>
|
||||
<artifactId>xiaomayi-core</artifactId>
|
||||
</dependency>
|
||||
<!-- ES搜索引擎,要和es版本一致 -->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.json</groupId>
|
||||
<artifactId>jakarta.json-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
// +======================================================================
|
||||
// | 小蚂蚁云·企业数字化开发平台 [ 加速数字化转型,构建智能未来 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 核心价值:赋能开发者 · 服务企业 · 共建生态
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2020-2026 小蚂蚁云团队 版权所有
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方平台: https://www.xiaomayicloud.com
|
||||
// +======================================================================
|
||||
// | 【知识产权声明】
|
||||
// | 1. 权属声明:本软件是受《中华人民共和国著作权法》
|
||||
// | 保护的专有财产,完整版权归小蚂蚁云团队所有
|
||||
// | 2. 权利范围:所有知识产权(商标、专利、著作权、
|
||||
// | 商业秘密等)均归小蚂蚁云团队所有,受法律保护
|
||||
// | 3. 授权性质:用户仅可获得有限的、非独占的使用许可
|
||||
// | 4. 禁止行为:未经书面授权,禁止对本软件本身
|
||||
// | 申请任何知识产权
|
||||
// | 5. 权利保留:未在本协议中明示授予的一切权利
|
||||
// | 均由小蚂蚁云团队保留
|
||||
// +======================================================================
|
||||
// | 【授权状态说明】
|
||||
// | 1. 未授权状态:用户未购买有效的商业许可证
|
||||
// | 2. 商业授权:已购买正式商业许可证的用户
|
||||
// +======================================================================
|
||||
// | 【绝对禁止条款】
|
||||
// | 无论授权状态如何,任何用户均严格禁止以下行为:
|
||||
// | 1. 逆向工程:对软件进行反编译、反汇编、技术破解或任何形式的逆向分析
|
||||
// | 2. 版权破坏:移除、修改、遮挡软件中的版权标识、许可控制或技术保护措施
|
||||
// | 3. 非法传播:复制、分发、出租、出借、转售、再许可本软件或其许可权利
|
||||
// | 4. 竞争性使用:利用本软件的代码、架构、设计开发直接竞争的产品或服务
|
||||
// | 5. 侵权使用:将软件用于侵犯第三方知识产权或其他合法权益的用途
|
||||
// +======================================================================
|
||||
// | 【商业授权许可范围】
|
||||
// | 购买有效商业许可证的用户,在遵守上述禁止条款的前提下,获得:
|
||||
// | 1. 使用权利:在许可证约定的项目数量、开发者数量范围内使用本软件
|
||||
// | 2. 技术支持:商业授权用户可以获取本地开发部署的技术支持服务
|
||||
// | 3. 版本更新:在许可证有效期内获取软件更新、安全补丁和功能增强
|
||||
// | 4. 商业部署:将基于本软件开发的应用程序用于商业运营和生产环境
|
||||
// +======================================================================
|
||||
// | 【许可证获取】
|
||||
// | 1. 获取途径:仅通过小蚂蚁云官方渠道购买(官网、授权代理商),购买记录凭证
|
||||
// | 2. 转让限制:许可证与购买主体绑定,未经书面同意不得转让
|
||||
// | 3. 终止情形:许可证到期、违反协议、破产清算等情况下自动终止
|
||||
// +======================================================================
|
||||
// | 【合法使用要求】
|
||||
// | 1. 仅限用于合法合规的软件研发
|
||||
// | 2. 严禁用于任何违法、侵权或恶意行为
|
||||
// | 3. 严禁违反我国法律法规和政策
|
||||
// +======================================================================
|
||||
// | 【免责声明】
|
||||
// | 因使用本框架导致的以下情况,我们概不负责:
|
||||
// | 1. 项目研发中的意外、疏忽、违约等风险
|
||||
// | 2. 侵犯第三方知识产权或其他合法权益
|
||||
// | 3. 任何直接、间接、附带或衍生一切损失
|
||||
// | 4. 用户违规违法行为的一切法律后果
|
||||
// +======================================================================
|
||||
// | 【协议重要提示】
|
||||
// | 1. 本声明是《小蚂蚁云最终用户许可协议》的摘要版本
|
||||
// | 2. 下载、安装或使用本软件即表示您接受全部协议条款
|
||||
// | 3. 我们保留单方面修改本声明的权利,修改后的版本在官网公布即生效
|
||||
// | 4. 用户有义务定期查看最新版本,继续使用视为接受修改
|
||||
// +======================================================================
|
||||
// | 【侵权举报与合规】
|
||||
// | 1. 知识产权侵权举报:请联系官方客服
|
||||
// | 2. 许可证合规咨询:xiaomayicloud@163.com
|
||||
// | 3. 违规使用将终止授权并追究责任
|
||||
// | 4. 无条件配合司法机关调查取证
|
||||
// | 5. 保留一切法律追诉权利
|
||||
// | 6. 对有效举报和合规建议,我们将给予感谢和奖励
|
||||
// +======================================================================
|
||||
|
||||
package com.xiaomayi.elasticsearch.config;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
||||
import co.elastic.clients.transport.ElasticsearchTransport;
|
||||
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
||||
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.client.config.RequestConfig;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ES搜索客户端配置
|
||||
* </p>
|
||||
*
|
||||
* @author 小蚂蚁云团队
|
||||
* @since 2024-05-31
|
||||
*/
|
||||
@Configuration
|
||||
public class ElasticSearchClientConfig {
|
||||
|
||||
@Autowired
|
||||
private ElasticSearchConfig elasticSearchConfig;
|
||||
|
||||
/**
|
||||
* 注册ES对象
|
||||
*
|
||||
* @return 返回结果
|
||||
*/
|
||||
@Bean
|
||||
public ElasticsearchClient client() {
|
||||
HttpHost[] httpHosts = toHttpHost();
|
||||
// 设置账号密码
|
||||
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(
|
||||
AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchConfig.getUsername(), elasticSearchConfig.getPassword()));
|
||||
|
||||
RestClientBuilder builder = RestClient.builder(httpHosts);
|
||||
builder.setRequestConfigCallback(
|
||||
new RestClientBuilder.RequestConfigCallback() {
|
||||
@Override
|
||||
public RequestConfig.Builder customizeRequestConfig(
|
||||
RequestConfig.Builder requestConfigBuilder) {
|
||||
return requestConfigBuilder.setSocketTimeout(60000).setConnectTimeout(5000);
|
||||
}
|
||||
});
|
||||
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
|
||||
@Override
|
||||
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
|
||||
return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
}
|
||||
});
|
||||
RestClient restClient = builder.build();
|
||||
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
|
||||
return new ElasticsearchClient(transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务主机处理
|
||||
*
|
||||
* @return 返回结果
|
||||
*/
|
||||
private HttpHost[] toHttpHost() {
|
||||
if (!StringUtils.hasLength(elasticSearchConfig.getUris())) {
|
||||
throw new RuntimeException("invalid elasticsearch configuration. elasticsearch.hosts不能为空!");
|
||||
}
|
||||
// 多个IP逗号隔开
|
||||
String[] hostArray = elasticSearchConfig.getUris().split(",");
|
||||
HttpHost[] httpHosts = new HttpHost[hostArray.length];
|
||||
HttpHost httpHost;
|
||||
for (int i = 0; i < hostArray.length; i++) {
|
||||
String[] strings = hostArray[i].split(":");
|
||||
httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]));
|
||||
httpHosts[i] = httpHost;
|
||||
}
|
||||
return httpHosts;
|
||||
}
|
||||
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
// +======================================================================
|
||||
// | 小蚂蚁云·企业数字化开发平台 [ 加速数字化转型,构建智能未来 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 核心价值:赋能开发者 · 服务企业 · 共建生态
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2020-2026 小蚂蚁云团队 版权所有
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方平台: https://www.xiaomayicloud.com
|
||||
// +======================================================================
|
||||
// | 【知识产权声明】
|
||||
// | 1. 权属声明:本软件是受《中华人民共和国著作权法》
|
||||
// | 保护的专有财产,完整版权归小蚂蚁云团队所有
|
||||
// | 2. 权利范围:所有知识产权(商标、专利、著作权、
|
||||
// | 商业秘密等)均归小蚂蚁云团队所有,受法律保护
|
||||
// | 3. 授权性质:用户仅可获得有限的、非独占的使用许可
|
||||
// | 4. 禁止行为:未经书面授权,禁止对本软件本身
|
||||
// | 申请任何知识产权
|
||||
// | 5. 权利保留:未在本协议中明示授予的一切权利
|
||||
// | 均由小蚂蚁云团队保留
|
||||
// +======================================================================
|
||||
// | 【授权状态说明】
|
||||
// | 1. 未授权状态:用户未购买有效的商业许可证
|
||||
// | 2. 商业授权:已购买正式商业许可证的用户
|
||||
// +======================================================================
|
||||
// | 【绝对禁止条款】
|
||||
// | 无论授权状态如何,任何用户均严格禁止以下行为:
|
||||
// | 1. 逆向工程:对软件进行反编译、反汇编、技术破解或任何形式的逆向分析
|
||||
// | 2. 版权破坏:移除、修改、遮挡软件中的版权标识、许可控制或技术保护措施
|
||||
// | 3. 非法传播:复制、分发、出租、出借、转售、再许可本软件或其许可权利
|
||||
// | 4. 竞争性使用:利用本软件的代码、架构、设计开发直接竞争的产品或服务
|
||||
// | 5. 侵权使用:将软件用于侵犯第三方知识产权或其他合法权益的用途
|
||||
// +======================================================================
|
||||
// | 【商业授权许可范围】
|
||||
// | 购买有效商业许可证的用户,在遵守上述禁止条款的前提下,获得:
|
||||
// | 1. 使用权利:在许可证约定的项目数量、开发者数量范围内使用本软件
|
||||
// | 2. 技术支持:商业授权用户可以获取本地开发部署的技术支持服务
|
||||
// | 3. 版本更新:在许可证有效期内获取软件更新、安全补丁和功能增强
|
||||
// | 4. 商业部署:将基于本软件开发的应用程序用于商业运营和生产环境
|
||||
// +======================================================================
|
||||
// | 【许可证获取】
|
||||
// | 1. 获取途径:仅通过小蚂蚁云官方渠道购买(官网、授权代理商),购买记录凭证
|
||||
// | 2. 转让限制:许可证与购买主体绑定,未经书面同意不得转让
|
||||
// | 3. 终止情形:许可证到期、违反协议、破产清算等情况下自动终止
|
||||
// +======================================================================
|
||||
// | 【合法使用要求】
|
||||
// | 1. 仅限用于合法合规的软件研发
|
||||
// | 2. 严禁用于任何违法、侵权或恶意行为
|
||||
// | 3. 严禁违反我国法律法规和政策
|
||||
// +======================================================================
|
||||
// | 【免责声明】
|
||||
// | 因使用本框架导致的以下情况,我们概不负责:
|
||||
// | 1. 项目研发中的意外、疏忽、违约等风险
|
||||
// | 2. 侵犯第三方知识产权或其他合法权益
|
||||
// | 3. 任何直接、间接、附带或衍生一切损失
|
||||
// | 4. 用户违规违法行为的一切法律后果
|
||||
// +======================================================================
|
||||
// | 【协议重要提示】
|
||||
// | 1. 本声明是《小蚂蚁云最终用户许可协议》的摘要版本
|
||||
// | 2. 下载、安装或使用本软件即表示您接受全部协议条款
|
||||
// | 3. 我们保留单方面修改本声明的权利,修改后的版本在官网公布即生效
|
||||
// | 4. 用户有义务定期查看最新版本,继续使用视为接受修改
|
||||
// +======================================================================
|
||||
// | 【侵权举报与合规】
|
||||
// | 1. 知识产权侵权举报:请联系官方客服
|
||||
// | 2. 许可证合规咨询:xiaomayicloud@163.com
|
||||
// | 3. 违规使用将终止授权并追究责任
|
||||
// | 4. 无条件配合司法机关调查取证
|
||||
// | 5. 保留一切法律追诉权利
|
||||
// | 6. 对有效举报和合规建议,我们将给予感谢和奖励
|
||||
// +======================================================================
|
||||
|
||||
package com.xiaomayi.elasticsearch.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ES搜索配置类
|
||||
* </p>
|
||||
*
|
||||
* @author 小蚂蚁云团队
|
||||
* @since 2024-05-31
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "elasticsearch")
|
||||
public class ElasticSearchConfig {
|
||||
|
||||
/**
|
||||
* 服务器主机
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 服务端口
|
||||
*/
|
||||
private String port;
|
||||
|
||||
/**
|
||||
* 登录账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 登录密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* URI地址,多个逗号分割
|
||||
*/
|
||||
private String uris;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(String port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUris() {
|
||||
return uris;
|
||||
}
|
||||
|
||||
public void setUris(String uris) {
|
||||
this.uris = uris;
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
// +======================================================================
|
||||
// | 小蚂蚁云·企业数字化开发平台 [ 加速数字化转型,构建智能未来 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 核心价值:赋能开发者 · 服务企业 · 共建生态
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2020-2026 小蚂蚁云团队 版权所有
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方平台: https://www.xiaomayicloud.com
|
||||
// +======================================================================
|
||||
// | 【知识产权声明】
|
||||
// | 1. 权属声明:本软件是受《中华人民共和国著作权法》
|
||||
// | 保护的专有财产,完整版权归小蚂蚁云团队所有
|
||||
// | 2. 权利范围:所有知识产权(商标、专利、著作权、
|
||||
// | 商业秘密等)均归小蚂蚁云团队所有,受法律保护
|
||||
// | 3. 授权性质:用户仅可获得有限的、非独占的使用许可
|
||||
// | 4. 禁止行为:未经书面授权,禁止对本软件本身
|
||||
// | 申请任何知识产权
|
||||
// | 5. 权利保留:未在本协议中明示授予的一切权利
|
||||
// | 均由小蚂蚁云团队保留
|
||||
// +======================================================================
|
||||
// | 【授权状态说明】
|
||||
// | 1. 未授权状态:用户未购买有效的商业许可证
|
||||
// | 2. 商业授权:已购买正式商业许可证的用户
|
||||
// +======================================================================
|
||||
// | 【绝对禁止条款】
|
||||
// | 无论授权状态如何,任何用户均严格禁止以下行为:
|
||||
// | 1. 逆向工程:对软件进行反编译、反汇编、技术破解或任何形式的逆向分析
|
||||
// | 2. 版权破坏:移除、修改、遮挡软件中的版权标识、许可控制或技术保护措施
|
||||
// | 3. 非法传播:复制、分发、出租、出借、转售、再许可本软件或其许可权利
|
||||
// | 4. 竞争性使用:利用本软件的代码、架构、设计开发直接竞争的产品或服务
|
||||
// | 5. 侵权使用:将软件用于侵犯第三方知识产权或其他合法权益的用途
|
||||
// +======================================================================
|
||||
// | 【商业授权许可范围】
|
||||
// | 购买有效商业许可证的用户,在遵守上述禁止条款的前提下,获得:
|
||||
// | 1. 使用权利:在许可证约定的项目数量、开发者数量范围内使用本软件
|
||||
// | 2. 技术支持:商业授权用户可以获取本地开发部署的技术支持服务
|
||||
// | 3. 版本更新:在许可证有效期内获取软件更新、安全补丁和功能增强
|
||||
// | 4. 商业部署:将基于本软件开发的应用程序用于商业运营和生产环境
|
||||
// +======================================================================
|
||||
// | 【许可证获取】
|
||||
// | 1. 获取途径:仅通过小蚂蚁云官方渠道购买(官网、授权代理商),购买记录凭证
|
||||
// | 2. 转让限制:许可证与购买主体绑定,未经书面同意不得转让
|
||||
// | 3. 终止情形:许可证到期、违反协议、破产清算等情况下自动终止
|
||||
// +======================================================================
|
||||
// | 【合法使用要求】
|
||||
// | 1. 仅限用于合法合规的软件研发
|
||||
// | 2. 严禁用于任何违法、侵权或恶意行为
|
||||
// | 3. 严禁违反我国法律法规和政策
|
||||
// +======================================================================
|
||||
// | 【免责声明】
|
||||
// | 因使用本框架导致的以下情况,我们概不负责:
|
||||
// | 1. 项目研发中的意外、疏忽、违约等风险
|
||||
// | 2. 侵犯第三方知识产权或其他合法权益
|
||||
// | 3. 任何直接、间接、附带或衍生一切损失
|
||||
// | 4. 用户违规违法行为的一切法律后果
|
||||
// +======================================================================
|
||||
// | 【协议重要提示】
|
||||
// | 1. 本声明是《小蚂蚁云最终用户许可协议》的摘要版本
|
||||
// | 2. 下载、安装或使用本软件即表示您接受全部协议条款
|
||||
// | 3. 我们保留单方面修改本声明的权利,修改后的版本在官网公布即生效
|
||||
// | 4. 用户有义务定期查看最新版本,继续使用视为接受修改
|
||||
// +======================================================================
|
||||
// | 【侵权举报与合规】
|
||||
// | 1. 知识产权侵权举报:请联系官方客服
|
||||
// | 2. 许可证合规咨询:xiaomayicloud@163.com
|
||||
// | 3. 违规使用将终止授权并追究责任
|
||||
// | 4. 无条件配合司法机关调查取证
|
||||
// | 5. 保留一切法律追诉权利
|
||||
// | 6. 对有效举报和合规建议,我们将给予感谢和奖励
|
||||
// +======================================================================
|
||||
|
||||
package com.xiaomayi.elasticsearch.dto;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import com.xiaomayi.core.utils.StringUtils;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ES查询DTO
|
||||
* </p>
|
||||
*
|
||||
* @author 小蚂蚁云团队
|
||||
* @since 2024-05-31
|
||||
*/
|
||||
@Data
|
||||
public class ESQueryDTO {
|
||||
|
||||
/**
|
||||
* 索引名称
|
||||
*/
|
||||
private String indexName;
|
||||
|
||||
/**
|
||||
* 关键字属性
|
||||
*/
|
||||
private String field;
|
||||
|
||||
/**
|
||||
* 关键字值
|
||||
*/
|
||||
private String word;
|
||||
|
||||
/**
|
||||
* 关键字值数组
|
||||
*/
|
||||
private List<String> words;
|
||||
|
||||
/**
|
||||
* 起始行
|
||||
*/
|
||||
private Integer from;
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
*/
|
||||
private Integer index;
|
||||
|
||||
/**
|
||||
* 分页条数
|
||||
*/
|
||||
private Integer size;
|
||||
|
||||
/**
|
||||
* 排序字段
|
||||
*/
|
||||
private String order;
|
||||
|
||||
/**
|
||||
* 排序方式:Asc/Desc
|
||||
*/
|
||||
private String orderType;
|
||||
|
||||
/**
|
||||
* 时间字段
|
||||
*/
|
||||
private String dateField;
|
||||
|
||||
/**
|
||||
* 时间范围-开始时间
|
||||
*/
|
||||
private String startTime;
|
||||
|
||||
/**
|
||||
* 时间范围-开始时间
|
||||
*/
|
||||
private String endTime;
|
||||
|
||||
public String getOrderType() {
|
||||
if (StringUtils.isBlank(orderType)) {
|
||||
orderType = SortOrder.Desc.name();
|
||||
}
|
||||
return orderType;
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return size == 0 ? 30 : size;
|
||||
}
|
||||
|
||||
public Integer getFrom() {
|
||||
return getIndex() != 0 ? ((getIndex() - 1) * getSize()) : 0;
|
||||
}
|
||||
|
||||
public Integer getIndex() {
|
||||
return null == index ? 0 : index;
|
||||
}
|
||||
|
||||
public String getStartTime(int offset) {
|
||||
if (StringUtils.isBlank(startTime)) {
|
||||
startTime = DateUtil.format(DateUtil.offsetDay(new Date(), offset), "yyyy-MM-dd 00:00:00");
|
||||
return String.valueOf(DateUtil.parse(startTime, "yyyy-MM-dd 00:00:00").getTime());
|
||||
}
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public String getEndTime() {
|
||||
if (StringUtils.isBlank(endTime)) {
|
||||
endTime = String.valueOf(System.currentTimeMillis());
|
||||
}
|
||||
return endTime;
|
||||
}
|
||||
|
||||
}
|
||||
+500
@@ -0,0 +1,500 @@
|
||||
// +======================================================================
|
||||
// | 小蚂蚁云·企业数字化开发平台 [ 加速数字化转型,构建智能未来 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 核心价值:赋能开发者 · 服务企业 · 共建生态
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2020-2026 小蚂蚁云团队 版权所有
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方平台: https://www.xiaomayicloud.com
|
||||
// +======================================================================
|
||||
// | 【知识产权声明】
|
||||
// | 1. 权属声明:本软件是受《中华人民共和国著作权法》
|
||||
// | 保护的专有财产,完整版权归小蚂蚁云团队所有
|
||||
// | 2. 权利范围:所有知识产权(商标、专利、著作权、
|
||||
// | 商业秘密等)均归小蚂蚁云团队所有,受法律保护
|
||||
// | 3. 授权性质:用户仅可获得有限的、非独占的使用许可
|
||||
// | 4. 禁止行为:未经书面授权,禁止对本软件本身
|
||||
// | 申请任何知识产权
|
||||
// | 5. 权利保留:未在本协议中明示授予的一切权利
|
||||
// | 均由小蚂蚁云团队保留
|
||||
// +======================================================================
|
||||
// | 【授权状态说明】
|
||||
// | 1. 未授权状态:用户未购买有效的商业许可证
|
||||
// | 2. 商业授权:已购买正式商业许可证的用户
|
||||
// +======================================================================
|
||||
// | 【绝对禁止条款】
|
||||
// | 无论授权状态如何,任何用户均严格禁止以下行为:
|
||||
// | 1. 逆向工程:对软件进行反编译、反汇编、技术破解或任何形式的逆向分析
|
||||
// | 2. 版权破坏:移除、修改、遮挡软件中的版权标识、许可控制或技术保护措施
|
||||
// | 3. 非法传播:复制、分发、出租、出借、转售、再许可本软件或其许可权利
|
||||
// | 4. 竞争性使用:利用本软件的代码、架构、设计开发直接竞争的产品或服务
|
||||
// | 5. 侵权使用:将软件用于侵犯第三方知识产权或其他合法权益的用途
|
||||
// +======================================================================
|
||||
// | 【商业授权许可范围】
|
||||
// | 购买有效商业许可证的用户,在遵守上述禁止条款的前提下,获得:
|
||||
// | 1. 使用权利:在许可证约定的项目数量、开发者数量范围内使用本软件
|
||||
// | 2. 技术支持:商业授权用户可以获取本地开发部署的技术支持服务
|
||||
// | 3. 版本更新:在许可证有效期内获取软件更新、安全补丁和功能增强
|
||||
// | 4. 商业部署:将基于本软件开发的应用程序用于商业运营和生产环境
|
||||
// +======================================================================
|
||||
// | 【许可证获取】
|
||||
// | 1. 获取途径:仅通过小蚂蚁云官方渠道购买(官网、授权代理商),购买记录凭证
|
||||
// | 2. 转让限制:许可证与购买主体绑定,未经书面同意不得转让
|
||||
// | 3. 终止情形:许可证到期、违反协议、破产清算等情况下自动终止
|
||||
// +======================================================================
|
||||
// | 【合法使用要求】
|
||||
// | 1. 仅限用于合法合规的软件研发
|
||||
// | 2. 严禁用于任何违法、侵权或恶意行为
|
||||
// | 3. 严禁违反我国法律法规和政策
|
||||
// +======================================================================
|
||||
// | 【免责声明】
|
||||
// | 因使用本框架导致的以下情况,我们概不负责:
|
||||
// | 1. 项目研发中的意外、疏忽、违约等风险
|
||||
// | 2. 侵犯第三方知识产权或其他合法权益
|
||||
// | 3. 任何直接、间接、附带或衍生一切损失
|
||||
// | 4. 用户违规违法行为的一切法律后果
|
||||
// +======================================================================
|
||||
// | 【协议重要提示】
|
||||
// | 1. 本声明是《小蚂蚁云最终用户许可协议》的摘要版本
|
||||
// | 2. 下载、安装或使用本软件即表示您接受全部协议条款
|
||||
// | 3. 我们保留单方面修改本声明的权利,修改后的版本在官网公布即生效
|
||||
// | 4. 用户有义务定期查看最新版本,继续使用视为接受修改
|
||||
// +======================================================================
|
||||
// | 【侵权举报与合规】
|
||||
// | 1. 知识产权侵权举报:请联系官方客服
|
||||
// | 2. 许可证合规咨询:xiaomayicloud@163.com
|
||||
// | 3. 违规使用将终止授权并追究责任
|
||||
// | 4. 无条件配合司法机关调查取证
|
||||
// | 5. 保留一切法律追诉权利
|
||||
// | 6. 对有效举报和合规建议,我们将给予感谢和奖励
|
||||
// +======================================================================
|
||||
|
||||
package com.xiaomayi.elasticsearch.utils;
|
||||
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.SortOptions;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import co.elastic.clients.elasticsearch.core.*;
|
||||
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
|
||||
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
|
||||
import co.elastic.clients.elasticsearch.core.search.Hit;
|
||||
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
|
||||
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
|
||||
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
|
||||
import co.elastic.clients.transport.endpoints.BooleanResponse;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.xiaomayi.core.utils.StringUtils;
|
||||
import com.xiaomayi.elasticsearch.dto.ESQueryDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ES搜索工具类
|
||||
* </p>
|
||||
*
|
||||
* @author 小蚂蚁云团队
|
||||
* @since 2024-05-31
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ElasticClientUtils<T> {
|
||||
|
||||
ElasticsearchClient client = SpringUtil.getBean(ElasticsearchClient.class);
|
||||
|
||||
/**
|
||||
* 创建ES索引
|
||||
*
|
||||
* @param indexName 索引名称
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public boolean createIndex(String indexName) throws IOException {
|
||||
try {
|
||||
// 判断索引是否已存在
|
||||
if (indexExists(indexName)) {
|
||||
// 存在直接返回true
|
||||
return true;
|
||||
}
|
||||
CreateIndexResponse createIndexResponse = client
|
||||
.indices()
|
||||
.create(c -> c.index(indexName));
|
||||
// 返回结果
|
||||
return createIndexResponse.acknowledged();
|
||||
} catch (IOException e) {
|
||||
log.error("向es中创建索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断索引是否存在
|
||||
*
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public boolean indexExists(String indexName) throws IOException {
|
||||
try {
|
||||
// 查询索引是否存在
|
||||
BooleanResponse exists = client.indices().exists(e -> e.index(indexName));
|
||||
// 返回结果
|
||||
return exists.value();
|
||||
} catch (IOException e) {
|
||||
log.error("向es中检测索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
|
||||
throw new RuntimeException("向es中检测索引【" + indexName + "】出错");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询索引
|
||||
*
|
||||
* @param indexName 索引名称
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public void queryIndex(String indexName) throws IOException {
|
||||
try {
|
||||
GetIndexResponse getIndexResponse = client.indices().get(c -> c.index(indexName));
|
||||
System.out.println(getIndexResponse);
|
||||
} catch (IOException e) {
|
||||
log.error("向es中添加document出错:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除索引
|
||||
*
|
||||
* @param indexName 索引名称
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public boolean deleteIndex(String indexName) throws IOException {
|
||||
try {
|
||||
DeleteIndexResponse deleteIndexResponse = client.indices().delete(c -> c.index(indexName));
|
||||
return deleteIndexResponse.acknowledged();
|
||||
} catch (IOException e) {
|
||||
log.error("向es中删除索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文档
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param docId 文档ID
|
||||
* @param document 文档对象
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public IndexResponse addDocument(String indexName, String docId, T document) throws IOException {
|
||||
try {
|
||||
// 判断索引是否存在
|
||||
if (!this.indexExists(indexName)) {
|
||||
// 不存在索引则创建
|
||||
this.createIndex(indexName);
|
||||
}
|
||||
IndexResponse indexResponse = client.index(i -> i
|
||||
.index(indexName)
|
||||
// 设置文档ID,可以传null
|
||||
.id(docId)
|
||||
// 传入对象
|
||||
.document(document));
|
||||
return indexResponse;
|
||||
} catch (IOException e) {
|
||||
log.error("向es中添加document出错:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入文档
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param bulkOperationList 对象列表
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public BulkResponse bulkAddDocument(String indexName, List<BulkOperation> bulkOperationList) throws IOException {
|
||||
BulkResponse bulkResponse = client.bulk(b -> b
|
||||
.index(indexName)
|
||||
.operations(bulkOperationList));
|
||||
return bulkResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量添加
|
||||
*
|
||||
* @param dataList 添加的数量集合
|
||||
* @param indexName indexName
|
||||
*/
|
||||
public void batchAddDocuments(List<T> dataList, String indexName) throws IOException {
|
||||
try {
|
||||
// 判断索引是否存在
|
||||
if (!this.indexExists(indexName)) {
|
||||
// 不存在索引则创建
|
||||
this.createIndex(indexName);
|
||||
}
|
||||
// 实例化批量请求
|
||||
BulkRequest.Builder builder = new BulkRequest.Builder();
|
||||
// 遍历数据源并加入请求对象
|
||||
dataList.forEach(data -> builder.operations(op -> op
|
||||
.index(idx -> idx
|
||||
.index(indexName)
|
||||
// 如果不需要指定 id,可以省略 id 字段
|
||||
// .id(null)
|
||||
.document(data)
|
||||
))
|
||||
);
|
||||
// 批量添加文档
|
||||
BulkResponse result = client.bulk(builder.build());
|
||||
if (result.errors()) {
|
||||
log.error("Bulk had errors");
|
||||
for (BulkResponseItem item : result.items()) {
|
||||
if (item.error() != null) {
|
||||
log.error(item.error().reason());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("向es中添加document出错:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文档
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param docId 文档ID
|
||||
* @param document 文档对象
|
||||
* @param clazz 文档类
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public UpdateResponse updateDocument(String indexName, String docId, T document, Class<T> clazz) throws IOException {
|
||||
try {
|
||||
UpdateResponse<T> updateResponse = client.update(u -> u
|
||||
.index(indexName)
|
||||
.id(docId)
|
||||
.doc(document), clazz);
|
||||
return updateResponse;
|
||||
} catch (IOException e) {
|
||||
log.error("向es中更新document出错:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文档是否存在
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param docId 文档ID
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public BooleanResponse existDocument(String indexName, String docId) throws IOException {
|
||||
try {
|
||||
BooleanResponse indexResponse = client.exists(e -> e
|
||||
.index(indexName)
|
||||
.id(docId));
|
||||
return indexResponse;
|
||||
} catch (IOException e) {
|
||||
log.error("向es中查询是否存在document出错:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文档
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param docId 文档ID
|
||||
* @param classz 文档对象
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public GetResponse getDocument(String indexName, String docId, Class<T> classz) throws IOException {
|
||||
try {
|
||||
GetResponse<T> getResponse = client.get(g -> g
|
||||
.index(indexName)
|
||||
.id(docId), classz);
|
||||
return getResponse;
|
||||
} catch (IOException e) {
|
||||
log.error("向es中查询document出错:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文档
|
||||
*
|
||||
* @param indexName ES索引
|
||||
* @param docId 文档ID
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public DeleteResponse deleteDocument(String indexName, String docId) throws IOException {
|
||||
try {
|
||||
DeleteResponse deleteResponse = client.delete(d -> d
|
||||
.index(indexName)
|
||||
.id(docId)
|
||||
);
|
||||
return deleteResponse;
|
||||
} catch (IOException e) {
|
||||
log.error("向es中删除document出错:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除文档
|
||||
*
|
||||
* @param indexName 索引名称
|
||||
* @param documentIds 要删除的文档 ID 列表
|
||||
* @return 是否全部删除成功
|
||||
* @throws IOException 如果删除操作失败
|
||||
*/
|
||||
public boolean bulkDeleteDocuments(String indexName, List<String> documentIds) throws IOException {
|
||||
// 构建批量删除请求
|
||||
BulkRequest.Builder builder = new BulkRequest.Builder();
|
||||
for (String docId : documentIds) {
|
||||
builder.operations(op -> op
|
||||
.delete(d -> d
|
||||
.index(indexName)
|
||||
.id(docId)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 执行批量删除
|
||||
BulkResponse response = client.bulk(builder.build());
|
||||
|
||||
// 检查是否有错误
|
||||
if (response.errors()) {
|
||||
for (BulkResponseItem item : response.items()) {
|
||||
if (item.error() != null) {
|
||||
System.err.println("Failed to delete document " + item.id() + ": " + item.error().reason());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
System.out.println("Bulk delete completed successfully!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据索引名称和字段查询文档数据
|
||||
*
|
||||
* @param indexName 索引名称
|
||||
* @param filedValue 查询字段值
|
||||
* @param filedName 查询字段名称
|
||||
*/
|
||||
public List<T> getDocumentPageList(String indexName, String filedName, String filedValue, Class<T> classz) {
|
||||
try {
|
||||
SearchResponse<T> searchResponse = client.search(s -> s
|
||||
.index(indexName)
|
||||
.query(q -> q
|
||||
.match(t -> t
|
||||
.field(filedName)
|
||||
.query(filedValue)
|
||||
)), classz);
|
||||
List<Hit<T>> hitList = searchResponse.hits().hits();
|
||||
List<T> dataList = new ArrayList<>();
|
||||
for (Hit<T> mapHit : hitList) {
|
||||
// 查询结果数据转JSON
|
||||
String json = JSON.toJSONString(mapHit.source());
|
||||
// 查询结果JSON转对象
|
||||
T data = JSON.parseObject(json, classz);
|
||||
// 加入列表
|
||||
dataList.add(data);
|
||||
}
|
||||
return dataList;
|
||||
} catch (IOException e) {
|
||||
log.error("【查询 -> 失败】从es中查询分析后的日志出错,错误信息为:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文档分页数据
|
||||
*
|
||||
* @param esQueryDTO 查询对象
|
||||
* @param classz 实例类名称
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public List<T> getDocumentPage(ESQueryDTO esQueryDTO, Class<T> classz) throws IOException {
|
||||
try {
|
||||
// 排序规则
|
||||
List<SortOptions> sorts = new ArrayList<>();
|
||||
if (StringUtils.isNotBlank(esQueryDTO.getOrder())) {
|
||||
SortOptions sortOptions = SortOptions.of(s -> s
|
||||
.field(f -> f
|
||||
.field(esQueryDTO.getOrder())
|
||||
.order(SortOrder.valueOf(esQueryDTO.getOrderType())))
|
||||
);
|
||||
sorts.add(sortOptions);
|
||||
}
|
||||
// 查询数据源
|
||||
SearchResponse<T> search = client.search(s -> s
|
||||
.index(esQueryDTO.getIndexName())
|
||||
.query(q -> q.match(t -> t
|
||||
.field(esQueryDTO.getField())
|
||||
.query(esQueryDTO.getWord())
|
||||
))
|
||||
.sort(sorts)
|
||||
.from(esQueryDTO.getFrom())
|
||||
.size(esQueryDTO.getSize()),
|
||||
classz);
|
||||
// 查询结果集
|
||||
List<Hit<T>> hitList = search.hits().hits();
|
||||
// 实例化查询数据
|
||||
List<T> dataList = new ArrayList<>();
|
||||
for (Hit<T> mapHit : hitList) {
|
||||
// 查询结果数据转JSON
|
||||
String json = JSON.toJSONString(mapHit.source());
|
||||
// 查询结果JSON转对象
|
||||
T data = JSON.parseObject(json, classz);
|
||||
// 加入列表
|
||||
dataList.add(data);
|
||||
}
|
||||
return dataList;
|
||||
} catch (IOException e) {
|
||||
log.error("【查询 -> 失败】从es中查询分页数据出错,错误信息为:{}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询文档数量
|
||||
*
|
||||
* @param esQueryDTO 查询条件
|
||||
* @return 返回结果
|
||||
* @throws IOException 异常处理
|
||||
*/
|
||||
public long getDocumentCount(ESQueryDTO esQueryDTO) throws IOException {
|
||||
try {
|
||||
CountResponse countResponse = client.count(c -> c
|
||||
.index(esQueryDTO.getIndexName())
|
||||
.query(q -> q.term(t -> t
|
||||
.field(esQueryDTO.getField())
|
||||
.value(esQueryDTO.getWord())
|
||||
)));
|
||||
// 查询总数
|
||||
long count = countResponse.count();
|
||||
// 返回结果
|
||||
return count;
|
||||
} catch (IOException e) {
|
||||
log.error("从es中查询数据总数出错,错误信息为:{}", e.getMessage());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
com.xiaomayi.elasticsearch.config.ElasticSearchConfig
|
||||
com.xiaomayi.elasticsearch.config.ElasticSearchClientConfig
|
||||
Reference in New Issue
Block a user