首页 > 编程语言 > 详细

springboot~集成elasticsearch的jest

时间:2020-07-06 16:25:14      阅读:52      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/lori/p/13255199.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!