目錄
12.5 RestClient操作索引庫
12.5.1創建庫
12.5.2 刪除索引庫
12.5.3 判斷是否存在
12.6 RestClient操作文檔
12.6.1 新增文檔
12.6.2 查詢文檔
12.6.3 修改文檔
12.6.4 刪除文檔
12.6.5 批量導入文檔
12.5 RestClient操作索引庫
酒店mapping映射
?PUT /hotel{"mappings": {"properties": {"id": {"type": "keyword","copy_to": "all"},"name": {"type": "text","analyzer": "ik_max_word"},"address": {"type": "keyword","index": false},"price": {"type": "integer"},"score": {"type": "integer"},"brand": {"type": "keyword","copy_to": "all"},"city": {"type": "keyword"},"starName": {"type": "keyword"},"business": {"type": "keyword"},"location": {"type": "geo_point"},"pic": {"type": "keyword","index": false},"all": {"type": "text","analyzer": "ik_max_word"}}}}
導入依賴
<!--elasticSearch依賴--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version></dependency>
<properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version></properties>
創建對象
@SpringBootTestclass HotelIndexTest {private RestHighLevelClient client;?// 客戶端初始化@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.142.129:9200")));}?@Testvoid tetsInit(){System.out.println(client);}?// 客戶端銷毀@AfterEachvoid tearDown() throws IOException {this.client.close();}}
client.indices()包含了操作索引庫的所有方法
12.5.1創建庫
@Testvoid testCreateHotelIndex() throws IOException {// 1. 創建Request對象CreateIndexRequest request = new CreateIndexRequest("hotel");// 2. 準備請求的參數request.source(MAPPING_TEMPLATE,XContentType.JSON);// 3. 發送請求 ? client.indices()的返回值包含了索引庫額所有操作client.indices().create(request,RequestOptions.DEFAULT);}
MAPPING_TEMPLATE是自定義的常量,也就是上面創建索引庫的語句
12.5.2 刪除索引庫
@Testvoid testDeleteIndex() throws IOException {// 1. 創建request對象DeleteIndexRequest request = new DeleteIndexRequest("hotel");// 2. 發送請求client.indices().delete(request,RequestOptions.DEFAULT);}
12.5.3 判斷是否存在
@Testvoid testExistIndex() throws IOException {// 1. 創建request對象GetIndexRequest request = new GetIndexRequest("hotel");// 2. 發送請求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 3. 輸出System.out.println(exists ? "索引庫存在" : "索引庫不存在");}
12.6 RestClient操作文檔
12.6.1 新增文檔
@Testvoid testIndexDocument() throws IOException {// 在數據庫查到數據Hotel hotel = iHotelService.getById(61083L);HotelDoc hotelDoc = new HotelDoc(hotel); // 經度 + 緯度 拼接之后的對象 ,即索引庫需要的類型// 1. 創建請求對象IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());// 2. 準備json文檔 把查到的對象轉換成json對象request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);// 3. 發送請求client.index(request,RequestOptions.DEFAULT);}
12.6.2 查詢文檔
@Testvoid testGetDocumentById() throws IOException {// 1. 準備requestGetRequest request = new GetRequest("hotel", "61083");// 2. 發送請求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 3. 從響應中解析對象String json = response.getSourceAsString();// 4. 把json轉成HotelDoc對象HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc);}
12.6.3 修改文檔
第二種更新方式,即局部更新的代碼 :
@Testvoid testUpdateDocumentById() throws IOException {// 1. 準備RequestUpdateRequest request = new UpdateRequest("hotel", "61083");// 2. 準備請求參數request.doc("age", 18,"name","Rose");// 3. 發送請求client.update(request,RequestOptions.DEFAULT);}
12.6.4 刪除文檔
@Testvoid testDeleteDocumentById() throws IOException {// 1. 準備requestDeleteRequest request = new DeleteRequest("hotel","61083");// 2. 發送請求client.delete(request,RequestOptions.DEFAULT);}
12.6.5 批量導入文檔
@Testvoid testBulk() throws IOException {// 批量查詢酒店數據List<Hotel> hotels = iHotelService.list();// 1. 創建Bulk請求BulkRequest request = new BulkRequest();// 2. 準備參數 添加多個新增的requestfor (Hotel hotel : hotels) {// 把hotel轉成hotelDoc對象HotelDoc hotelDoc = new HotelDoc(hotel);request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));}?// 3. 發送請求client.bulk(request,RequestOptions.DEFAULT);}