springboot集成es詳解
1.導(dǎo)入 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.編寫(xiě)config類 相當(dāng)于 xlm導(dǎo)入文檔
@Configuration public class ESConfig { @Bean public RestHighLevelClient restHighLevelClient (){ RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost",9100,"http") ) ); return restHighLevelClient; }
注意這里的端口號(hào) 一定不能搞錯(cuò)
3測(cè)試書(shū)寫(xiě) 添加 索引
@Test void contextLoads() throws IOException { //1.創(chuàng)建索引的請(qǐng)求 CreateIndexRequest createIndexRequest = new CreateIndexRequest("mao"); //2.執(zhí)行請(qǐng)求 獲得響應(yīng) CreateIndexResponse createIndexResponse = estHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println(createIndexResponse); }
4.查詢索引是否存在
@Test //查詢索引是否存在 void existIndex() throws IOException { GetIndexRequest getIndexRequest = new GetIndexRequest("test"); //獲得索引請(qǐng)求 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.添加文檔數(shù)據(jù) 第一 要設(shè)置實(shí)體類 導(dǎo)入阿里巴巴JSON 工具類
@Data @Accessors(chain = true) //實(shí)體類 public class User { private String name; private String age; }
@Test //添加文檔 void addDocument() throws IOException { //創(chuàng)建對(duì)象啊 User user = new User().setAge("13").setName("mao"); //創(chuàng)建請(qǐng)求 IndexRequest request = new IndexRequest("mao"); //設(shè)置規(guī)則 PUT /test/_doc/id request.id("1"); request.timeout("1s"); //將請(qǐng)求放入josn request.source(JSON.toJSONString(user),XContentType.JSON); //客戶端發(fā)送請(qǐng)求 IndexResponse index = estHighLevelClient.index(request, RequestOptions.DEFAULT); //獲取響應(yīng)結(jié)果 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"); //請(qǐng)求更新文檔 updateRequest.timeout("1s"); //設(shè)置超時(shí)時(shí)間 User user= new User().setName("張三").setAge("26"); updateRequest.doc(JSON.toJSONString(user),XContentType.JSON); //將對(duì)象封裝丟進(jìn)去 XContentType方法 將要傳輸?shù)臄?shù)據(jù)進(jìn)行告知 UpdateResponse update = estHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);//發(fā)送請(qǐng)求 System.out.println(update); }
8.批量增加
@Test //批量丟入數(shù)據(jù) void TestBulkIndexRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); //大批量導(dǎo)入數(shù)據(jù) 本質(zhì)是for循環(huán) bulkRequest.timeout("10s"); ArrayList<User> users = new ArrayList<>(); for(int i=0;i<10;i++){ users.add(new User().setName("張三"+i+"號(hào)").setAge(""+i)); } //批處理請(qǐng)求 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) ;//將規(guī)則加入 // searchSourceBuilder.from(); //設(shè)置分頁(yè) // searchSourceBuilder.size(); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));//設(shè)置高并發(fā)下的延遲時(shí)間 searchSourceBuilder.highlighter(); searchRequest.source(searchSourceBuilder);//將剛剛做的請(qǐng)求體放入 SearchResponse search = estHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//請(qǐng)求信息 System.out.println(JSON.toJSONString(search.getHits()));//返回查詢情況 getHits 封裝返回對(duì)象 for( SearchHit SearchHit:search.getHits().getHits() ){ System.out.println(SearchHit.getSourceAsMap()); } }
到此這篇關(guān)于springboot集成es詳解的文章就介紹到這了,更多相關(guān)springboot集成es內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成redis與session實(shí)現(xiàn)分布式單點(diǎn)登錄
- SpringBoot集成Redis—使用RedisRepositories詳解
- springboot集成RestTemplate及常見(jiàn)的用法說(shuō)明
- Springboot集成restTemplate過(guò)程詳解
- springboot集成ES實(shí)現(xiàn)磁盤(pán)文件全文檢索的示例代碼
- SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法)
- springboot集成es插入和查詢的簡(jiǎn)單使用示例詳解
相關(guān)文章
詳解用Spring Boot Admin來(lái)監(jiān)控我們的微服務(wù)
這篇文章主要介紹了用Spring Boot Admin來(lái)監(jiān)控我們的微服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08關(guān)于RedisTemplate之opsForValue的使用說(shuō)明
這篇文章主要介紹了關(guān)于RedisTemplate之opsForValue的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06springboot實(shí)現(xiàn)文件上傳步驟解析
這篇文章主要介紹了springboot實(shí)現(xiàn)文件上傳步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12解決項(xiàng)目沒(méi)有build path的問(wèn)題
這篇文章主要介紹了解決項(xiàng)目沒(méi)有build path的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)
下面小編就為大家?guī)?lái)一篇ActiveMQ簡(jiǎn)單入門(mén)(新手必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06mybatis動(dòng)態(tài)SQL?if的test寫(xiě)法及規(guī)則詳解
這篇文章主要介紹了mybatis動(dòng)態(tài)SQL?if的test寫(xiě)法及規(guī)則詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01