springboot集成es详解
作者:qq_43381763 时间:2021-08-21 01:13:53
1.导入 maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-dataelasticsearch</artifactId>
<dependency>
注意 保持版本一致 我用的是7.6.2版本的
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.6.2</elasticsearch.version>
<!--自定义版本 保持版本一致-->
</properties>
2.编写config类 相当于 xlm导入文档
@Configuration
public class ESConfig {
@Bean
public RestHighLevelClient restHighLevelClient (){
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost",9100,"http")
)
);
return restHighLevelClient;
}
注意这里的端口号 一定不能搞错
3测试书写 添加 索引
@Test
void contextLoads() throws IOException {
//1.创建索引的请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest("mao");
//2.执行请求 获得响应
CreateIndexResponse createIndexResponse = estHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
4.查询索引是否存在
@Test //查询索引是否存在
void existIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("test"); //获得索引请求
boolean exists = estHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
5.删除索引
@Test//删除
void delIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test");
AcknowledgedResponse delete = estHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
System.out.println(delete.isAcknowledged());
}
6.添加文档数据 第一 要设置实体类 导入阿里巴巴JSON 工具类
@Data
@Accessors(chain = true) //实体类
public class User {
private String name;
private String age;
}
@Test //添加文档
void addDocument() throws IOException {
//创建对象啊
User user = new User().setAge("13").setName("mao");
//创建请求
IndexRequest request = new IndexRequest("mao");
//设置规则 PUT /test/_doc/id
request.id("1");
request.timeout("1s");
//将请求放入josn
request.source(JSON.toJSONString(user),XContentType.JSON);
//客户端发送请求
IndexResponse index = estHighLevelClient.index(request, RequestOptions.DEFAULT);
//获取响应结果
System.out.println(index.toString());
System.out.println(index.status());
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
7.修改文档
@Test //Update 文档操作
void GengXin() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("mao","1"); //请求更新文档
updateRequest.timeout("1s"); //设置超时时间
User user= new User().setName("张三").setAge("26");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON); //将对象封装丢进去 XContentType方法 将要传输的数据进行告知
UpdateResponse update = estHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//发送请求
System.out.println(update);
}
8.批量增加
@Test //批量丢入数据
void TestBulkIndexRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest(); //大批量导入数据 本质是for循环
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
for(int i=0;i<10;i++){
users.add(new User().setName("张三"+i+"号").setAge(""+i));
}
//批处理请求
for(int i =0;i<users.size();i++){
bulkRequest.add(
new IndexRequest("mao")
.id(""+(i+1))
.source(JSON.toJSONString(users.get(i)),XContentType.JSON)
);
}
BulkResponse bulk = estHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk);
System.out.println(bulk.hasFailures());//查询是否失败
}
9.精确查询
@Test//查询
void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest(ESConstant.ESConstant);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//精确查询条件
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "0");
//查询所有
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(termQueryBuilder) ;//将规则加入
// searchSourceBuilder.from(); //设置分页
// searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));//设置高并发下的延迟时间
searchSourceBuilder.highlighter();
searchRequest.source(searchSourceBuilder);//将刚刚做的请求体放入
SearchResponse search = estHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//请求信息
System.out.println(JSON.toJSONString(search.getHits()));//返回查询情况 getHits 封装返回对象
for( SearchHit SearchHit:search.getHits().getHits() ){
System.out.println(SearchHit.getSourceAsMap());
}
}
来源:https://blog.csdn.net/qq_43381763/article/details/109142241
标签:springboot,es
0
投稿
猜你喜欢
android接收到蓝牙配对请求时如何点亮屏幕具体实现
2023-08-11 03:28:38
Android使用FontMetrics对象计算位置坐标
2023-02-06 15:35:20
SpringBoot整合WebService服务的实现代码
2021-11-11 14:59:11
Android如何判断页面是否全屏
2021-08-31 14:03:22
C#中?、?.、??、??=运算符的用法
2022-11-10 18:11:51
C#实现关闭其他程序窗口或进程代码分享
2022-05-26 07:38:03
Java8 如何正确高效的使用并行流
2021-06-01 20:29:19
SpringBoot教程_创建第一个SpringBoot项目
2022-02-19 23:12:54
一文解决springboot打包成jar文件无法正常运行的问题
2021-11-03 14:54:28
C#使用smtp发送带附件的邮件实现方法
2022-11-03 17:53:39
android采用FFmpeg实现音视频合成与分离
2022-03-05 09:18:16
Java必备知识之位运算及常见进制解读
2022-02-10 00:42:19
spring boot 全局异常处理方法汇总
2021-07-06 22:44:04
C#中的委托介绍
2023-07-27 13:26:23
java中 String和StringBuffer的区别实例详解
2023-06-19 18:48:31
利用Android中BitmapShader制作自带边框的圆形头像
2021-12-25 20:09:11
关于C#继承的简单应用代码分析
2021-10-28 13:11:31
MyBatis-Plus实现逻辑删除的示例代码
2022-05-14 06:12:35
C#直线的最小二乘法线性回归运算实例
2022-05-03 11:19:33
Android item长按删除功能
2022-11-13 03:45:24