ElasticSearch7.x 升级后SpringBoot连不上?

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 本文主要讲述使用ElasticSearch6.x 升级到 ElasticSearch7.x后所需要的修改以及案例代码。

前言


原来项目中使用的ElasticSearch版本6.8升级到了7.5.1,且开启了x-pack安全认证,导致原来的查询接口全部使用不了。


注:原SprintBoot版本为2.1.x。

连接ElasticSearch使用的是spring-boot-starter-data-elasticsearch


配置修改

SpringBoot版本升级


在SpringBoot2.2.x版本才支持ElasticSearch7.x,所以我们先需要升级SpringBoot版本。


<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/><!--lookupparentfromrepository--></parent>


ES连接配置修改


原来spring-boot-starter-data-elasticsearch连接ES的配置如下:


spring.data.elasticsearch.cluster-nodes=172.31.0.207:9300spring.data.elasticsearch.cluster-name=elk-clusterspring.data.elasticsearch.repositories.enabled=true


升级后主要使用rest接口进行连接,由于开启了x-pack认证,所以需要修改es的账号密码,修改后配置如下:


spring.elasticsearch.rest.uris=172.31.0.207:9300spring.elasticsearch.rest.username=elasticspring.elasticsearch.rest.password=changeme


ElasticsearchTemplate升级


原来spring-boot-starter-data-elasticsearch连接ES主要使用ElasticsearchTemplate进行操作,新版本主要使用ElasticsearchRestTemplate


@AutowiredprivateElasticsearchRestTemplateelasticsearchRestTemplate;


案例源码


  • pom文件


<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version></parent><groupId>com.jianzh5</groupId><artifactId>esalarm</artifactId><version>0.0.1-SNAPSHOT</version><name>esalarm</name><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--ES--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency></project>


  • 建立ElasticSearch实体类LogDoc


@Data@Document(indexName="alarm-*",type="_doc")
publicclassLogDocimplementsSerializable {
privatestaticfinallongserialVersionUID=6320548148250372657L;
@IdprivateStringid;
privateStringlogLevel;
privateStringmodule;
privateStringsystem;
privateStringrefCode;
privateStringresponse;
privateStringparm;
privateStringmessage;
privatelonglogTime;
}


  • 建立LogRepository,操作底层查询


@RepositorypublicinterfaceLogRepositoryextendsElasticsearchRepository<LogDoc,String> {
}


  • 建立接口层LogService


publicinterfaceLogService {
/*** 根据ID获取对应的实体* @param id 日志id* @return*/LogDocgetById(Stringid);
/*** 根据时间范围查找指定格式的日志* @author javadaily* @date 2019/9/21 9:24* @param minRange 开始时间* @param maxRange 结束时间* @param logLevel 日志级别* @return 日志列表*/List<LogDoc>findRangeLogByLevel(DateTimeminRange,DateTimemaxRange,StringlogLevel);
}


  • 建立接口实现层LogServiceImpl


/*** @author javadaily* @date 2019/9/10 15:12*/@ServicepublicclassLogServiceImplimplementsLogService {
@AutowiredprivateLogRepositorylogRepository;
@AutowiredprivateElasticsearchRestTemplateelasticsearchRestTemplate;
@OverridepublicLogDocgetById(Stringid) {
Optional<LogDoc>logOptional=logRepository.findById(id);
returnlogOptional.orElse(null);
    }
@OverridepublicList<LogDoc>findRangeLogByLevel(DateTimeminRange, DateTimemaxRange, StringlogLevel) {
//需要强制转换成小写logLevel=logLevel.toLowerCase();
SearchQuerysearchQuery=newNativeSearchQueryBuilder()
                .withQuery(boolQuery()
//module,system 必须有值才能告警                        .must(existsQuery("module"))
                        .must(existsQuery("system"))
                        .must(termQuery("logLevel", logLevel))
                        .must(rangeQuery("logTime")
                                .from(minRange.getMillis())
                                .to(maxRange.getMillis())))
                .build();
returnelasticsearchRestTemplate.queryForList(searchQuery, LogDoc.class);
    }
}


使用ElasticsearchRepositoryElasticsearchRestTemplate对ES进行操作,ElasticsearchRestTemplate的功能比较强大,能支持更为复杂的查询,大家可以根据具体情况选择。

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
2天前
|
存储 JSON 搜索推荐
Springboot2.x整合ElasticSearch7.x实战(三)
Springboot2.x整合ElasticSearch7.x实战(三)
8 0
|
3天前
|
存储 自然语言处理 关系型数据库
Springboot2.x整合ElasticSearch7.x实战(二)
Springboot2.x整合ElasticSearch7.x实战(二)
6 0
|
3天前
|
搜索推荐 数据可视化 Java
Springboot2.x整合ElasticSearch7.x实战(一)
Springboot2.x整合ElasticSearch7.x实战(一)
6 0
|
9天前
|
Java
springboot和elasticsearch以及springboot data elasticsearch对应的版本
springboot和elasticsearch以及springboot data elasticsearch对应的版本
|
9天前
|
Java Spring
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
|
1月前
|
移动开发 前端开发 NoSQL
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
158 0
|
1月前
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
1月前
|
JSON Java 数据格式
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
22 0
|
1月前
|
自然语言处理 安全 Linux
干货 | Elasticsearch 8.X 版本升级指南
干货 | Elasticsearch 8.X 版本升级指南
44 0
|
1月前
|
安全 Java API
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
SpringBoot 实现 elasticsearch 索引操作(RestHighLevelClient 的应用)
25 1