当前位置: 首页 > news >正文

springboot-2.7.3+ES-7.10.0

跟着官网走,能干99。一年几次变,次次不一样。刚部署好ES-6.8,又买阿里云Es-7.10.0根本忙不完。

做为JDK1.8最后一个版本。今天就拿新技术部署一套。致辞:大家以后就用这套好了。别轻易触发springboot3.0了

学习无止境:Dependency Versions

一springboot干货:

pom文件配置:

 <elasticsearch.version>7.10.0</elasticsearch.version><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency>

一。直接干底层模版接口代码:

/*** description: Es基础功能组件**/
public interface ElasticsearchTemplate<T,M> {/*** 通过Low Level REST Client 查询* @param request 原生查询对象*/public Response request(Request request) throws Exception;/*** 新增索引* @param t 索引pojo*/public boolean save(T t) throws Exception;/*** 新增索引(路由方式)* @param t 索引pojo* @param routing 路由信息(默认路由为索引数据_id)*/public boolean save(T t,String routing) throws Exception;/*** 新增索引集合* @param list 索引pojo集合*/public BulkResponse save(List<T> list) throws Exception;/*** 新增索引集合(分批方式,提升性能,防止es服务内存溢出,每批默认5000条数据)* @param list 索引pojo集合*/public BulkResponse[] saveBatch(List<T> list) throws Exception;/*** 更新索引集合* @param list 索引pojo集合* @return* @throws Exception*/public BulkResponse bulkUpdate(List<T> list) throws Exception;
}

二。模版接口实现类:

@Component
public class ElasticsearchTemplateImpl<T, M> implements ElasticsearchTemplate<T, M> {private Logger logger = LoggerFactory.getLogger(this.getClass());@AutowiredRestHighLevelClient client;@AutowiredElasticsearchIndex elasticsearchIndex;@Overridepublic Response request(Request request) throws Exception {Response response = client.getLowLevelClient().performRequest(request);return response;}@Overridepublic boolean save(T t) throws Exception {return save(t,null);}@Overridepublic boolean save(T t, String routing) throws Exception {MetaData metaData = elasticsearchIndex.getMetaData(t.getClass());String indexname = metaData.getIndexname();String id = Tools.getESId(t);IndexRequest indexRequest=new IndexRequest(indexname);;if (StringUtils.hasText(id)) {indexRequest.id(id);}JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(t));indexRequest.source(jsonObject);if(StringUtils.hasText(routing)){indexRequest.routing(routing);}IndexResponse indexResponse = null;indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {logger.info("INDEX CREATE SUCCESS");elasticsearchIndex.rollover(t.getClass(),true);} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {logger.info("INDEX UPDATE SUCCESS");} else {return false;}return true;}
}

三 。对外客户端提供接口:

/*** program: 对客户端提供api* description:**/
public interface ESCRepository<T,M> {/*** 通过Low Level REST Client 查询*/public Response request(Request request) throws Exception;/*** 新增索引* @param t*/public boolean save(T t) throws Exception;/*** 新增索引集合* @param list*/public BulkResponse save(List<T> list) throws Exception;/*** 按照有值字段更新索引* @param t*/public boolean update(T t) throws Exception;
}

四。对ES提供实现类:

/*** program: ESCRepository对外提供统一接口**/
public class SimpleESCRepository<T,M> implements ESCRepository<T,M> {private Class<T> domainClass;private Class<M> idClass;private ApplicationContext applicationContext;private ElasticsearchTemplate elasticsearchTemplate = null;public SimpleESCRepository(ApplicationContext applicationContext){this.applicationContext = applicationContext;}private ElasticsearchTemplate getElasticsearchTemplate(){return applicationContext.getBean(ElasticsearchTemplate.class);}@Overridepublic Response request(Request request) throws Exception {return getElasticsearchTemplate().request(request);}@Overridepublic boolean save(T o) throws Exception {return getElasticsearchTemplate().save(o);}@Overridepublic BulkResponse save(List<T> list) throws Exception {return getElasticsearchTemplate().save(list);}
}@Overridepublic boolean update(T t) throws Exception {return getElasticsearchTemplate().update(t);}@Overridepublic boolean updateCover(T t) throws Exception {return getElasticsearchTemplate().updateCover(t);}

写到现在终于把底层封装完毕。客户端如何调用

客户端实现接口:

public interface IndexRepository extends ESCRepository<IndexEntity,String> {
}

这就完了,你答对了,至此springboot+ES已经封装完成

直接controller接口测试:

    @Resourceprivate IndexRepository indexRepository;@GetMapping("/demo/add")public String add() throws Exception {IndexEntity indexEntity = new IndexEntity ();indexEntity .setProposal_no("1");indexEntity .setAppli_name("a1");indexEntity .setRisk_code("aa1");indexEntity .setSum_premium(1);indexDemoRepository.save(indexEntity );return "新增成功";}@GetMapping("/demo/query")public List<IndexDemo> query() throws Exception {List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchAllQuery());return search;}

一套掌法打出。查询结果到手

[{"proposal_no": "1","risk_code": "aa1","risk_name": null,"business_nature": null,"business_nature_name": null,"appli_code": null,"appli_name": "a1","insured_code": null,"insured_name": null,"operate_date": null,"operate_date_format": null,"start_date": null,"end_date": null,"sum_amount": 0.0,"sum_premium": 1.0,"com_code": null}
]

能欣赏到这里说明你对ES的理解钢圈了。需要源码的欢迎加我V。10元

相关文章:

  • MODBUS通讯
  • Vue3使用Monaco-editor
  • 视频列表:点击某个视频进行播放,其余视频全部暂停(同时只播放一个视频)
  • java实体类全部复制到新类及部分复制到新类
  • 2024最新最全:【Windows10】u盘安装系列教程【附安装包】
  • 【笔记】Arrays.binarySearch()实践,以及需要注意的一些问题点
  • Python画图之HelloKitty
  • overflow溢出属性、定位、前端基础之JavaScript
  • 手写一个uniapp的步骤条组件
  • OSPF高级特性2(特殊区域,聚合)
  • 【Linux基础IO篇】系统文件接口(1)
  • 大厂面试题-TCP协议为什么要设计三次握手?
  • Python selenium驱动下载,模块安装以及基本使用
  • fastadmin笔记,关联查询,下拉框,关联下拉框查询,编辑时下拉框默认值
  • ArcGIS制作土地利用现状图
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Codepen 每日精选(2018-3-25)
  • css系列之关于字体的事
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • LintCode 31. partitionArray 数组划分
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • miaov-React 最佳入门
  • python 装饰器(一)
  • Redis的resp协议
  • session共享问题解决方案
  • SpiderData 2019年2月23日 DApp数据排行榜
  • windows下如何用phpstorm同步测试服务器
  • 创建一种深思熟虑的文化
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 记一次和乔布斯合作最难忘的经历
  • 力扣(LeetCode)357
  • 前端技术周刊 2019-02-11 Serverless
  • 网页视频流m3u8/ts视频下载
  • 再次简单明了总结flex布局,一看就懂...
  • 【干货分享】dos命令大全
  • C# - 为值类型重定义相等性
  • 容器镜像
  • #android不同版本废弃api,新api。
  • #include到底该写在哪
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (二)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (一)80c52学习之旅-起始篇
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .NET的微型Web框架 Nancy
  • .net流程开发平台的一些难点(1)
  • .NET业务框架的构建
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .net中我喜欢的两种验证码