package com.hhr.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.sort.ScoreSortBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.swing.*;
import java.util.*;
@Component
@Slf4j
public class EsRestClient {
private static Map<String, RestHighLevelClient> clientMap = new HashMap<>();
@Resource
private EsConfigProperties esConfigProperties;
private static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@PostConstruct
public void initialize() {
List<EsClusterConfig> esConfigs = esConfigProperties.getEsConfigs();
for (EsClusterConfig esConfig : esConfigs) {
log.info("initialize.config.name:{},node:{}", esConfig.getName(), esConfig.getNodes());
RestHighLevelClient restHighLevelClient = initRestClient(esConfig);
if (restHighLevelClient != null) {
clientMap.put(esConfig.getName(), restHighLevelClient);
} else {
log.error("config.name:{},node:{}.initError", esConfig.getName(), esConfig.getNodes());
}
}
}
private RestHighLevelClient initRestClient(EsClusterConfig esClusterConfig) {
String[] ipPortArr = esClusterConfig.getNodes().split(",");
List<HttpHost> httpHostList = new ArrayList<>(ipPortArr.length);
for (String ipPort : ipPortArr) {
String[] ipPortInfo = ipPort.split(":");
if (ipPortInfo.length == 2) {
HttpHost httpHost = new HttpHost(ipPortInfo[0], NumberUtils.toInt(ipPortInfo[1]));
httpHostList.add(httpHost);
}
}
HttpHost[] httpHosts = new HttpHost[httpHostList.size()];
httpHostList.toArray(httpHosts);
RestClientBuilder builder = RestClient.builder(httpHosts);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
return restHighLevelClient;
}
private static RestHighLevelClient getClient(String clusterName) {
return clientMap.get(clusterName);
}
public static boolean insertDoc(EsIndexInfo esIndexInfo, EsSourceData esSourceData) {
try {
IndexRequest indexRequest = new IndexRequest(esIndexInfo.getIndexName());
indexRequest.source(esSourceData.getData());
indexRequest.id(esSourceData.getDocId());
getClient(esIndexInfo.getClusterName()).index(indexRequest, COMMON_OPTIONS);
return true;
} catch (Exception e) {
log.error("insertDoc.exception:{}", e.getMessage(), e);
}
return false;
}
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());
getClient(esIndexInfo.getClusterName()).update(updateRequest, COMMON_OPTIONS);
return true;
} catch (Exception e) {
log.error("updateDoc.exception:{}", e.getMessage(), e);
}
return false;
}
public static boolean batchUpdateDoc(EsIndexInfo esIndexInfo, List<EsSourceData> esSourceDataList) {
try {
boolean flag = false;
BulkRequest bulkRequest = new BulkRequest();
for (EsSourceData esSourceData : esSourceDataList) {
String docId = esSourceData.getDocId();
if (StringUtils.isNotBlank(docId)){
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(esIndexInfo.getIndexName());
updateRequest.id(esSourceData.getDocId());
updateRequest.doc(esSourceData.getData());
bulkRequest.add(updateRequest);
flag = true;
}
}
if (flag){
BulkResponse bulk = getClient(esIndexInfo.getClusterName()).bulk(bulkRequest, COMMON_OPTIONS);
if (bulk.hasFailures()){
return false;
}
}
return true;
} catch (Exception e) {
log.error("batchUpdateDoc.exception:{}", e.getMessage(), e);
}
return false;
}
public static boolean delete(EsIndexInfo esIndexInfo) {
try {
DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(esIndexInfo.getIndexName());
deleteByQueryRequest.setQuery(QueryBuilders.matchAllQuery());
BulkByScrollResponse response = getClient(esIndexInfo.getIndexName()).deleteByQuery(
deleteByQueryRequest, COMMON_OPTIONS
);
long deleted = response.getDeleted();
log.info("deleted.exception:{}", deleted);
return true;
} catch (Exception e) {
log.error("delete.exception:{}", e.getMessage(), e);
}
return false;
}
public static boolean deleteDoc(EsIndexInfo esIndexInfo, String docId) {
try {
DeleteRequest deleteRequest = new DeleteRequest(esIndexInfo.getIndexName());
deleteRequest.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.exception:{}", e.getMessage(), e);
}
return false;
}
public static boolean isExistDocById(EsIndexInfo esIndexInfo, String docId) {
try {
GetRequest getRequest = new GetRequest(esIndexInfo.getIndexName());
getRequest.id(docId);
return getClient(esIndexInfo.getClusterName()).exists(getRequest, COMMON_OPTIONS);
} catch (Exception e) {
log.error("isExistDocById.exception:{}", e.getMessage(), e);
}
return false;
}
public static Map<String, Object> getDocById(EsIndexInfo esIndexInfo, String docId) {
try {
GetRequest getRequest = new GetRequest(esIndexInfo.getIndexName());
getRequest.id(docId);
GetResponse response = getClient(esIndexInfo.getClusterName()).get(getRequest, COMMON_
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是基于Java语言的练题社区lt-club设计源码,包含313个文件,包括243个Java源文件、42个XML配置文件、13个YAML配置文件、12个虚拟机配置文件、2个Markdown文件、1个Git忽略文件。lt-club是一个专为程序员打造的练题平台,旨在提供高效的编程练习与交流环境。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java语言的程序员练题社区lt-club设计源码 (312个子文件)
.gitignore 350B
EsRestClient.java 11KB
CodeGenerationCode.java 10KB
SubjectInfoDomainServiceImpl.java 9KB
SubjectCategoryDomainServiceImpl.java 8KB
SubjectEsServiceImpl.java 8KB
SubjectCategoryController.java 7KB
SubjectController.java 7KB
AuthUserDomainServiceImpl.java 7KB
UserController.java 7KB
SubjectLikedController.java 6KB
SubjectLikedDomainServiceImpl.java 6KB
MybatisPlusAllSqlLog.java 5KB
MybatisInterceptor.java 5KB
MysqlDataHandler.java 5KB
SubjectLabelController.java 5KB
SubjectLabelDomainServiceImpl.java 5KB
PermissionController.java 4KB
RedisUtil.java 4KB
SubjectLikedServiceImpl.java 4KB
CallBackController.java 3KB
AuthUser.java 3KB
MinioUtil.java 3KB
RoleController.java 3KB
AuthPermissionDomainServiceImpl.java 3KB
AuthPermission.java 3KB
AuthPermission.java 3KB
IdWorkerUtil.java 3KB
FunctionUtils.java 3KB
StpInterfaceImpl.java 3KB
SubjectInfoDao.java 3KB
RedisUtil.java 3KB
RedisUtil.java 3KB
RedisUtil.java 3KB
SubjectRadio.java 2KB
XxlJobConfig.java 2KB
SubjectInfoServiceImpl.java 2KB
AuthRolePermissionServiceImpl.java 2KB
MultipleTypeHandler.java 2KB
SubjectLabel.java 2KB
BizPutContextHandler.java 2KB
SubjectMapping.java 2KB
RadioTypeHandler.java 2KB
SubjectCategoryServiceImpl.java 2KB
SubjectMultipleServiceImpl.java 2KB
RedisConfig.java 2KB
RedisConfig.java 2KB
RedisConfig.java 2KB
AuthRolePermissionDomainServiceImpl.java 2KB
RedisConfig.java 2KB
SubjectMappingDao.java 2KB
AuthRolePermissionDao.java 2KB
SubjectMappingServiceImpl.java 2KB
SubjectLabelDao.java 2KB
AuthPermissionDao.java 2KB
Context.java 2KB
SubjectJudgeServiceImpl.java 2KB
SubjectLabelServiceImpl.java 2KB
MySQLToJavaTypeConverter.java 2KB
SubjectBriefServiceImpl.java 2KB
SubjectMultipleDao.java 2KB
SubjectRadioServiceImpl.java 2KB
MinioStorageAdapter.java 2KB
RolePermissionController.java 2KB
GatewayExceptionHandler.java 2KB
JudgeTypeHandler.java 2KB
AuthUserRoleDao.java 2KB
AuthRolePermission.java 2KB
SubjectJudgeDao.java 2KB
SubjectBriefDao.java 2KB
AuthPermissionServiceImpl.java 2KB
SubjectCategoryDao.java 2KB
AuthRole.java 2KB
AuthRoleDao.java 2KB
AuthRole.java 2KB
AuthRoleServiceImpl.java 2KB
AuthUserRole.java 2KB
SqlStatementInterceptor.java 2KB
AuthUserDao.java 2KB
CacheUtil.java 2KB
SubjectRadioDao.java 2KB
AuthUserServiceImpl.java 2KB
ReceiveTextMsgHandler.java 2KB
SubjectInfoDTO.java 2KB
SubjectInfoBO.java 2KB
AuthUserRoleServiceImpl.java 2KB
GlobalConfig.java 2KB
BriefTypeHandler.java 2KB
GlobalConfig.java 2KB
LoginFilter.java 2KB
TableInfo.java 2KB
Result.java 2KB
SHA1.java 2KB
AuthRoleDomainServiceImpl.java 1KB
DruidEncryptUtil.java 1KB
SubjectLiked.java 1KB
CodeGeneratorUtils.java 1KB
SubjectInfoService.java 1KB
TestFeignController.java 1KB
TableUtils.java 1KB
共 312 条
- 1
- 2
- 3
- 4
资源评论
lly202406
- 粉丝: 2613
- 资源: 5446
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功