Springboot整合ES调用Jest(2.2版本以后)

直接说操作吧,只需要勾选springboot的web模块:

然后在pom文件中添加下面的依赖:

     	<dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>6.3.1</version>
        </dependency>

博主想按照书上写的,在application.properties中指定一下elasticsearch服务部的ip和端口,结果发现这种方法已经过时了:在这里插入图片描述
并且,jestClient没有自动注入进来,博主猜想大概是springboot2.2版本以后以及没有自动给容器注入jest了。

在这里插入图片描述

到这里,很多小伙伴决定放弃jest的学习,毕竟这个方法都过时了。但博主依旧发挥共产党的坚持不懈精神,springboot不自动注入,那我就自己注入,将下面的bean 纳入Ioc容器。该bean依旧解决了指定elastic服务地址的任务。所以再无需任何配置, 直接使用。

@Configuration
public class MyConfig {
    @Bean
    public JestClient jestClient(){
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder("http://192.168.31.53:9200")
                .multiThreaded(true)
                .build());
        return  factory.getObject();
    }
    
}

使用实例:

@SpringBootTest
class BootelasticsearchApplicationTests {

    @Autowired
    JestClient jestClient;

    
    //放数据
    @Test
    void contextLoads() {
        Article article = new Article();
        article.setId(1);
        article.setTitle("good night");
        article.setAuthor("lzl");
        article.setContent("hello world");
        Index index = new Index.Builder(article).index("lzl").type("news").build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   //查数据
    @Test
    public void search(){
       String json = "{\n" +
               "  \"query\" : {\n" +
               "  \t\t\"match\" : {\n" +
               "  \t\t\t\"content\" : \"hello\"\n" +
               "  \t\t}\n" +
               "  }\n" +
               "}";
       //构建搜索功能
        Search search = new Search.Builder(json).addIndex("lzl").addType("news").build();
        try {
            SearchResult execute = jestClient.execute(search);
            System.out.println(execute.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘歌唱家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值