目录
1、SpringBoot集成ElasticSearch
在创建项目时,选择elasticsearch选项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
注意:版本兼容的问题,不同版本的SpringBoot有对应的ElasticSearch版本
如SpringBoot2.5版本对应ES-7.11.X
2、ElasticSearch之文档的基本操作(增删改查)
ES提供了基于HTTP协议的RESTful API用于操作文档,使用Kibana中的开发工具,可以方便的执行以下操作(不仅限下述方法):
方法 | 功能 |
PUT | 创建索引数据 |
GET | 查询文档 |
POST | 修改文档 |
DELETE | 删除文档 |
HEAD | 检查某文档是否存在 |
ES是面向文档的,文档是es
中可搜索的最小单位,ES
的文档由一个或多个字段组成,类似于关系型数据库中的一行记录,但ES
的文档是以JSON
进行序列化并保存的,每个JSON
对象由一个或多个字段组成,字段类型可以是布尔,数值,字符串、二进制、日期等数据类型。
3、通过RestHighClient操作ES索引
(1)配置RestHighClient对象
/**
* Spring的两步骤:
* 1、找对象
* 2、放入Spring容器中待用
*
*/
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
));
return client;
}
}