Springboot版本2.3.3
ElasticSearch版本7.6.1
Springboot以往整合ElasticSearch的方法
1.Jest(已经被废除)
jest已经被取消
2.ElasticSearchTemplate(已经被废除)
3.ElasticSearchRestTemplate
创建索引的时候
唉~~~连接ElasticSearch可太难了
好了,下面进入正题
1.导入依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
2.创建连接客户端
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http") //ip地址,端口号,连接方式
)
);
return client;
}
}
3.创建索引
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
void testCreateIndex() throws IOException {
//创建索引请求
CreateIndexRequest request = new CreateIndexRequest("test_index");
//执行请求indices,请求后获得响应
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
启动测试(前提是ElasticSearch已经启动)
通过ElasticSearch-head来查看索引是否创建成功
索引创建成功
4.测试指定索引是否存在
//测试索引是否存在
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("test_index"); //测试test_index索引是否存在
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("索引是否存在:"+exists);
}
5.删除索引
//删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
索引已经被删除
6.向索引中添加文档
//添加文档
@Test
void testAddDocument() throws IOException {
//创建对象
User user = new User("张三",20);
//创建请求
IndexRequest request = new IndexRequest("test_index");
// put /test_index/_doc/1
request.id("1");
//设置超时时间
//request.timeout(TimeValue.timeValueSeconds(5));
//将user对象转换为json字符串
String value = new ObjectMapper().writeValueAsString(user);
//将数据放入请求
request.source(value,XContentType.JSON);
//执行请求
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
}
控制台输出(记得把之前删除的test_index索引再创建回来)
7.判断文档是否存在
//判断文档是否存在
@Test
void testExistDoc() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
//不获取返回的_source的上下文了
//request.fetchSourceContext(new FetchSourceContext(false));
//request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println("文档是否存在:"+exists);
}
8.获取文档
//获取文档
@Test
void testGetDoc() throws IOException {
GetRequest request = new GetRequest("test_index", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
}
9.更新文档
//更新文档
@Test
void testUpdateDoc() throws IOException {
UpdateRequest request = new UpdateRequest("test_index", "1");
//超时时间
//request.timeout("5s");
User user = new User("李四", 18);
String value = new ObjectMapper().writeValueAsString(user);
request.doc(value,XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
System.out.println(response);
}
10.删除文档
//删除文档
@Test
void testDeleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest("test_index", "1");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
System.out.println(response);
}
11.批量插入数据
//批量插入数据
@Test
void testBulkRequest() throws IOException {
BulkRequest request = new BulkRequest();
ArrayList users = new ArrayList<>();
users.add(new User("user1",20));
users.add(new User("user2",30));
users.add(new User("user3",25));
users.add(new User("user4",24));
int i=0;
ObjectMapper mapper = new ObjectMapper();
for (User user:users){
request.add(
new IndexRequest("test_index")
.id(""+(++i))
.source(mapper.writeValueAsString(user),XContentType.JSON)
);
}
BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.hasFailures()); //是否成功,返回false代表成功
}
其中id可以不设置,默认随机生成
12.搜索
//查询
@Test
void testSearch() throws IOException {
SearchRequest request = new SearchRequest("test_index");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件 可以使用QueryBuilders工具来实现
//QueryBuilders.termQuery精确匹配
TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name", "user1");
sourceBuilder.query(queryBuilder);
sourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));
sourceBuilder.from(0); //查询结果从第几条数据开始返回
sourceBuilder.size(5);//一次返回几条数据
request.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
System.out.println(response.getHits());
//遍历输出
for (SearchHit fields : response.getHits().getHits()) {
System.out.println(fields.getSourceAsString());
}
}