springboot集成elasticsearch7的圖文方法
1.創(chuàng)建項(xiàng)目
修改依賴版本
2.創(chuàng)建配置文件
package com.huanmingjie.elasticsearch.config; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ElasticsearchClientConfig { @Bean public RestHighLevelClient restHighLevelClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); return client; } }
3.測試
3.1索引操作
1.創(chuàng)建索引
2.判斷索引是否存在
3.刪除索引
索引操作代碼
package com.huanmingjie.elasticsearch; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; @SpringBootTest class ElasticsearchApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; //創(chuàng)建索引 PUT zoomy_index @Test void createIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("zoomy_index"); restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); } //判斷索引是否存在 @Test void getIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("zoomy_index"); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //刪除索引 @Test void deleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } }
3.2文檔操作
創(chuàng)建實(shí)體類
package com.huanmingjie.elasticsearch.pojo; import org.springframework.stereotype.Component; @Component public class User { private String name; private int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
1.添加文檔
2.獲取文檔,判斷是否存在
3.獲取文檔信息
4.更新文檔
5.刪除文檔
3.3實(shí)戰(zhàn)操作
批量創(chuàng)建數(shù)據(jù)
查詢
package com.huanmingjie.elasticsearch; import com.alibaba.fastjson.JSON; import com.huanmingjie.elasticsearch.pojo.User; import com.huanmingjie.elasticsearch.utils.ESConstant; import net.minidev.json.JSONObject; import org.apache.lucene.util.QueryBuilder; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.naming.directory.SearchResult; import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.concurrent.TimeUnit; @SpringBootTest class ElasticsearchApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; //創(chuàng)建索引 PUT zoomy_index @Test void createIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("zoomy_index"); restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); } //判斷索引是否存在 @Test void getIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("zoomy_index"); boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //刪除索引 @Test void deleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index"); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } //添加文檔 PUT zoomy_index/_doc/1 @Test void addDocument() throws IOException { User user = new User("zoomy", 21); IndexRequest request = new IndexRequest("zoomy_index"); request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); request.source(JSON.toJSONString(user), XContentType.JSON); //客戶端發(fā)送請求,獲取響應(yīng)結(jié)果 IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); //命令返回的狀態(tài) System.out.println(indexResponse.status()); } //獲取文檔,判斷是否存在 @Test void exitDocument() throws IOException { GetRequest request = new GetRequest("zoomy_index", "1"); //不獲取返回的_source的上下文,效率更高 request.fetchSourceContext(new FetchSourceContext(false)); request.storedFields("_none_"); boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT); System.out.println(exists); } //獲取文檔信息 @Test void getDocument() throws IOException { GetRequest request = new GetRequest("zoomy_index", "1"); GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT); //打印文檔內(nèi)容 System.out.println(getResponse.getSourceAsString()); //返回全部內(nèi)容 System.out.println(getResponse); } //更新文檔 POST zoomy_index/_doc/1/_update @Test void updateDocument() throws IOException { UpdateRequest request = new UpdateRequest("zoomy_index", "1"); request.timeout(TimeValue.timeValueSeconds(1)); User user = new User("zoomy", 22); request.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(updateResponse.status()); } //刪除文檔 @Test void deleteDocument() throws IOException { DeleteRequest request = new DeleteRequest("zoomy_index", "1"); DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(deleteResponse.status()); } //批量處理數(shù)據(jù) @Test void bulkRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout(TimeValue.timeValueSeconds(10)); ArrayList<User> userList = new ArrayList<>(); userList.add(new User("zoomy1", 21)); userList.add(new User("zoomy2", 22)); userList.add(new User("zoomy3", 23)); for (int i = 0; i < userList.size(); i++) { bulkRequest.add( new IndexRequest("zoomy_index") .id("" + (i + 1)) .source(JSON.toJSONString(userList.get(i)), XContentType.JSON)); } BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkItemResponses.hasFailures()); } //批量處理數(shù)據(jù) @Test void searchRequest() throws IOException { SearchRequest searchRequest = new SearchRequest(ESConstant.ZOOMY_INDEX); //構(gòu)建搜索條件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //查詢條件QueryBuilders工具 termQuery 精確查詢 matchAllQuery匹配所有 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zoomy1"); // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); searchSourceBuilder.query(termQueryBuilder); //from size有默認(rèn)參數(shù) // searchSourceBuilder.from(); // searchSourceBuilder.size(); searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); for (SearchHit hit : searchResponse.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); } } }
以上就是springboot集成elasticsearch7的詳細(xì)內(nèi)容,更多關(guān)于springboot集成elasticsearch7的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
BeanFactory與ApplicationContext的區(qū)別示例解析
這篇文章主要為大家介紹了BeanFactory與ApplicationContext的區(qū)別示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果
今天小編就為大家分享一篇關(guān)于Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Java matches類,Pattern類及matcher類用法示例
這篇文章主要介紹了Java matches類,Pattern類及matcher類用法,結(jié)合實(shí)例形式分析了java matches類,Pattern類及matcher類針對字符串常見操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-03-03java 內(nèi)部類(匿名類,匿名對象,靜態(tài)內(nèi)部類)詳解及實(shí)例
這篇文章主要介紹了java 內(nèi)部類詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12kafka運(yùn)維consumer-groups.sh消費(fèi)者組管理
這篇文章主要為大家介紹了kafka運(yùn)維consumer-groups.sh消費(fèi)者組管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Java使用FileInputStream流讀取文件示例詳解
這篇文章主要介紹了Java使用FileInputStream流讀取文件示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java開發(fā)之手把手教你搭建企業(yè)級工程SSM框架
這篇文章主要為大家介紹Java教程中搭建企業(yè)級工程SSM框架,手把手的過程操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例
本文主要介紹了SpringSecurity實(shí)現(xiàn)權(quán)限認(rèn)證與授權(quán)的使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11