文章目录
1 相应的官方文档
1、ES 的依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>
2、相关类的配置
2 项目实战
2.1 添加依赖
导入后发现,Spring Boot 2.5.4 的默认 ES 依赖的版本是 7.12.1
而我本地的 ES 版本为 7.14.1
而官方在兼容性方面已经实现了 ES 版本对客户端版本的向后兼容,因此,依据此说明,我的本地环境在版本上,在理论上是没有问题的。
2.2 添加配置类
根据官方文档的示例,我们将此配置类注入到项目中
仿照官方示例:
依赖中自动配置类的位置
2.3 关于索引的API操作
索引:创建、是否存在、删除
2.3.1 创建索引
@SpringBootTest
class Es01ApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//1------------测试索引的创建
@Test
void testCreateIndex() throws IOException {
//1 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("zlc_index");
//2 客户端执行请求
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
2.3.2 索引是否存在
//2------------测试索引是否存在
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("zlc_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
2.3.3 删除索引
//3------------测试删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("zlc_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
2.4 关于文档的API操作
文档:添加、获取、是否存在、更新、删除、批量添加、 条件查询文档
2.4.1 添加文档
//测试添加文档
@Test
void testAddDocument() throws IOException {
//创建对象
User user = new User("周龙超", 20);
//创建请求
IndexRequest request = new IndexRequest("zlc_index");
//规则 put /zlc_index/_doc/1
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//将数据放入请求
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求,获取响应结果
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
2.4.2 文档是否存在
//文档是否存在,判断是否存在 GET /index/_doc/1
@Test
void testIsExists() throws IOException {
GetRequest getRequest = new GetRequest("zlc_index", "1");
//不获取返回的 _source 的上下文,提高效率
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
2.4.3 获取文档信息
//获取文档信息
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("zlc_index", "1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse);
System.out.println(getResponse.getSourceAsString());
}
2.4.4 更新文档信息
//更新文档信息
@Test
void testUpdateDocument() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("zlc_index", "1");
updateRequest.timeout("1s");
User user = new User("Zhou_LC", 20);
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse);
System.out.println(updateResponse.status());
}
2.4.5 删除文档
//删除文档
@Test
void testDeleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("zlc_index", "1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse);
System.out.println(deleteResponse.status());
}