参考: 2021版最新SpringBoot2_权威教程_请直接从P112开始学习新版视频--置顶评论有直达链接-_雷丰阳_尚硅谷
### 准备工作
1.elasticsearch镜像:我选的最新的elasticsearch:7.14.1
可进入 Docker hub 进行查看所有tag Docker hub - elasticsearch
2.一个linux环境
我有一个腾讯云服务器内存太小,创建容器后会自动关闭。后选择了用虚拟机。
### 安装ES
#### 1. 参考自 博客园:centos8 安装docker
首先更新一下:yum -y update
centos8默认使用podman代替docker,所以需要containerd.io,那我们就安装一下就好了
yum install https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm
安装一些其他依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装docker
yum install -y docker-ce
执行yum install -y docker-ce安装docker可能会有报错
按照提示执行 yum install --allowersaing docker -ce 就好了
#### 2.结束之后执行 docker version 看是否安装成功
#### 3. 启动docker服务
systemctl start docker
#### 4. 利用pull命令获取elasticsearch镜像
docker pull elasticsearch:7.14.1
#### 5. 查看系统镜像信息,可以看到已经存在elasticsearch的镜像
docker images
#### 6.利用镜像创建容器 docker run 命令的参数可以自己去了解
参考自 ElasticSearch的安装,使用,以及使用java Api操作Elasticsearch
#### 7. docker ps 查看容器
#### 8. 浏览器输入 ip:9200 显示一下内容表示elasticsearch安装成功
### 通过postman进行测试 elasticsearch官方文档
#### 1.1 PUT 一个文档到ES (id=6)
#### 1.2 查询id=6的文档
#### 2. GET id=6的文档
### elasticsearch Demo (springboot project)
#### 1. 加入jest依赖
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
#### 2. 创建pojo Article
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
#### 3.我的springboot版本不支持JestClient的@Autowired自动注入,需要手动获取JestClient
public class MyJestClient{
public JestClient getJestClient(){
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://ipaddress:9200/")
.multiThreaded(true)
.build());
return factory.getObject();
}
}
#### 4.测试
elasticsearch 的 PUT 操作
@SpringBootTest
class BootApplicationTests {
private MyJestClient jestClient = new MyJestClient();
@Test
void contextLoads() {
JestClient client = jestClient.getJestClient();
Article article = new Article();
article.setId(4);
article.setTitle("好消息");
article.setAuthor("zhangsan");
article.setContent("Hello World");
//构建一个索引
Index index = new Index.Builder(article).index("megacorp").type("employee").build();
try {
client.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
}
elasticsearch 的 GET 操作
@SpringBootTest
class BootApplicationTests {
private MyJestClient jestClient = new MyJestClient();
@Test
public void search(){
JestClient client = jestClient.getJestClient();
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"last_name\" : \"Smith\"\n" +
" }\n" +
" }\n" +
"}";
Search search = new Search.Builder(json).addIndex("megacorp").addType("employee").build();
try {
SearchResult result = client.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
}