當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【SpringBoot集成ElasticSearch 01】2️⃣ 种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)
生活随笔
收集整理的這篇文章主要介紹了
【SpringBoot集成ElasticSearch 01】2️⃣ 种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.方式一
1.1 依賴
【不使用 spring-boot-starter-data-elasticsearch 就可以脫離 springboot 版本的限制,可以自行選擇ES的版本】我用的是 springboot 2.2.5.RELEASE 版本,ES部署文件為 elasticsearch-6.4.3.tar.gz,這里只貼出主要依賴:
<!-- ElasticSearch Server --> <dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.4.3</version> </dependency> <!-- Java High Level REST Client --> <dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.4.3</version> </dependency>1.2 配置信息
這里根據(jù)elasticsearch服務(wù)端的地址進(jìn)行配置:
@Configuration public class ElasticSearchConfig {@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));return client;} }1.3 客戶端使用
@SpringBootTest class EsApiApplicationTests {@Autowired@Qualifier(value = "restHighLevelClient")private RestHighLevelClient client;/*1.創(chuàng)建索引*/@Testvoid createIndex() throws IOException {// 創(chuàng)建請求CreateIndexRequest request = new CreateIndexRequest("yz_index");// 執(zhí)行請求,獲取響應(yīng)CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);System.out.println("creatIndex--" + response);}/*2.獲取索引,判斷是否存在*/@Testvoid getIndex() throws IOException {// 創(chuàng)建請求GetIndexRequest request = new GetIndexRequest("yz_index");// 執(zhí)行請求,獲取響應(yīng)boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println("getIndex--exists--" + exists);}/*3.刪除索引*/@Testvoid deleteIndex() throws IOException {// 創(chuàng)建請求DeleteIndexRequest request = new DeleteIndexRequest("yz_index");// 執(zhí)行請求AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println("deleteIndex--" + response);}/*4.添加文檔*/@Testvoid addDocument() throws IOException {// 創(chuàng)建對象User user = new User("Java虛擬機(jī)", 30);// 創(chuàng)建索引,即獲取索引IndexRequest indexRequest = new IndexRequest("yz_index");// 添加規(guī)則 /index/_doc/idindexRequest.id("1");indexRequest.timeout(TimeValue.timeValueSeconds(1));// 存入對象indexRequest.source(JSON.toJSONString(user), XContentType.JSON);// 發(fā)送請求IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println("addDocument--" + response);}/*5.獲取文檔是否存在*/@Testvoid getDocument() throws IOException {// 創(chuàng)建get請求GetRequest getRequest = new GetRequest("yz_index", "1");// 不獲取返回的_source的上下文getRequest.fetchSourceContext(new FetchSourceContext(false));getRequest.storedFields("_none_");// 發(fā)送請求boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);System.out.println("addDocument--" + exists);/*6.獲取文檔信息*/if (exists) {// 創(chuàng)建請求GetRequest getRequest1 = new GetRequest("yz_index", "1");// 發(fā)送請求GetResponse response = client.get(getRequest1, RequestOptions.DEFAULT);System.out.println("source--" + response.getSource());System.out.println("getDocument--" + response.getSourceAsString());}/*7.更新文檔信息*/if (exists) {// 創(chuàng)建請求UpdateRequest updateRequest = new UpdateRequest("yz_index", "1");updateRequest.timeout("1s");// 修改內(nèi)容User user = new User("小米", 10);updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);System.out.println("updateDocument--" + response.status());}/*8.刪除文檔信息*/if (exists) {// 創(chuàng)建請求DeleteRequest deleteRequest = new DeleteRequest("yz_index", "1");deleteRequest.timeout("1s");// 修改內(nèi)容DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println("deleteDocument--" + response.status());}}/*9.批量添加文檔*/@Testvoid batchAddDocument() throws IOException {// 批量請求BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");// 創(chuàng)建對象ArrayList<User> userArrayList = new ArrayList<>();userArrayList.add(new User("小米1", 1));userArrayList.add(new User("小米2", 2));userArrayList.add(new User("小米3", 3));userArrayList.add(new User("小米4", 4));userArrayList.add(new User("小米5", 5));// 添加請求for (int i = 0; i < userArrayList.size(); i++) {bulkRequest.add(new IndexRequest("yz_index").id("" + (i + 2)).source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON));}// 執(zhí)行請求BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println("batchAddDocument--" + response.status());}/*10.查詢*/@Testvoid search() throws IOException {// 創(chuàng)建請求SearchRequest searchRequest = new SearchRequest("jd_index");// 構(gòu)建搜索條件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "杜伽");// 精確查詢searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));searchRequest.source(searchSourceBuilder);// 執(zhí)行請求SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);// 解析查詢結(jié)果System.out.println("search--getHists--" + JSON.toJSONString(response.getHits()));for (SearchHit documentFields : response.getHits()) {System.out.println("search--getHist--" + documentFields.getSourceAsMap());}} }2.方式二
2.1 依賴
我用的是 springboot 2.5.4【所以spring-boot-starter-data-elasticsearch 的版本也是 2.5.4】此時對應(yīng)的 elasticsearch 服務(wù)端和客戶端的版本是 7.12.1 那要部署的ES版本也要是 7.12.1:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>2.2 配置信息
spring:elasticsearch:rest:uris: localhost:92002.3 客戶端使用
@SpringBootTest class EsStarterApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testvoid contextLoads() {// 由于不同版本的API不同【沒有參考價值】,這里不再貼出測試代碼。}}3.注意事項
不同的 elasticsearch 版本有不同的 API 這就給升級造成了阻礙,為了避免這種情況,我們可以使用elasticsearch的 HTTP 客戶端 Jest,下篇文章會詳細(xì)說明
總結(jié)
以上是生活随笔為你收集整理的【SpringBoot集成ElasticSearch 01】2️⃣ 种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JavaScript代码】使用Java
- 下一篇: 【Spring Boot 分享】开源项目