1、介绍
ElasticSearch是Java开发的一款开源的,分布式的搜索引擎。
它的搜索采用内存中检索的方式,大大提高了检索的效率,es是基于REST API的方式对数据操作的,可以让存储、检索、索引效率更高。
1、es可以做什么
-
网站检索数据,比如说购物网站,检索商品名称
-
搜索引擎模糊查询,比如说百度搜索
-
等等
主要就是做数据检索功能的,而且很多时候也会配合关系型数据库或非关系型数据库操作数据。
2、es使用场景
-
大数据量基础上,数据太少没必要用它
-
涉及到复杂检索情况
-
支持对数据的管理,不单单是一个搜索引擎
-
等等
2、环境搭建
1、下载镜像
#命令1:
docker pull elasticsearch:7.6.2
#命令2:
docker pull kibana:7.6.2
【执行命令1】
【执行命令2】
2、docker配置
1.创建挂载路径
创建两个目录文件,config和data,用来存放配置文件和数据。
2.分配访问权限
chmod -R 777 /usr/local/docker/es/
chmod -R 777 /usr/local/docker/es/config
chmod -R 777 /usr/local/docker/es/data
3.添加访问配置,允许任何ip访问
echo "http.host: 0.0.0.0" > /usr/local/docker/es/config/elasticsearch.yml
vim config/elasticsearch.yml
3、启动ElasticSearch
docker run --name es -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /usr/local/docker/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /usr/local/docker/es/data:/usr/share/elasticsearch/data \
-v /usr/local/docker/es/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.6.2
discovery.type=single-node : 以单节点方式运行
ES_JAVA_OPTS : 设置es的内存,因为es对内存占用比较大,学习时设置小一点方便学习
4、访问es
查看elasticsearch版本信息:http://192.168.142.128:9200/ ip加端口号
5、启动Kibana
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://服务器ip地址:9200 -p 5601:5601 -d kibana:7.6.2 注意ip是自己的
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.142.128:9200 -p 5601:5601 -d kibana:7.6.2
6、应用Kibana
访问地址:http://192.168.142.128:5601
7、CRUD
使用Kibana代码,来操作es。
# 增 /student/_doc/1 student是表名,_doc是文档类型的记录,1是主键的值;
POST /student/_doc/1
{
"id":"1",
"name":"张三",
"age":21,
"scoreList":[
{"id":"1","name":"数学", "score":95},
{"id":"2","name":"语文", "score":98},
{"id":"3","name":"外语", "score":90}
]
}
POST /student/_doc/2
{
"id":"2",
"name":"李四",
"age":22,
"scoreList":[
{"id":"4","name":"数学", "score":85},
{"id":"5","name":"语文", "score":88},
{"id":"6","name":"外语", "score":80}
]
}
POST /student/_doc/3
{
"id":"3",
"name":"王五",
"age":23,
"scoreList":[
{"id":"7","name":"数学", "score":75},
{"id":"8","name":"语文", "score":78},
{"id":"9","name":"外语", "score":70}
]
}
# 查
GET /student/_doc/3
# 查所有
GET /student/_search
# 改
PUT /student/_doc/3
{
"id":"3",
"name":"王六",
"age":26,
"scoreList":[
{"id":"7","name":"数学", "score":65},
{"id":"8","name":"语文", "score":78},
{"id":"9","name":"外语", "score":70}
]
}
# 删
DELETE /student/_doc/3
8、分词(了解)
可以安装分词插件,实现分词检索。
3、SpringBoot整合
1、整合方式
1.SpringBoot自带插件(了解)
这种方式比较容易上手,类似于之前学习其它中间件时的XXX-starter的操作,但是更新比较慢,支持的版本较低,功能不够强大。
2.发送请求(整合使用)
任何一个可以发送REST请求的方式都可以请求ES,官方推荐的是Elasticsearch-Rest-Client依赖包,我们采用最多的也是这种方式,帮我们封装了很多的方法。
官网地址: Java High Level REST Client | Java REST Client [7.17] | Elastic
2、Springboot整合Elasticsearch-Rest-Client
1.导入依赖
这里的版本要和所按照的ELK版本匹配。
只接用后面常规依赖
#es依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
#常规依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<jwt.version>0.7.0</jwt.version>
<fastjson.version>1.2.60</fastjson.version>
<spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.6.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
2.准备基础类
两个实体类一个启动器
@Data
public class Score {
private String id;
private String name;
private Double score;
}@Data
public class Student {
private String id;
private String name;
private int age;
private List<Score> scoreList;}
@SpringBootApplication
public class SpringBootESMain {public static void main(String[] args) {
SpringApplication.run(SpringBootESMain.class,args);
}
}
3.准备配置文件
两个配置文件
#application.properties
spring.application.name=demoes#application.yml
server:
port: 100
4.编写测试类
(1)填写配置类
配置类中将RestHighLevelClient添加到IoC容器中
package com.jr.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class EsConfig { @Bean public RestHighLevelClient restHighLevelClient() { RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.142.128", 9200, "http"));//注意IP改为自己的 return new RestHighLevelClient(restClientBuilder); } }
(2)测试
官方文档介绍保存数据功能:
Index API | Java REST Client [7.17] | Elastic
官方文档介绍查询数据功能:
Search API | Java REST Client [7.17] | Elastic
package com.jr;
import com.alibaba.fastjson.JSON;
import com.jr.entry.Score;
import com.jr.entry.Student;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootESMain.class) // 启动器名
public class EsTest {
@Autowired
private RestHighLevelClient client;
/**
* 保存数据
*/
@Test
public void save() throws IOException {
/*创建索引名student,student相当于表名,将数据保存到哪里*/
IndexRequest indexRequest = new IndexRequest("student");
Student student = new Student();
student.setAge(20);
student.setId("5");
student.setName("钱六");
Score score1 = new Score();
score1.setName("数学");
score1.setScore(89.0);
score1.setId("shuxue");
Score score2 = new Score();
score2.setName("语文");
score2.setScore(121.0);
score2.setId("yuwen");
Score score3 = new Score();
score3.setName("外语");
score3.setScore(124.0);
score3.setId("waiyu");
List<Score> scoreList = new ArrayList<>();
scoreList.add(score1);
scoreList.add(score2);
scoreList.add(score3);
student.setScoreList(scoreList);
/*保存数据也是向es发送了rest请求,保存需要一个json的数据,将对象转化成JSON串*/
String studentJson = JSON.toJSONString(student);
//向 Elasticsearch 索引中添加一个文档,其中 studentJson 是要添加的文档的 JSON 格式数据,
indexRequest.source(studentJson, XContentType.JSON);
//执行请求,获得响应。
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
/**
* 查询数据
*/
@Test
public void select() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/*查询的字段要加上keyword,表示按照数据中这个字段值查询,不加就查询不到;id主键除外!*/
TermQueryBuilder termQuery = QueryBuilders.termQuery("name.keyword", "张三");
searchSourceBuilder.query(termQuery);
searchRequest.source(searchSourceBuilder);
//执行请求,获得响应。
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
}
es中,最常用的两个功能就是保存数据和查询数据。它是一个检索引擎,它的作用不是用来存储数据的,而是用来检索数据、搜索数据的。它能帮助咱们快速的去搜索数据,我们往es里放的数据,目的都是为了后面检索能快一点,所以咱们往里放的一般都是查询关键字,再加上关键字对应的id,让es查询这些关键字,找到这些id,把这些id放到项目代码里,然后代码里拿着这些id去关系型数据库或者非关系型数据库里查询这些数据。