Springboot整合ElasticSearch7.6.x
前言
关于ElasticSearch的安装,原理和基本使用可以参考之前写的文章。
ElasticSearch7.6.x 学习笔记
接下来是与springboot的整合。
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
注意版本要和你安装的es版本一致
<properties>
<!-- 统一版本-->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
编写配置类
EsClientConfig
/**
* @author CHEN
* @date 2020/10/31 18:37
* 原生elasticsearch配置类
*/
@Configuration
@EnableElasticsearchRepositories
public class EsClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("118.178.125.139", 9200, "http")));
}
}
调用es的API
高级es客户端
注入之前配置的高级客户端对象
@Autowired
RestHighLevelClient restHighLevelClient;
创建索引
void testCreateIndex() throws IOException {
//1. 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("test_index");
//2. 客户端执行创建请求 IndicesClient 请求后获得响应
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
判断索引是否存在
void testExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("test_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
删除索引
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test_index");
//删除
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
创建文档
void testAddDocument() throws IOException {
//创建对象
Student student = new Student("test", 3);
//创建请求
IndexRequest request = new IndexRequest("test_index");
//规则 put /test_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//将我们的数据放入请求 json
request.source(JSON.toJSONString(student), XContentType.JSON);
//客户端发送请求,获取响应的结果
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
//对应我们命令返回的状态 CREATED
System.out.println(indexResponse.status());
}
获取文档,判断是否存在
void testIsExists() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
//不获取返回的 _source 的上下文了
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
获得文档的信息
void testGetDocument() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
//打印文档的内容
System.out.println(response.getSourceAsString());
//返回的全部内容和命令是一样的
System.out.println(response);
}
更新文档的信息
void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("test_index", "1");
request.timeout("1s");
Student student = new Student("test", 3);
request.doc(JSON.toJSONString(student), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
删除文档的记录
void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("test_index", "1");
request.timeout("1s");
DeleteResponse