Springboot2.x整合Elasticsearch(6.8.6版本)
1.添加依赖
Springboot使用版本为2.3.4.RELEASE,Elasticsearch版本为6.8.6
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.6</version>
</dependency>
2.在application.yml配置elasticsearch
在spring节点下添加
#Elasticsearch配置
elasticsearch:
rest:
uris: http://127.0.0.1:9200 #ip是我的ES server地址
3.编写测试类测试CRUD操作
测试类里使用到了Hutool工具类,必要时请在pom.xml中添加hutool依赖。
假设ES有以下索引数据:

package com.simoniu.exam.elsearch;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticSearchTest {
//学生内部类
@Data
@AllArgsConstructor
@NoArgsConstructor
class Students implements Serializable {
private String sid;
private String sname;
private String gender;
private Integer score;
}
@Resource
private RestHighLevelClient restHighLevelClient;
//测试新增学生
@Test
public void testSaveStudents() {
IndexRequest indexRequest = null;
IndexResponse indexResponse = null;
try {
indexRequest = new IndexRequest("students", "_doc", "8");
Students stu = new Students("s0008", "风清扬", "男", 98);
String jsonStr = JSONUtil.toJsonStr(stu);
indexRequest.source(jsonStr, XContentType.JSON);
indexResponse = null;
indexResponse = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
}
//测试查询学生
@Test
public void testSearchStudentById() {
GetRequest request = new GetRequest("students", "8");
try {
GetResponse response = restHighLevelClient.get(request,RequestOptions.DEFAULT);
Map<String, Object> result = response.getSource();
Students s = JSONUtil.toBean(JSONUtil.toJsonStr(result), Students.class);
System.out.println(s);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//测试修改学生
@Test
public void testUpdateStudentsById() {
IndexRequest indexRequest = null;
IndexResponse indexResponse = null;
try {
indexRequest = new IndexRequest("students", "_doc", "8");
Students stu = new Students("s0008", "东方不败", "女", 88);
String jsonStr = JSONUtil.toJsonStr(stu);
indexRequest.source(jsonStr, XContentType.JSON);
indexResponse = null;
indexResponse = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
}
//测试删除学生
@Test
public void testDeleteStudentsById() {
DeleteRequest request = new DeleteRequest("students", "8");
try {
DeleteResponse response = restHighLevelClient.delete(request,RequestOptions.DEFAULT);
System.out.println(response.getResult());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}