【SpringBoot集成ElasticSearch 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(配置+增删改查测试源码)【推荐使用】
生活随笔
收集整理的這篇文章主要介紹了
【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:92004.使用
(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)查詢索引【注入等代碼不再貼出 只貼出方法】
輸出結果【這里簡單換個行 避免格式化后占較大篇幅】:
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)刪除索引
輸出結果:
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: null6.總結
使用 Jest 的靈活性明顯要比 Java api 要高,我們可以將增刪改查的創建過程進行封裝,輸入參數獲取解析后的結果。
總結
以上是生活随笔為你收集整理的【SpringBoot集成ElasticSearch 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(配置+增删改查测试源码)【推荐使用】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java代码】反射机制处理传递给map
- 下一篇: 【Java代码】使用双冒号 :: 简洁代