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元

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/158582.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面试算法54:所有大于或等于节点的值之和

题目 给定一棵二叉搜索树&#xff0c;请将它的每个节点的值替换成树中大于或等于该节点值的所有节点值之和。假设二叉搜索树中节点的值唯一。例如&#xff0c;输入如图8.10&#xff08;a&#xff09;所示的二叉搜索树&#xff0c;由于有两个节点的值大于或等于6&#xff08;即…

如何使用Ruby 多线程爬取数据

现在比较主流的爬虫应该是用python&#xff0c;之前也写了很多关于python的文章。今天在这里我们主要说说ruby。我觉得ruby也是ok的&#xff0c;我试试看写了一个爬虫的小程序&#xff0c;并作出相应的解析。 Ruby中实现网页抓取&#xff0c;一般用的是mechanize&#xff0c;使…

【数据结构】数组和字符串(十三):链式字符串的基本操作(串长统计、查找、复制、插入、删除、串拼接)

文章目录 4.3 字符串4.3.1 字符串的定义与存储4.3.2 字符串的基本操作&#xff08;链式存储&#xff09;1. 结构体2. 初始化3. 判空4. 串尾添加5. 打印6. 串长统计7. 查找8. 复制9. 插入10. 删除11. 串拼接12. 销毁13. 主函数14. 代码整合 4.3 字符串 字符串(String)是由零个或…

模电学习路径

交流通路实质 列出电路方程1&#xff0c;方程1对时刻t做微分 所得方程1‘ 即为 交流通路 方程1对时刻t做微分&#xff1a;两个不同时刻的方程1相减&#xff0c;并 令两时刻差为 无穷小 微分 改成 差 模电学习路径&#xff1a; 理论 《电路原理》清华大学 于歆杰 朱桂萍 陆文…

ES-初识ES

文章目录 介绍ElasticSearchElasticSearch的主要功能ElasticSearch的主要特性ElasticSearch的家族成员LogStashKibanaBeats ELK&#xff08;ElasticSearch LogStash Kibana&#xff09;的应用场景与数据库集成指标采集/日志分析 安装和配置ElasticSearch一、安装1、下载ES安装…

【Linux】Nignx的入门使用负载均衡动静分离(前后端项目部署)---超详细

一&#xff0c;Nignx入门 1.1 Nignx是什么 Nginx是一个高性能的开源Web服务器和反向代理服务器。它使用事件驱动的异步框架&#xff0c;可同时处理大量请求&#xff0c;支持负载均衡、反向代理、HTTP缓存等常见Web服务场景。Nginx可以作为一个前端的Web服务器&#xff0c;也可…

Python能够超过JAVA吗?

前言 如果你定期关注现今的科技发展&#xff0c;那么你可能想知道我为什么要写这篇文章告诉人们学习Python&#xff1f;因为几年前我提倡Java而不是Python。 Python是最适用于人工智能的编程语言&#xff0c;伴随着人工智能时代的到来&#xff0c;Python变得越来越火&#xf…

系列四、全局配置文件mybatis-config.xml

一、全局配置文件中的属性 mybatis全局配置中的文件非常多&#xff0c;主要有如下几个&#xff1a; properties&#xff08;属性&#xff09;settings&#xff08;全局配置参数&#xff09;typeAliases&#xff08;类型别名&#xff09;typeHandlers&#xff08;类型处理器&am…

http1,https,http2,http3总结

1.HTTP 当我们浏览网页时&#xff0c;地址栏中使用最多的多是https://开头的url&#xff0c;它与我们所学的http协议有什么区别&#xff1f; http协议又叫超文本传输协议&#xff0c;它是应用层中使用最多的协议&#xff0c; http与我们常说的socket有什么区别吗&#xff1f; …

【mfc/VS2022】计图实验:绘图工具设计知识笔记3

实现类对串行化的支持 如果要用CArchive类保存对象的话&#xff0c;那么这个对象的类必须支持串行化。一个可串行化的类通常有一个Serialize成员函数。要想使一个类可串行化&#xff0c;要经历以下5个步骤&#xff1a; 1、从CObject派生类 2、重写Serialize成员函数 3、使用DE…

上海物理、化学高考命题趋势及2024年上海物理、化学高考备考建议

在上海高考时&#xff0c;物理、化学虽然不像语文、英语和数学那样分数高&#xff0c;但是仍然很重要。那么&#xff0c;从这几年的上海物理、化学的高考题目来看&#xff0c;我们互发现什么命题趋势和考题特点呢&#xff1f;如何备考接下来的2024年高考物理和化学呢&#xff1…

jar包的精细化运营,Java模块化简介 | 京东云技术团队

图&#xff1a;模块化手机概念 一、什么是Java模块化 Java模块化&#xff08;module&#xff09;是Java9及以后版本引入的新特性。 官方对模块的定义为&#xff1a;一个被命名的&#xff0c;代码和数据的自描述集合。&#xff08; the module, which is a named, self-descri…