springboot整合ES
在Springboot整合ES提供了启动依赖jar。 该jar包封了一个类: RestHighLevelClient
该类可以对象ES中各个接口进行相应的操作。
1. 新建项目
创建springboot工程并导入相关的依赖 2.3.12.RELEASE。最新版spring boot2.7.5中RestHighLevelClient已过时
2. 创建配置类
创建一个配置类,返回 RestHighLevelClient
package com.aaa.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
/**
* @author shang tf
* @createTime 2022/11/10 18:33
* @since 1.0.0
*/
public class ESConfig {
//springboot连接ES提供的一个客户端类。RestHighLevelClient
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client=new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
);
return client;
}
}
RestHighLevelClient的常见API
@Autowired
private RestHighLevelClient client;
操作名称 | 相关API |
---|---|
创建ES的索引 | client.indices().create(…) |
判断索引是否存在 | client.indices().exists(…) |
删除索引 | client.indices().delete(…) |
//创建ES的索引
@Test
void createIndex() throws Exception{
//创建CreateIndexRequest请求类---对应索引的创建需要的参数 都封装到该类中
CreateIndexRequest indexRequest=new CreateIndexRequest("qy156");
//关于对索引操作的功能都IndiceClient类中
CreateIndexResponse indexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT);
System.out.println("是否创建索引成功:"+indexResponse.isAcknowledged());
}
//判断索引是否存在.
@Test
public void isExist() throws IOException {
GetIndexRequest getIndexRequest=new GetIndexRequest("qy156");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println("索引是否存在:"+exists);
}
//删除索引:
@Test
public void delete() throws IOException{
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy158");
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println("是否删除成功:"+delete.isAcknowledged());
}
操作名称 | 相关API |
---|---|
往索引中添加文档 | client.index(…) |
根据id查询文档内容 | client.get(…) |
根据id删除文档 | client.delete(…) |
修改文档 | client.update(…) |
//往索引中添加文档
@Test
public void insertDoc() throws IOException {
IndexRequest indexRequest=new IndexRequest("qy156");
//指定文档的id值
indexRequest.id("1");
//指定文档的内容
Student student=new Student("李四","郑州","男",18);
indexRequest.source(JSON.toJSONString(student), XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("结果:"+index.getResult());
}
//根据id查询文档内容
@Test
public void findByid() throws IOException {
GetRequest getRequest=new GetRequest("qy156");
getRequest.id("1");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
//getSourceAsMap:拿到的结果封装为Map
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap.get("name")+"===="+sourceAsMap.get("sex"));
//getSourceAsString:拿到的结果为一个字符串
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
}
//根据id删除文档
@Test
public void deleteByid() throws IOException{
DeleteRequest deleteRequest=new DeleteRequest("qy156");
deleteRequest.id("1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.getResult());
}
//修改文档
@Test
public void update() throws IOException{
UpdateRequest updateRequest=new UpdateRequest("qy156","1");
Student student=new Student();
student.setName("张学友");
updateRequest.doc(JSON.toJSONString(student),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getResult());
}