ES 關於文件的API操作
阿新 • • 發佈:2020-09-11
新增FastJSON依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
關於文件的操作
package com.dance.danceesapi.test; import com.alibaba.fastjson.JSON; importcom.dance.danceesapi.pojo.User; 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.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse;import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; 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.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; /** * 關於文件的API的操作 */ @SpringBootTest public class TestDocument { @Autowired @Qualifier("restHighLevelClient") RestHighLevelClient restHighLevelClient; /** * 測試新增文件 * @throws IOException */ @Test void addDocument() throws IOException { // 建立文件物件 User user = new User("彼岸舞111", 18); // 指定索引庫 IndexRequest flower = new IndexRequest("flower"); // 設定引數 id 超時時間 和資料來源 flower.id("4").timeout(TimeValue.timeValueSeconds(5)).source(JSON.toJSONString(user), XContentType.JSON); // 執行請求 IndexResponse index = restHighLevelClient.index(flower, RequestOptions.DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } /** * 測試文件是否存在 * @throws IOException */ @Test void existDocument() throws IOException { GetRequest flower = new GetRequest("flower", "1"); boolean exists = restHighLevelClient.exists(flower, RequestOptions.DEFAULT); System.out.println(exists); } /** * 獲取文件資訊 * @throws IOException */ @Test void getDocument() throws IOException { GetRequest flower = new GetRequest("flower", "1"); GetResponse documentFields = restHighLevelClient.get(flower, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); } /** * 測試文件的更新 * @throws IOException */ @Test void updateDocument() throws IOException { User user = new User("彼岸草小姐姐",19); UpdateRequest flower = new UpdateRequest("flower","1"); flower.timeout(TimeValue.timeValueSeconds(5)); flower.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse update = restHighLevelClient.update(flower, RequestOptions.DEFAULT); System.out.println(update); System.out.println(update.status()); } /** * 測試文件的刪除 * @throws IOException */ @Test void deleteDocument() throws IOException { DeleteRequest flower = new DeleteRequest("flower", "4"); DeleteResponse delete = restHighLevelClient.delete(flower, RequestOptions.DEFAULT); System.out.println(delete); System.out.println(delete.status()); } /** * 測試批量 增刪改查都可以 只需要更換不同的Request就可以了 * @throws IOException */ @Test void batchDocument() throws IOException { BulkRequest flower = new BulkRequest(); for (int i = 0; i < 100; i++) { int y = i + 4; User user = new User("測試" + y, y); flower.add( new IndexRequest("flower") .id(y+"") .source(JSON.toJSONString(user),XContentType.JSON) ); } BulkResponse bulk = restHighLevelClient.bulk(flower, RequestOptions.DEFAULT); System.out.println(bulk); System.out.println(bulk.status()); // 返回false 代表沒有失敗 System.out.println(bulk.hasFailures()); } @Test void query() throws IOException { SearchRequest flower = new SearchRequest("flower"); // 包含的欄位 String[] includes = new String[]{"name"}; // 排除的欄位 String[] excludes = new String[]{"age"}; // 構造搜尋條件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // match匹配 欄位 MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "彼岸"); // 加入條件 sourceBuilder.query(matchQueryBuilder); sourceBuilder.fetchSource(new FetchSourceContext(true,includes,excludes)); // 分頁 sourceBuilder.from(0); sourceBuilder.size(2); flower.source(sourceBuilder); SearchResponse search = restHighLevelClient.search(flower, RequestOptions.DEFAULT); System.out.println(search); System.out.println(search.getHits()); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsString()); } } }
作者:彼岸舞
時間:2020\09\11
內容關於:ElasticSearch
本文來源於網路,只做技術分享,一概不負任何責任