文章目录
- 🌞 Sun Frame:SpringBoot 的轻量级开发框架(个人开源项目推荐)
- 🌟 亮点功能
- 📦 spring cloud模块概览
- 常用工具
- 🔗 更多信息
- 1.spring-data-es操作ES
- 1.引入依赖
- 2.application.yml配置uris
- 3.SubjectInfoEs.java 创建ES实体类(指定映射和索引)
- 4.SubjectEsRepository.java 依赖注入一个接口(对应于实体类)
- 5.SubjectEsService.java
- 6.SubjectEsServiceImpl.java
- 7.TestFeignController.java 测试
- 8.测试
- 1.请求,注意要加上loginId因为没走网关,不加就会空指针
- 9.添加文档
- 1.SubjectEsService.java
- 2.SubjectEsServiceImpl.java
- 3.TestFeignController.java
- 4.测试
- 10.查找文档
- 1.SubjectEsService.java
- 2.SubjectEsServiceImpl.java
- 3.TestFeignController.java
- 4.测试
- 2.自定义封装ES
- 1.环境准备
- 1.删除上面引入的ES依赖
- 2.删除application.yml中的ES配置
- 3.删除原来的文件
- 4.sun-club-infra 引入原生的ES依赖
- 2.自定义封装集群es连接统一管理
- 1.目录结构
- 2.EsClusterConfig.java ES的集群配置的实体类
- 3.EsConfigProperties.java 读取配置文件的es集群配置
- 3.编写配置文件
- 4.EsRestClient.java Es工具类,将每个集群节点对应的某个客户端放到Map
- 5.启动测试,可以读取到配置信息
- 3.封装ES常用操作(全)
- 1.EsClusterConfig.java ES的集群配置的实体类
- 2.EsConfigProperties.java 读取配置文件的集群配置
- 3.EsIndexInfo.java Es的索引信息
- 4.EsSourceData.java Es源数据
- 5.EsSearchRequest.java Es查询的请求实体类
- 6.EsRestClient.java
- 7.配置文件和依赖同上
🌞 Sun Frame:SpringBoot 的轻量级开发框架(个人开源项目推荐)
轻松高效的现代化开发体验
Sun Frame 是我个人开源的一款基于 SpringBoot 的轻量级框架,专为中小型企业设计。它提供了一种快速、简单且易于扩展的开发方式。
我们的开发文档记录了整个项目从0到1的任何细节,实属不易,请给我们一个Star!🌟
您的支持是我们持续改进的动力。
您的支持是我们持续改进的动力。
🌟 亮点功能
- 组件化开发:灵活选择,简化流程。
- 高性能:通过异步日志和 Redis 缓存提升性能。
- 易扩展:支持多种数据库和消息队列。
📦 spring cloud模块概览
- Nacos 服务:高效的服务注册与发现。
- Feign 远程调用:简化服务间通信。
- 强大网关:路由与限流。
常用工具
- 日志管理:异步处理与链路追踪。
- Redis 集成:支持分布式锁与缓存。
- Swagger 文档:便捷的 API 入口。
- 测试支持:SpringBoot-Test 集成。
- EasyCode:自定义EasyCode模板引擎,一键生成CRUD。
🔗 更多信息
- 开源地址:Gitee Sun Frame
- 详细文档:语雀文档
1.spring-data-es操作ES
1.引入依赖
<!-- ES -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>2.4.2</version>
</dependency>
2.application.yml配置uris
3.SubjectInfoEs.java 创建ES实体类(指定映射和索引)
package com.sunxiansheng.subject.infra.basic.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;import java.util.Date;/*** Description: 题目ES,相当于db中的表结构* @Author sun* @Create 2024/6/17 16:01* @Version 1.0*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "subject_index", createIndex = false) // 指定索引名,这里的createIndex=true,默认自动创建索引
public class SubjectInfoEs {// 使用es必须的@Field(type = FieldType.Long)@Idprivate Long id;@Field(type = FieldType.Text, analyzer = "ik_smart")private String subjectName;@Field(type = FieldType.Text, analyzer = "ik_smart")private String subjectAnswer;@Field(type = FieldType.Text, analyzer = "ik_smart")private String createUser;@Field(type = FieldType.Date, index = false)private Date createTime;
}
4.SubjectEsRepository.java 依赖注入一个接口(对应于实体类)
package com.sunxiansheng.subject.infra.basic.esRepo;import com.sunxiansheng.subject.infra.basic.entity.SubjectInfoEs;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;/*** Description:* @Author sun* @Create 2024/6/17 16:09* @Version 1.0*/
@Component
public interface SubjectEsRepository extends ElasticsearchRepository<SubjectInfoEs, Long> {}
5.SubjectEsService.java
package com.sunxiansheng.subject.infra.basic.service;/*** Description:* @Author sun* @Create 2024/6/17 16:11* @Version 1.0*/
public interface SubjectEsService {void createIndex();void getDocs();void find();void search();
}
6.SubjectEsServiceImpl.java
package com.sunxiansheng.subject.infra.basic.service.impl;import com.sunxiansheng.subject.infra.basic.entity.SubjectInfoEs;
import com.sunxiansheng.subject.infra.basic.service.SubjectEsService;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** Description:* @Author sun* @Create 2024/6/17 16:12* @Version 1.0*/
@Service
public class SubjectEsServiceImpl implements SubjectEsService {@Resourceprivate ElasticsearchRestTemplate elasticsearchRestTemplate;/*** 创建索引*/@Overridepublic void createIndex() {// 得到ES实体类中的索引信息然后创建IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(SubjectInfoEs.class);// 创建索引indexOperations.create();// 绑定映射Document mapping = indexOperations.createMapping(SubjectInfoEs.class);indexOperations.putMapping(mapping);}/****/@Overridepublic void getDocs() {}@Overridepublic void find() {}@Overridepublic void search() {}
}
7.TestFeignController.java 测试
8.测试
1.请求,注意要加上loginId因为没走网关,不加就会空指针
9.添加文档
1.SubjectEsService.java
void addDocs();
2.SubjectEsServiceImpl.java
/*** 添加文档,也就相当于添加一条数据*/
@Override
public void addDocs() {List<SubjectInfoEs> list = new LinkedList<>();// 添加两条数据list.add(new SubjectInfoEs(1L, "Java", "Java", "sun", null));list.add(new SubjectInfoEs(2L, "Spring", "Spring", "sun", null));// 批量添加subjectEsRepository.saveAll(list);
}
3.TestFeignController.java
@RequestMapping("/addDocs")
public void createaddDocsIndex() {subjectEsService.addDocs();
}
4.测试
10.查找文档
1.SubjectEsService.java
void find();
2.SubjectEsServiceImpl.java
/*** 查找列对应的元素*/
@Override
public void find() {NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("subjectName", "Java")).build();SearchHits<SubjectInfoEs> search = elasticsearchRestTemplate.search(nativeSearchQuery, SubjectInfoEs.class);List<SearchHit<SubjectInfoEs>> searchHits = search.getSearchHits();log.info("searchHits:{}", searchHits);}
3.TestFeignController.java
@RequestMapping("/find")
public void find() {subjectEsService.find();
}
4.测试
2.自定义封装ES
1.环境准备
1.删除上面引入的ES依赖
2.删除application.yml中的ES配置
3.删除原来的文件
4.sun-club-infra 引入原生的ES依赖
<!-- 封装自定义ES -->
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.5.2</version>
</dependency>
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.5.2</version>
</dependency>
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.5.2</version>
</dependency>
<!-- 集合工具类,配合封装自定义ES -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version>
</dependency>
2.自定义封装集群es连接统一管理
1.目录结构
2.EsClusterConfig.java ES的集群配置的实体类
package com.sunxiansheng.subject.infra.basic.es;import lombok.Data;import java.io.Serializable;/*** Description: ES的集群配置的实体类* @Author sun* @Create 2024/6/17 17:08* @Version 1.0*/
@Data
public class EsClusterConfig implements Serializable {/*集群名称*/private String name;/*集群节点*/private String nodes;
}
3.EsConfigProperties.java 读取配置文件的es集群配置
package com.sunxiansheng.subject.infra.basic.es;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;/*** Description: 读取配置文件的集群配置* @Author sun* @Create 2024/6/17 17:10* @Version 1.0*/
@Component
@ConfigurationProperties(prefix = "es.cluster") // 这个就相当于将esConfigs对应于配置文件的es.cluster.esConfigs
public class EsConfigProperties {private List<EsClusterConfig> esConfigs = new ArrayList<>();public List<EsClusterConfig> getEsConfigs() {return esConfigs;}public void setEsConfigs(List<EsClusterConfig> esConfigs) {this.esConfigs = esConfigs;}
}
3.编写配置文件
es:cluster:es-configs[0]:name: 98bd89b5ccafnodes: ip:9200
4.EsRestClient.java Es工具类,将每个集群节点对应的某个客户端放到Map
package com.sunxiansheng.subject.infra.basic.es;import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpPost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** Description: Es工具类* @Author sun* @Create 2024/6/17 17:43* @Version 1.0*/
@Component
@Slf4j
public class EsRestClient {/*表示某个集群节点对应的某个客户端的mapRestHighLevelClient:是由集群的ip端口数组生成的*/public static Map<String, RestHighLevelClient> clientMap = new HashMap<>();/*获取配置文件中填写的es.cluster.es-configs的集群信息*/@Resourceprivate EsConfigProperties esConfigProperties;/*** @PostConstruct:注解用于标记一个方法,该方法会在依赖注入完成后自动调用* 这里将从配置文件中读取的集群信息封装到map中*/@PostConstructpublic void initialize() {// 获取集群节点的信息List<EsClusterConfig> esConfigs = esConfigProperties.getEsConfigs();esConfigs.forEach(esClusterConfig -> {String name = esClusterConfig.getName();String nodes = esClusterConfig.getNodes();log.info("initialize.esClusterConfig.name:{},nodes:{}", name, nodes);// 将每个集群的ip端口数组转换为RestHighLevelClientRestHighLevelClient restHighLevelClient = initRestClient(esClusterConfig);if (restHighLevelClient != null) {// 如果不为空就放到clientMap中clientMap.put(name, restHighLevelClient);} else {// 如果为空,打印日志log.error("initialize.esClusterConfig.name:{},nodes:{} is null", name, nodes);}});}/*** 通过一个集群节点来初始化该集群节点对应的客户端* @param esClusterConfig* @return*/private RestHighLevelClient initRestClient(EsClusterConfig esClusterConfig) {// 得到该集群节点的IP端口数组String[] ipPortArr = esClusterConfig.getNodes().split(",");// 初始化一个HttpHost的列表List<HttpHost> httpHostList = new ArrayList<>(ipPortArr.length);// 对每一个ip端口进行处理for (String ipPort : ipPortArr) {// 分割ip端口String[] ipPortInfo = ipPort.split(":");// 校验if (ipPortInfo.length == 2) {// 创建一个HttpPost的对象,存放ip和端口HttpHost httpHost = new HttpHost(ipPortInfo[0], NumberUtils.toInt(ipPortInfo[1]));// 放到list中httpHostList.add(httpHost);}}// 创建一个HttpHost的数组,存放HttpHostHttpHost[] httpHosts = new HttpHost[httpHostList.size()];// httpHostList的数据放到这个数组里httpHostList.toArray(httpHosts);// 得到RestClientRestClientBuilder builder = RestClient.builder(httpHosts);// 升级一下RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);return restHighLevelClient;}}
5.启动测试,可以读取到配置信息
3.封装ES常用操作(全)
1.EsClusterConfig.java ES的集群配置的实体类
package com.sunxiansheng.subject.infra.basic.es;import lombok.Data;import java.io.Serializable;/*** Description: ES的集群配置的实体类* @Author sun* @Create 2024/6/17 17:08* @Version 1.0*/
@Data
public class EsClusterConfig implements Serializable {/*集群名称*/private String name;/*集群节点*/private String nodes;
}
2.EsConfigProperties.java 读取配置文件的集群配置
package com.sunxiansheng.subject.infra.basic.es;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;/*** Description: 读取配置文件的集群配置* @Author sun* @Create 2024/6/17 17:10* @Version 1.0*/
@Component
@ConfigurationProperties(prefix = "es.cluster") // 这个就相当于将esConfigs对应于配置文件的es.cluster.esConfigs
public class EsConfigProperties {private List<EsClusterConfig> esConfigs = new ArrayList<>();public List<EsClusterConfig> getEsConfigs() {return esConfigs;}public void setEsConfigs(List<EsClusterConfig> esConfigs) {this.esConfigs = esConfigs;}
}
3.EsIndexInfo.java Es的索引信息
package com.sunxiansheng.subject.infra.basic.es;import lombok.Data;import java.io.Serializable;/*** Description: Es的索引信息* @Author sun* @Create 2024/6/17 17:17* @Version 1.0*/
@Data
public class EsIndexInfo implements Serializable {/*集群名称*/private String clusterName;/*索引名称*/private String indexName;}
4.EsSourceData.java Es源数据
package com.sunxiansheng.subject.infra.basic.es;import lombok.Data;import java.io.Serializable;
import java.util.Map;/*** Description: Es源数据* @Author sun* @Create 2024/6/17 17:41* @Version 1.0*/
@Data
public class EsSourceData implements Serializable {private String docId;private Map<String, Object> data;}
5.EsSearchRequest.java Es查询的请求实体类
package com.sunxiansheng.subject.infra.basic.es;import lombok.Data;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;/*** Description: Es查询的请求实体类* @Author sun* @Create 2024/6/17 17:20* @Version 1.0*/
@Data
public class EsSearchRequest {/*** 查询条件*/private BoolQueryBuilder bq;/*** 查询字段*/private String[] fields;/*** 页数*/private int from;/*** 条数*/private int size;/*** 需要快照*/private Boolean needScroll;/*** 快照缓存时间*/private Long minutes;/*** 排序字段*/private String sortName;/*** 排序类型*/private SortOrder sortOrder;/*** 高亮builder*/private HighlightBuilder highlightBuilder;}
6.EsRestClient.java
package com.sunxiansheng.subject.infra.basic.es;import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;/*** Description: Es工具类* @Author sun* @Create 2024/6/17 17:43* @Version 1.0*/
@Component
@Slf4j
public class EsRestClient {/*表示某个集群节点对应的某个客户端的mapRestHighLevelClient:是由集群的ip端口数组生成的*/public static Map<String, RestHighLevelClient> clientMap = new HashMap<>();/*获取配置文件中填写的es.cluster.es-configs的集群信息*/@Resourceprivate EsConfigProperties esConfigProperties;/**/private static final RequestOptions COMMON_OPTIONS;/*** 为COMMON_OPTIONS进行静态初始化,在类加载的时候会执行*/static {RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();COMMON_OPTIONS = builder.build();}/*** @PostConstruct:注解用于标记一个方法,该方法会在依赖注入完成后自动调用* 这里将从配置文件中读取的集群信息封装到map中*/@PostConstructpublic void initialize() {// 获取集群节点的信息List<EsClusterConfig> esConfigs = esConfigProperties.getEsConfigs();esConfigs.forEach(esClusterConfig -> {String name = esClusterConfig.getName();String nodes = esClusterConfig.getNodes();log.info("initialize.esClusterConfig.name:{},nodes:{}", name, nodes);// 将每个集群的ip端口数组转换为RestHighLevelClientRestHighLevelClient restHighLevelClient = initRestClient(esClusterConfig);if (restHighLevelClient != null) {// 如果不为空就放到clientMap中clientMap.put(name, restHighLevelClient);} else {// 如果为空,打印日志log.error("initialize.esClusterConfig.name:{},nodes:{} is null", name, nodes);}});}/*** 通过一个集群节点来初始化该集群节点对应的客户端* @param esClusterConfig* @return*/private RestHighLevelClient initRestClient(EsClusterConfig esClusterConfig) {// 得到该集群节点的IP端口数组String[] ipPortArr = esClusterConfig.getNodes().split(",");// 初始化一个HttpHost的列表List<HttpHost> httpHostList = new ArrayList<>(ipPortArr.length);// 对每一个ip端口进行处理for (String ipPort : ipPortArr) {// 分割ip端口String[] ipPortInfo = ipPort.split(":");// 校验if (ipPortInfo.length == 2) {// 创建一个HttpPost的对象,存放ip和端口HttpHost httpHost = new HttpHost(ipPortInfo[0], NumberUtils.toInt(ipPortInfo[1]));// 放到list中httpHostList.add(httpHost);}}// 创建一个HttpHost的数组,存放HttpHostHttpHost[] httpHosts = new HttpHost[httpHostList.size()];// httpHostList的数据放到这个数组里httpHostList.toArray(httpHosts);// 得到RestClientRestClientBuilder builder = RestClient.builder(httpHosts);// 升级一下RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);return restHighLevelClient;}/*** 获取集群名对应的RestHighLevelClient* @param clusterName* @return*/private static RestHighLevelClient getClient(String clusterName) {return clientMap.get(clusterName);}/*** 新增文档(新增一条记录)* @param esIndexInfo 包含集群名和索引名* @param esSourceData 文档id和map类型的data* @return*/public static boolean insertDoc(EsIndexInfo esIndexInfo, EsSourceData esSourceData) {try {// ==========构建表的一条数据==========// 获取es的索引名(表名)对应的请求IndexRequest indexRequest = new IndexRequest(esIndexInfo.getIndexName());// 设置源数据indexRequest.source(esSourceData.getData());// 设置记录的idindexRequest.id(esSourceData.getDocId());// ==========构建表的一条数据==========// 根据集群名获取clientgetClient(esIndexInfo.getClusterName()).index(indexRequest, COMMON_OPTIONS);// 如果没报错就返回truereturn true;} catch (Exception e) {// 打日志log.error("insertDoc.error:{}", e.getMessage(), e);}// 如果报错就返回falsereturn false;}/*** 更新文档(更新记录)* @param esIndexInfo* @param esSourceData* @return*/public static boolean updateDoc(EsIndexInfo esIndexInfo, EsSourceData esSourceData) {try {// 构建更新请求UpdateRequest updateRequest = new UpdateRequest();// 设置索引updateRequest.index(esIndexInfo.getIndexName());// 设置文档updateRequest.id(esSourceData.getDocId());// 设置源数据updateRequest.doc(esSourceData.getData());// 根据集群名获取client,执行更新操作getClient(esIndexInfo.getClusterName()).update(updateRequest, COMMON_OPTIONS);// 如果没报错就返回truereturn true;} catch (Exception e) {// 打日志log.error("updateDoc.error:{}", e.getMessage(), e);}// 如果报错就返回falsereturn false;}/*** 批量更新文档(更新记录)* @param esIndexInfo* @param esSourceDataList* @return*/public static boolean batchUpdateDoc(EsIndexInfo esIndexInfo, List<EsSourceData> esSourceDataList) {try {// 标志位boolean flag = false;// 创建一个多请求对象BulkRequest bulkRequest = new BulkRequest();// 遍历源数据的列表for (EsSourceData esSourceData : esSourceDataList) {// 获取文档idString docId = esSourceData.getDocId();// 构建一个更新请求放到BulkRequest中if (StringUtils.isNotBlank(docId)) {// 构建更新请求UpdateRequest updateRequest = new UpdateRequest();// 设置索引updateRequest.index(esIndexInfo.getIndexName());// 设置文档updateRequest.id(esSourceData.getDocId());// 设置源数据updateRequest.doc(esSourceData.getData());// 将更新请求放到BulkRequestbulkRequest.add(updateRequest);flag = true;}}// 如果flag为true,就说明更新请求已经存放完毕,可以更新了if (flag) {// 根据集群名获取客户端执行批量更新BulkResponse bulk = getClient(esIndexInfo.getClusterName()).bulk(bulkRequest, COMMON_OPTIONS);// 如果有失败的,就返回falseif (bulk.hasFailures()) {return false;}}// 如果没报错就返回truereturn true;} catch (Exception e) {// 打日志log.error("batchUpdateDoc.error:{}", e.getMessage(), e);}// 如果报错就返回falsereturn false;}/*** 删除索引下的所有文档* @param esIndexInfo* @return*/public static boolean delete(EsIndexInfo esIndexInfo) {try {// 根据索引构建一个请求DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(esIndexInfo.getIndexName());// 设置查询deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());// 指定集群得到客户端进行删除操作BulkByScrollResponse response = getClient(esIndexInfo.getClusterName()).deleteByQuery(deleteByQueryRequest, COMMON_OPTIONS);// 获取删除的数量long deleted = response.getDeleted();log.info("deleted.size:{}", deleted);return true;} catch (Exception e) {// 打日志log.error("delete.error:{}", e.getMessage(), e);}return false;}/*** 根据id删除文档* @param esIndexInfo* @return*/public static boolean deleteDoc(EsIndexInfo esIndexInfo, String docId) {try {// 根据索引获取删除请求DeleteRequest deleteRequest = new DeleteRequest(esIndexInfo.getIndexName());// 设置要删除的文档iddeleteRequest.id(docId);// 根据集群名获取客户端,执行删除操作DeleteResponse response = getClient(esIndexInfo.getClusterName()).delete(deleteRequest, COMMON_OPTIONS);// 打日志log.info("deleted.response:{}", JSON.toJSONString(response));return true;} catch (Exception e) {// 打日志log.error("deleteDoc.error:{}", e.getMessage(), e);}return false;}/*** 根据id判断文档是否存在* @param esIndexInfo* @param docId* @return*/public static boolean isExistDocById(EsIndexInfo esIndexInfo, String docId) {try {// 根据索引获取请求GetRequest getRequest = new GetRequest(esIndexInfo.getIndexName());// 设置文档idgetRequest.id(docId);// 根据集群名返回客户端,判断文档是否存在return getClient(esIndexInfo.getClusterName()).exists(getRequest, COMMON_OPTIONS);} catch (Exception e) {// 打日志log.error("isExistDocById.error:{}", e.getMessage(), e);}return false;}/*** 根据id查询文档(一条记录)* @param esIndexInfo* @param docId* @return*/public static Map<String, Object> getDocById(EsIndexInfo esIndexInfo, String docId) {try {// 根据索引获取请求GetRequest getRequest = new GetRequest(esIndexInfo.getIndexName());// 设置文档idgetRequest.id(docId);// 根据集群名返回客户端,根据id查询文档GetResponse response = getClient(esIndexInfo.getClusterName()).get(getRequest, COMMON_OPTIONS);// 获取文档信息Map<String, Object> source = response.getSource();return source;} catch (Exception e) {// 打日志log.error("getDocById.error:{}", e.getMessage(), e);}return null;}/*** 根据id查询指定字段的文档* @param esIndexInfo* @param docId* @param fields* @return*/public static Map<String, Object> getDocById(EsIndexInfo esIndexInfo, String docId, String[] fields) {try {// 根据索引获取请求GetRequest getRequest = new GetRequest(esIndexInfo.getIndexName());// 设置文档idgetRequest.id(docId);// 设置要查询文档的指定字段FetchSourceContext fetchSourceContext = new FetchSourceContext(true, fields, null);getRequest.fetchSourceContext(fetchSourceContext);// 根据集群名返回客户端,根据id查询文档GetResponse response = getClient(esIndexInfo.getClusterName()).get(getRequest, COMMON_OPTIONS);// 获取文档信息Map<String, Object> source = response.getSource();return source;} catch (Exception e) {// 打日志log.error("getDocById.error:{}", e.getMessage(), e);}return null;}/*** 条件查询* @param esIndexInfo* @param esSearchRequest* @return*/public static SearchResponse searchWithTermQuery(EsIndexInfo esIndexInfo, EsSearchRequest esSearchRequest) {try {// 获取BoolQueryBuilderBoolQueryBuilder bq = esSearchRequest.getBq();// 获取查询条件String[] fields = esSearchRequest.getFields();int from = esSearchRequest.getFrom();int size = esSearchRequest.getSize();Boolean needScroll = esSearchRequest.getNeedScroll();Long minutes = esSearchRequest.getMinutes();String sortName = esSearchRequest.getSortName();SortOrder sortOrder = esSearchRequest.getSortOrder();HighlightBuilder highlightBuilder = esSearchRequest.getHighlightBuilder();// 构建查询条件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(bq);// 查询的字段、分页、大小searchSourceBuilder.fetchSource(fields, null).from(from).size(size);// 如果有高亮的就加入条件if (Objects.nonNull(highlightBuilder)) {searchSourceBuilder.highlighter(highlightBuilder);}// 如果有排序字段就加入条件if (StringUtils.isNotBlank(sortName)) {searchSourceBuilder.sort(sortName);}// 降序排序searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));// 构建查询请求SearchRequest searchRequest = new SearchRequest();// 设置查询类型searchRequest.searchType(SearchType.DEFAULT);// 设置索引名searchRequest.indices(esIndexInfo.getIndexName());// 设置查询条件searchRequest.source(searchSourceBuilder);// 需要快照的情况if (needScroll) {Scroll scroll = new Scroll(TimeValue.timeValueMinutes(minutes));searchRequest.scroll(scroll);}// 通过集群名来获取客户端执行查询请求SearchResponse searchResponse = getClient(esIndexInfo.getClusterName()).search(searchRequest, COMMON_OPTIONS);return searchResponse;} catch (Exception e) {// 打日志log.error("searchWithTermQuery.error:{}", e.getMessage(), e);}return null;}}