1 java操作ES方式
1.1 操作ES 9300端口(TCP) 但开发中不在9300进行操作
ES集群节点通信使用的也是9300端口
如果通过9300操作ES,需要与ES建立长连接 可通过引入spring-data-elasticsearch:transport-api.jar
不在9300操作原因:
1.springboot版本不同,transport-api.jar不同,不能适配ES版本
2.官方在7.x版本已经不建议在9300操作ES,8以后会废弃ES通过9300操作的jar包
1.2 操作ES 9200端口(HTTP) 优选Elasticsearch-Rest-Client
通过9200对ES发送Http请求就可以了
1.使用第三方JestClient操作ES:更新慢
2.RestTemplate、HttpClient、OkHttp:发送HTTP请求,但如果操作ES一些DSL语句,需要自己封装,麻烦
3.Elasticsearch-Rest-Client(elasticsearch-rest-high-level-client):官方RestClient,封装了ES操作,API层次分明,上手简单
2 整合
2.1引入elasticsearch-rest-high-level-client依赖
<!--elasticsearch-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
刷新Maven 会发现elasticsearch和elasticsearch-rest-client版本是7.17.4,并不是7.4.2
原因是因为springboot对ES版本做了管理,当前springboot默认整合spring-data-elasticsearch操作ES
修改版本问题
如果使用Maven进行一个直接或间接继承spring-boot-dependencies(比如spring-boot-starter-parent)的构建,并想覆盖一个特定的
第三方依赖版本,可以添加<properties>标签,并重写第三方依赖版本
<properties>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
如果使用<scope>import</scope>,将spring-boot-dependencies添加到自己的dependencyManagement片段,
那么必须重新定义artifact而不是重写第三方依赖版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>