您好,登錄后才能下訂單哦!
這篇文章運用簡單易懂的例子給大家介紹使用springboot如何實現集成es,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1.導入 maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-dataelasticsearch</artifactId> <dependency>
注意 保持版本一致 我用的是7.6.2版本的
<properties> <java.version>1.8</java.version> <elasticsearch.version>7.6.2</elasticsearch.version> <!--自定義版本 保持版本一致--> </properties>
2.編寫config類 相當于 xlm導入文檔
@Configuration public class ESConfig { @Bean public RestHighLevelClient restHighLevelClient (){ RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost",9100,"http") ) ); return restHighLevelClient; }
注意這里的端口號 一定不能搞錯
3測試書寫 添加 索引
@Test void contextLoads() throws IOException { //1.創建索引的請求 CreateIndexRequest createIndexRequest = new CreateIndexRequest("mao"); //2.執行請求 獲得響應 CreateIndexResponse createIndexResponse = estHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println(createIndexResponse); }
4.查詢索引是否存在
@Test //查詢索引是否存在 void existIndex() throws IOException { GetIndexRequest getIndexRequest = new GetIndexRequest("test"); //獲得索引請求 boolean exists = estHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT); System.out.println(exists); }
5.刪除索引
@Test//刪除 void delIndex() throws IOException { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test"); AcknowledgedResponse delete = estHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); System.out.println(delete); System.out.println(delete.isAcknowledged()); }
6.添加文檔數據 第一 要設置實體類 導入阿里巴巴JSON 工具類
@Data @Accessors(chain = true) //實體類 public class User { private String name; private String age; }
@Test //添加文檔 void addDocument() throws IOException { //創建對象啊 User user = new User().setAge("13").setName("mao"); //創建請求 IndexRequest request = new IndexRequest("mao"); //設置規則 PUT /test/_doc/id request.id("1"); request.timeout("1s"); //將請求放入josn request.source(JSON.toJSONString(user),XContentType.JSON); //客戶端發送請求 IndexResponse index = estHighLevelClient.index(request, RequestOptions.DEFAULT); //獲取響應結果 System.out.println(index.toString()); System.out.println(index.status());
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
7.修改文檔
@Test //Update 文檔操作 void GengXin() throws IOException { UpdateRequest updateRequest = new UpdateRequest("mao","1"); //請求更新文檔 updateRequest.timeout("1s"); //設置超時時間 User user= new User().setName("張三").setAge("26"); updateRequest.doc(JSON.toJSONString(user),XContentType.JSON); //將對象封裝丟進去 XContentType方法 將要傳輸的數據進行告知 UpdateResponse update = estHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//發送請求 System.out.println(update); }
8.批量增加
@Test //批量丟入數據 void TestBulkIndexRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); //大批量導入數據 本質是for循環 bulkRequest.timeout("10s"); ArrayList<User> users = new ArrayList<>(); for(int i=0;i<10;i++){ users.add(new User().setName("張三"+i+"號").setAge(""+i)); } //批處理請求 for(int i =0;i<users.size();i++){ bulkRequest.add( new IndexRequest("mao") .id(""+(i+1)) .source(JSON.toJSONString(users.get(i)),XContentType.JSON) ); } BulkResponse bulk = estHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk); System.out.println(bulk.hasFailures());//查詢是否失敗 }
9.精確查詢
@Test//查詢 void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest(ESConstant.ESConstant); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //精確查詢條件 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "0"); //查詢所有 // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); searchSourceBuilder.query(termQueryBuilder) ;//將規則加入 // searchSourceBuilder.from(); //設置分頁 // searchSourceBuilder.size(); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));//設置高并發下的延遲時間 searchSourceBuilder.highlighter(); searchRequest.source(searchSourceBuilder);//將剛剛做的請求體放入 SearchResponse search = estHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//請求信息 System.out.println(JSON.toJSONString(search.getHits()));//返回查詢情況 getHits 封裝返回對象 for( SearchHit SearchHit:search.getHits().getHits() ){ System.out.println(SearchHit.getSourceAsMap()); } }
關于使用springboot如何實現集成es就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。