0
点赞
收藏
分享

微信扫一扫

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作


文章目录

  • ​​1 相应的官方文档​​
  • ​​2 项目实战​​
  • ​​2.1 添加依赖​​
  • ​​2.2 添加配置类​​
  • ​​2.3 关于索引的API操作​​
  • ​​2.3.1 创建索引​​
  • ​​2.3.2 索引是否存在​​
  • ​​2.3.3 删除索引​​
  • ​​2.4 关于文档的API操作​​
  • ​​2.4.1 添加文档​​
  • ​​2.4.2 文档是否存在​​
  • ​​2.4.3 获取文档信息​​
  • ​​2.4.4 更新文档信息​​
  • ​​2.4.5 删除文档​​
  • ​​2.4.6 批量添加文档​​
  • ​​2.4.7 条件查询文档​​

1 相应的官方文档

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_elasticsearch


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_02


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_elasticsearch_03


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_04


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_java_05


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_06


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_07


1、ES 的依赖

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>

2、相关类的配置

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_08

2 项目实战

2.1 添加依赖

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_java_09


导入后发现,Spring Boot 2.5.4 的默认 ES 依赖的版本是 7.12.1

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_10


而我本地的 ES 版本为 7.14.1

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_elasticsearch_11


而官方在兼容性方面已经实现了 ES 版本对客户端版本的向后兼容,因此,依据此说明,我的本地环境在版本上,在理论上是没有问题的。

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_12

2.2 添加配置类

根据官方文档的示例,我们将此配置类注入到项目中

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_13


仿照官方示例:

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_14


依赖中自动配置类的位置

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_15

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_16

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_17

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);
}
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_18

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);
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_java_19


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_20

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());
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_21

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());
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_java_22

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_23

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);
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_24

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());
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_elasticsearch_25

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());
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_26


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_spring boot_27

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());
}

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_elasticsearch_28


【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_java_29

2.4.6 批量添加文档

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_大数据_30

2.4.7 条件查询文档

【ELK技术栈】详解 SpringBoot 集成 ElasticSearch 之项目实战:关于索引的API操作、关于文档的API操作_restful_31


举报

相关推荐

0 条评论