SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码(1)

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

@Test
void createIndex() {
    // 创建对象
    User user = User.builder().id(1001).name("1号用户").age(22).build();
    // 创建索引
    Index index = new Index.Builder(user).index("user").type("name").build();
    // 执行创建方法
    try {
        jestClient.execute(index);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}


创建成功:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/b001591d91e24a38ba72d8586feee1de.png#pic_center)  
 (2)查询索引【注入等代码不再贴出 只贴出方法】



@Test
void queryIndex(){
    // 查询语句【可以在HiJson工具里编辑】
    String queryJson = "{\n" +
            " \"query\": {\n" +
            " \"match\": {\n" +
            " \"name\": \"1号\"\n" +
            " }\n" +
            " }\n" +
            "}";
    // 创建查询
    Search search = new Search.Builder(queryJson).addIndex("user").addType("name").build();
    // 执行查询方法
    try {
        SearchResult result = jestClient.execute(search);
        System.out.println(result.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果【这里简单换个行 避免格式化后占较大篇幅】:



Result:
{“took”:47,“timed_out”:false,
“_shards”:{“total”:5,“successful”:5,“skipped”:0,“failed”:0},
“hits”:{“total”:1,“max_score”:0.5753642,
“hits”:[
{“_index”:“user”,“_type”:“name”,“_id”:“1001”,“_score”:0.5753642,
“_source”:{“id”:1001,“name”:“1号用户”,“age”:22}
}]}},
isSucceeded: true,
response code: 200,
error message: null


(3)更新索引【特别注意:更新语句官网给出的无法使用】



@Test
void updateIndex() {
    // 更新语句【可以在HiJson工具里编辑】
    String script = "{\n" +
            " \"script\": {\n" +
            " \"inline\": \"ctx.\_source.name += params.nameInfo\",\n" +
            " \"params\": {\n" +
            " \"new\_nameInfo\": \"(懂事长)\"\n" +
            " }\n" +
            " }\n" +
            "}";
    // 创建更新
    Update update = new Update.Builder(script).index("user").type("name").id("1001").build();
    // 执行更新方法
    try {
        DocumentResult result = jestClient.execute(update);
        System.out.println(result.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果:



Result: {“_index”:“user”,“_type”:“name”,“_id”:“1001”,“_version”:3,“result”:“updated”,
“_shards”:{“total”:2,“successful”:1,“failed”:0},“_seq_no”:2,“_primary_term”:1},
isSucceeded: true,
response code: 200,
error message: null


页面查看:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/ef51c99d00a4483a89c18b83929de2f5.png#pic_center)  
 (4)删除索引



@Test
void deleteIndex() {
    // 创建删除
    Delete delete = new Delete.Builder("1001").index("user").type("name").build();
    // 执行删除方法
    try {
        DocumentResult result = jestClient.execute(delete);
        System.out.println(result);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果:



Result: {“_index”:“user”,“_type”:“name”,“_id”:“1001”,“_version”:4,“result”:“deleted”,
“_shards”:{“total”:2,“successful”:1,“failed”:0},“_seq_no”:3,“_primary_term”:1},
isSucceeded: true,
response code: 200,
error message: null


### 6.总结



![img](https://img-blog.csdnimg.cn/img_convert/1ab36ec882785349741993cb478fe096.png)
![img](https://img-blog.csdnimg.cn/img_convert/dd8db6d961169f835e9ab25b34dd6a7f.png)
![img](https://img-blog.csdnimg.cn/img_convert/e080909aa0a01160792f38de99810343.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于Spring Boot集成Elasticsearch Java API Client,你可以按照以下步骤进行操作: 1. 首先,你需要在你的项目中添加Elasticsearch依赖。可以在官方文档()中找到相关的依赖信息。 2. 接下来,你可以通过创建低级别的RestClient来连接到Elasticsearch。你可以使用以下代码片段来创建一个基于RestClient的传输对象: ```java RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build(); ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); ElasticsearchClient client = new ElasticsearchClient(transport); ``` 3. 一旦你建立了与Elasticsearch的连接,你就可以使用ElasticsearchClient来执行各种操作。比如,你可以使用以下代码来创建一个索引: ```java CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("newapi")); ``` 在这个例子中,"newapi"是你要创建的索引的名称。 所以,以上是使用Spring Boot集成Elasticsearch Java API Client的基本步骤。你可以根据你的具体需求进一步使用Elasticsearch的API来完成其他操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springboo整合elasticSearch8 java client api](https://blog.csdn.net/A434534658/article/details/125239480)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值