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~集成elasticsearch的jest_数据

作者:仓储大叔,张占岭,
荣誉:微软MVP