ElasticSearch学习笔记
一.ElasticSearch简述
** Elaticsearch ,简称为es,es是一个开源的高扩展的分布式全文检索引擎**,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别(大数据时代)的数据。es也使用java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单
据国际权威的数据库产品评测机构DB Engines的统计,在2016年1月,ElasticSearch已超过Solr等,成为排名第一的搜索引擎类应用
二.安装
1.elasticSearch的安装
下载地址:https://www.elastic.co/cn/downloads/
历史版本下载:https://www.elastic.co/cn/downloads/past-releases/
2.kibana的安装
下载地址:https://www.elastic.co/cn/downloads/
历史版本下载:https://www.elastic.co/cn/downloads/past-releases/
三.核心概念
1.索引(ElasticSearch)
- 包多个分片
2.字段类型(映射)
- 字段类型映射(字段是整型,还是字符型…)
3.文档
4.分片(Lucene索引,倒排索引)
![[Pasted image 20220701205838.png]]
![[Pasted image 20220701205853.png]]
四.分词器
1.分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把数据库中或者索引库中的数据进行分词,然后进行一一个匹配操作,默认的中文分词是将每个字看成一个词(不使用用IK分词器的情况下),比如“我爱狂神”会被分为”我”,”爱”,”狂”,”神” ,这显然是不符合要求的,所以我们需要安装中文分词器ik来解决这个问题。
2.安装
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
3.重启
4.查看插件
使用cmd,进入es的bin目录下,elasticsearch-plugin list
![[3.png]]
五.Rest的操作
1.![[Pasted image 20220704194848.png]]
2.创建一个索引
- put /索引名/文档id
3.字符串类型
- 字符串类型
- text、keyword
- text:支持分词,全文检索,支持模糊、精确查询,不支持聚合,排序操作;text类型的最大支持的字符长度无限制,适合大字段存储;
- keyword:不进行分词,直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
- text、keyword
- 数值型
- long、Integer、short、byte、double、float、half float、scaled float
- 日期类型
- date
- te布尔类型
- boolean
- 二进制类型
- binary
六.SpringBoot的集成
一.springboot项目集成
1.创建springboot项目
2.导入依赖
<properties>
<java.version>1.8</java.version>
<!-- 统一版本 -->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
3.导入elasticsearch
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
4.导入fastjson、lombok
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<!-- lombok需要安装插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
5.创建并且配置类
@Configuration
public class ElasticSearchConfig {
// 注册 rest高级客户端
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
)
);
return client;
}
}
6.创建并且配置实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = -3843548915035470817L;
private String name;
private Integer age;
}
二.索引的创建与解释
1.索引的创建
// 测试索引的创建, Request PUT liuyou_index
@Test
public void testCreateIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("fsy_index");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());// 查看是否创建成功
System.out.println(response);// 查看返回对象
restHighLevelClient.close();
}
2.索引的获取,并且判断是否存在
// 测试获取索引,并判断其是否存在
@Test
public void testIndexIsExists() throws IOException {
GetIndexRequest request = new GetIndexRequest("index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);// 索引是否存在
restHighLevelClient.close();
}
3.索引的删除
// 测试索引删除
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("fsy_index");
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());// 是否删除成功
restHighLevelClient.close();
}
七.项目实战
1.创建springboot项目
2.导入依赖
1. `<properties>`
2. `<java.version>1.8</java.version>`
3. `<elasticsearch.version>7.6.1</elasticsearch.version>`
4. `</properties>`
1. `<!-- jsoup解析页面 -->`
2. `<!-- 解析网页 爬视频可 研究tiko -->`
3. `<dependency>`
4. `<groupId>org.jsoup</groupId>`
5. `<artifactId>jsoup</artifactId>`
6. `<version>1.10.2</version>`
7. `</dependency>`
8. `<!-- fastjson -->`
9. `<dependency>`
10. `<groupId>com.alibaba</groupId>`
11. `<artifactId>fastjson</artifactId>`
12. `<version>1.2.70</version>`
13. `</dependency>`
14. `<!-- ElasticSearch -->`
15. `<dependency>`
16. `<groupId>org.springframework.boot</groupId>`
17. `<artifactId>spring-boot-starter-data-elasticsearch</artifactId>`
18. `</dependency>`
19. `<!-- thymeleaf -->`
20. `<dependency>`
21. `<groupId>org.springframework.boot</groupId>`
22. `<artifactId>spring-boot-starter-thymeleaf</artifactId>`
23. `</dependency>`
24. `<!-- web -->`
25. `<dependency>`
26. `<groupId>org.springframework.boot</groupId>`
27. `<artifactId>spring-boot-starter-web</artifactId>`
28. `</dependency>`
29. `<!-- devtools热部署 -->`
30. `<dependency>`
31. `<groupId>org.springframework.boot</groupId>`
32. `<artifactId>spring-boot-devtools</artifactId>`
33. `<scope>runtime</scope>`
34. `<optional>true</optional>`
35. `</dependency>`
36. `<!-- -->`
37. `<dependency>`
38. `<groupId>org.springframework.boot</groupId>`
39. `<artifactId>spring-boot-configuration-processor</artifactId>`
40. `<optional>true</optional>`
41. `</dependency>`
42. `<!-- lombok 需要安装插件 -->`
43. `<dependency>`
44. `<groupId>org.projectlombok</groupId>`
45. `<artifactId>lombok</artifactId>`
46. `<optional>true</optional>`
47. `</dependency>`
48. `<!-- test -->`
49. `<dependency>`
50. `<groupId>org.springframework.boot</groupId>`
51. `<artifactId>spring-boot-starter-test</artifactId>`
52. `<scope>test</scope>`
53. `</dependency>`
3.配置配置文件
# 更改端口,防止冲突
server.port=9999
# 关闭thymeleaf缓存
spring.thymeleaf.cache=false
4.编写代码以及测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gryzpz0H-1657329943665)(https://gitee.com/wochaojihaode/springboot-jd)]]