springboot~集成elasticsearch的jest
jest是一批操作es的http api接口,你可以像使用普法方法一下操作es,在springboot2.3.0之前,JestClient是支持自动注入的,而在2.3.0之后,你必须为JestClient写一个组件类,通过注入组件类来使用jest,这一点有些麻烦了。
依赖包
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.7</version>
</dependency>
需要定义注册类
/**
* springboot2.3.0之后不支持自动注册,只能手动写注册配置文件.
*/
@Component
public class JestClientConfig {
@Bean
public io.searchbox.client.JestClient getJestCline() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")
.multiThreaded(true)
.build());
return factory.getObject();
}
}
在程序中使用它
@Autowired
JestClient jestClient;
/**
* 创建所引
*
* @throws IOException
*/
@Test
public void createIndex() throws IOException {
User user = new User("1", "张三", "test");
Index index = new Index.Builder(user).index(INDEX).type(TYPE).build();
try {
JestResult jr = jestClient.execute(index);
System.out.println(jr.isSucceeded());
} catch (IOException e) {
e.printStackTrace();
}
}
数据成功插入
合集:
springboot(1)
, elasticsearch
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2017-07-06 持续集成~Jenkins里的powershell插件发布远程站点了
2017-07-06 持续集成~Jenkins里的NuGet和MSBuild插件
2016-07-06 技巧~向URL地址添加参数
2016-07-06 EF架构~CodeFirst数据迁移与防数据库删除
2012-07-06 代码之美~强大的构造方法重载
2011-07-06 说说Remoting