日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2024/10/6 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SpringBoot集成ElasticSearch 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(配置+增删改查测试源码)【推荐使用】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.簡介

SpringBoot 項目初始化時就有 NoSQL 選項 Spring Data Elasticsearch(Access+Driver) 此時 pom 文件里引入的依賴是 spring-boot-starter-data-elasticsearch 它的版本受到 springboot 版本的限制,不能自由選擇對應的 ES 版本。


還有另一個選擇就是 Jest,以下是 官網 的介紹:

簡單說就是:ES 有 Java API 但是沒有 Http Rest interface,Jest 就是它的 HTTP Client。

2.依賴

<!-- https://mvnrepository.com/artifact/io.searchbox/jest --> <dependency><groupId>io.searchbox</groupId><artifactId>jest</artifactId><version>6.3.1</version> </dependency>

3.配置

spring:elasticsearch:jest:uris: http://localhost:9200

4.使用

(1)創建客戶端【官網代碼】

// Construct a new Jest client according to configuration via factoryJestClientFactory factory = new JestClientFactory();factory.setHttpClientConfig(new HttpClientConfig.Builder("http://localhost:9200").multiThreaded(true)//Per default this implementation will create no more than 2 concurrent connections per given route.defaultMaxTotalConnectionPerRoute(<YOUR_DESIRED_LEVEL_OF_CONCURRENCY_PER_ROUTE>)// and no more 20 connections in total.maxTotalConnection(<YOUR_DESIRED_LEVEL_OF_CONCURRENCY_TOTAL>).build());JestClient client = factory.getObject();

(2)直接注入【配置文件進行參數配置】

@AutowiredJestClient jestClient;

5.測試源碼

(1)創建索引

@SpringBootTest class EsJestApplicationTests {@AutowiredJestClient jestClient;@Testvoid 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();}} }

創建成功:

(2)查詢索引【注入等代碼不再貼出 只貼出方法】

@Testvoid 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)更新索引【特別注意:更新語句官網給出的無法使用】

@Testvoid 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

頁面查看:

(4)刪除索引

@Testvoid 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.總結

使用 Jest 的靈活性明顯要比 Java api 要高,我們可以將增刪改查的創建過程進行封裝,輸入參數獲取解析后的結果。

總結

以上是生活随笔為你收集整理的【SpringBoot集成ElasticSearch 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(配置+增删改查测试源码)【推荐使用】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。