@springboot 2.3.0以后版本使用JestClient操作es
由于springboot 2.3.0以后版本不支持自动注入JestClient
解决方案:手动创建配置类
pom.xml
配置类
@Configuration
public class MyJestClient {
@Bean
public JestClient getJestCline(){
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")//自己elasticsearch安装位置的IP地址
.multiThreaded(true)
.build());
return factory.getObject();
}
}
实体类
自己生成get,set方法
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
测试
@Resource
JestClient jestClient;//自动注入import io.searchbox.client.JestClient;
@Test
public void contextLoads() {
Article article=new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("张山");
article.setContent("helloworid");
//构建一个索引功能
Index index = new Index.Builder(article).index("jiang").type("news").build();
try {
jestClient.execute(index);
}catch (IOException e){
e.printStackTrace();
}
}