Springboot 集成 ElasticSearch7.8.0

使用spring boot的版本为2.4.0,所以系统自带的ElasticSearch 的版本就是7.9.2,我们可以为他自动降个版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>11</java.version>
<elasticsearch.version>7.8.0</elasticsearch.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

使用版本要和我们本地的版本一致;

第一步、Quick Start

image-20201124214447961

初始化,在spring boot自动配置;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.demo.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient (RestClient.builder(
new HttpHost("localhost", 9200, "http")));
}
}

关于RestHighLevelClient

1
2
3
public RestHighLevelClient(RestClientBuilder restClientBuilder) {
this(restClientBuilder, Collections.emptyList());
}

对我们的客户端进行一个绑定

第二步、创建第一个索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.demo;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class DemoApplicationTests {
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
void contextLoads() throws IOException {
CreateIndexRequest request=new CreateIndexRequest ("saxon");
CreateIndexResponse response=restHighLevelClient.indices ().create (request, RequestOptions.DEFAULT);
}

}

有一个创建索引的服务端,就有一个创建索引的响应端创建执行索引命令;

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
GET /saxon

{
"saxon" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1606225198553",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "LwwRyzzISD20HJXIKsHZ8A",
"version" : {
"created" : "7080099"
},
"provided_name" : "saxon"
}
}
}
}

第三步、查看索引是否存在以及获得索引信息

1
2
GetIndexRequest request=new GetIndexRequest ("saxon");
System.out.println (restHighLevelClient.indices ().exists (request, RequestOptions.DEFAULT));

执行命令:restHighLevelClient.indices ()

获得索引信息:

1
2
GetIndexRequest request=new GetIndexRequest ("saxon");
System.out.println (restHighLevelClient.indices ().get (request,RequestOptions.DEFAULT));

第四步、测试删除一个索引

1
2
3
4
void test3() throws IOException {
DeleteIndexRequest request=new DeleteIndexRequest ("saxon");
System.out.println (restHighLevelClient.indices ().delete (request,RequestOptions.DEFAULT).isAcknowledged ());
}