package com.jzo2o.es.core.operations;
import co.elastic.clients.elasticsearch._types.Result;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ElasticSearchException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.dto.PageQueryDTO;
import com.jzo2o.common.utils.*;
import com.jzo2o.es.constants.FieldConstants;
import com.jzo2o.es.utils.TermUtils;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.GeoDistanceType;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.WriteResponseBase;
import co.elastic.clients.elasticsearch._types.query_dsl.FieldAndFormat;
import co.elastic.clients.elasticsearch._types.query_dsl.TermsQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.TermsQueryField;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Hit;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 后期优化分页功能 todo,临时使用
*/
@Slf4j
public class DefaultDocumentOperations implements DocumentOperations {
private final ElasticsearchClient elasticsearchClient;
public DefaultDocumentOperations(ElasticsearchClient elasticsearchClient) {
this.elasticsearchClient = elasticsearchClient;
}
@Override
public <T> Boolean insert(String index, T document) {
try {
CreateResponse createResponse = elasticsearchClient.create(builder -> builder.id(getId(document)).document(document).index(index));
log.debug("create document response : {}", createResponse);
boolean success = isSuccess(createResponse);
return success;
} catch (IOException e) {
// e.printStackTrace();
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
// return false;
}
@Override
public <T> Boolean batchInsert(String index, List<T> documents) {
BulkRequest.Builder br = new BulkRequest.Builder();
for (T document : documents) {
br.operations(op -> op.index(idx -> idx.index(index)
.id(getId(document))
.document(document)));
}
try {
BulkResponse bulk = elasticsearchClient.bulk(br.build());
Boolean success = isSuccess(bulk);
return success;
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <T> Boolean batchUpsert(String index, List<T> documents) {
if (CollUtils.isEmpty(documents)) {
return false;
}
List<String> ids = documents.stream().map(document -> getId(document)).collect(Collectors.toList());
List<?> documentInEs = this.findByIds(index, ids, Arrays.asList(FieldConstants.ID), documents.get(0).getClass());
List<String> idsInEs = CollUtils.isEmpty(documentInEs) ? new ArrayList<>() : documentInEs.stream().map(document -> getId(document)).collect(Collectors.toList());
BulkRequest.Builder builder = new BulkRequest.Builder();
for (T document : documents) {
String id = getId(document);
boolean exists = idsInEs.contains(id);
builder.operations(op -> {
if (exists) {
op.update(u -> u.action(a -> a.doc(document)).index(index).id(id));
} else {
op.index(idx -> idx.index(index)
.id(id)
.document(document));
}
return op;
});
}
try {
BulkResponse bulk = elasticsearchClient.bulk(builder.build());
Boolean success = isSuccess(bulk);
return success;
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <T> Boolean updateById(String index, T document) {
Object id = ReflectUtils.getFieldValue(document, IdUtils.ID);
if (id == null) {
throw new ElasticSearchException("es更新失败,id为空");
}
try {
// 2.数据更新
UpdateResponse<?> response = elasticsearchClient.update(u -> u
.index(index)
.id(id.toString())
.doc(document)
, document.getClass());
Boolean success = isSuccess(response);
return success;
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <ID> Boolean deleteById(String index, ID id) {
try {
// 2.数据更新
DeleteResponse response = elasticsearchClient.delete(builder -> builder.id(id.toString()).index(index));
Boolean success = isSuccess(response);
return success;
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <ID> Boolean batchDelete(String index, List<ID> ids) {
BulkRequest.Builder builder = new BulkRequest.Builder();
ids.stream().forEach(id ->
builder.operations(b -> b.delete(d -> d.index(index).id(id.toString())))
);
try {
BulkResponse bulk = elasticsearchClient.bulk(builder.build());
Boolean success = isSuccess(bulk);
return success;
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <T, ID> T findById(String index, ID id, Class<T> clazz) {
try {
GetResponse<T> response = elasticsearchClient.get(GetRequest.of(builder -> builder.id(id.toString()).index(index)), clazz);
return response.source();
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <T, ID> List<T> findByIds(String index, List<ID> ids, Class<T> clazz) {
SearchRequest.Builder searchRequestBuild = new SearchRequest.Builder();
TermsQuery termsQuery = TermsQuery.of(t -> t.field(FieldConstants.ID).terms(new TermsQueryField.Builder().value(TermUtils.parse(ids)).build()));
searchRequestBuild.index(index)
.query(builder -> builder.terms(termsQuery));
try {
SearchResponse<T> searchResponse = elasticsearchClient.search(searchRequestBuild.build(), clazz);
return searchResponse.hits().hits()
.stream()
.map(tHit -> tHit.source())
.collect(Collectors.toList());
} catch (IOException e) {
log.error(e.getMessage(),e);
throw new CommonException(500,e.getMessage());
}
}
@Override
public <T, ID> List<T> findByIds(String index, List<ID> ids, List<String> fields, Class<T> clazz) {
SearchRequest.Builder searchRequestBuild = new SearchRequest.Builder();
TermsQuery termsQuery = TermsQuery.of(t -> t.field(FieldConstants.ID).terms(new TermsQueryField.Builder().value(TermUtils.parse(ids)).build()));
searchRequestBuild.index(index)
.query(builder -> builder.terms(termsQuery));
if (CollUtils.isNotEmpty(fields)) {
List<FieldAndFormat> fieldAndFormats = fields.stream().map(field -> FieldAndFormat.of(builder -> builder.field(field))).coll
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一个基于Java技术的云岚家政平台设计源码,总计包含417个文件,涵盖多种类型,主要包括349个Java源文件、28个XML配置文件、13个YAML配置文件、10个工厂类文件、5个Git忽略文件、5个JSON文件和3个批处理脚本文件。此外,还包括Dockerfile文件,用于容器化部署。该平台致力于提供家政服务管理解决方案。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java技术的云岚家政平台设计源码 (418个子文件)
jzo2o-publics-startup.bat 264B
jzo2o-gateway-startup.bat 239B
jzo2o-foundations-startup.bat 235B
Dockerfile 285B
Dockerfile 281B
Dockerfile 257B
spring.factories 609B
spring.factories 222B
spring.factories 164B
spring.factories 119B
spring.factories 113B
spring.factories 111B
spring.factories 111B
spring.factories 107B
spring.factories 105B
spring.factories 102B
.gitignore 395B
.gitignore 395B
.gitignore 395B
.gitignore 395B
.gitignore 214B
DefaultDocumentOperations.java 13KB
AbstractStateMachine.java 12KB
ServeItemServiceImpl.java 10KB
CacheHelper.java 9KB
RegionServiceImpl.java 8KB
DateUtils.java 8KB
ServeTypeServiceImpl.java 8KB
SwaggerTransformServletResponse.java 6KB
CollUtils.java 6KB
RabbitMqConfiguration.java 6KB
SpringCacheConfig.java 5KB
GatewayWebUtils.java 5KB
HomeServiceTest.java 5KB
PageUtils.java 5KB
RabbitClient.java 5KB
BeanUtils.java 5KB
OrderAggregationResDTO.java 5KB
AbstractCanalRabbitMqMsgListener.java 4KB
OrderResDTO.java 4KB
WechatServiceImpl.java 4KB
RegionController.java 4KB
NumberUtils.java 4KB
AssertUtils.java 4KB
HashSyncThread.java 4KB
AmapMapServiceImpl.java 4KB
HashCacheClearAspect.java 4KB
TokenFilter.java 4KB
RedisConfiguration.java 3KB
ServeTypeController.java 3KB
CommonExceptionAdvice.java 3KB
StateMachinePersisterImpl.java 3KB
PageResult.java 3KB
SyncManagerImpl.java 3KB
SpringCacheTest.java 3KB
ServeItemController.java 3KB
TradingResDTO.java 3KB
OrderSimpleResDTO.java 3KB
RedissonConfiguration.java 3KB
BizSnapshotServiceImpl.java 3KB
ConfigRegionServiceImpl.java 3KB
FailMsgDaoImpl.java 3KB
JwtTool.java 3KB
AbstractSyncThread.java 3KB
EsConfiguration.java 3KB
MyBatisAutoFillInterceptor.java 3KB
LockAspect.java 3KB
Result.java 2KB
ServeSyncServiceImpl.java 2KB
ResponseWrapper.java 2KB
IServeItemService.java 2KB
DocumentOperations.java 2KB
ErrorInfo.java 2KB
ComparatorUtils.java 2KB
ResponseWrapper.java 2KB
Knife4jConfiguration.java 2KB
SmsCodeServiceImpl.java 2KB
PageHelperUtils.java 2KB
IServeServiceTest.java 2KB
EsClientTest.java 2KB
ConfigRegionSetReqDTO.java 2KB
AliOssStorageServiceImpl.java 2KB
ConfigRegionResDTO.java 2KB
ConfigRegionInnerResDTO.java 2KB
SpelUtils.java 2KB
RabbitMqResender.java 2KB
SpringCacheTest.java 2KB
ServeProviderResDTO.java 2KB
AspectUtils.java 2KB
JsonConfig.java 2KB
InnerRegionController.java 2KB
LockHelper.java 2KB
ServeItemResDTO.java 2KB
PackResultFilter.java 2KB
MybatisPlusConfiguration.java 2KB
ServeSync.java 2KB
FailMsgMapper.java 2KB
OperatorServiceImpl.java 2KB
InstitutionStaffApi.java 2KB
HashCacheClear.java 2KB
共 418 条
- 1
- 2
- 3
- 4
- 5
资源评论
csbysj2020
- 粉丝: 2564
- 资源: 5477
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功