八、HighLevelAPI 8.1、RestAPI介紹&項目導入 8.1.1、RestAPI介紹 ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質就是組裝DSL語句,通過http請求發送給ES 官方文檔地址 https://www.elastic.co/guide/en/elas ...
八、HighLevelAPI
8.1、RestAPI介紹&項目導入
8.1.1、RestAPI介紹
- ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質就是組裝DSL語句,通過http請求發送給ES
- 官方文檔地址
- 其中的Java Rest Client又包括兩種
- Java Low Level Rest Client
- Java High Level Rest Client
本次學習的是HighLevel版本
8.1.2、項目導入
有需要的可以直接聯繫本人
①、資料庫數據導入
- 導入自定義的數據即可;
②、創建初始工程
-
初始工程創建成功後,目錄結構如下所示
- 其中
HotelDoc
後續會說到
- 其中
-
配置application.yml
-
在spring層級下添加es的服務端路徑配置
-
elasticsearch: rest: uris: - http://192.168.222.135:9200
-
-
導入相關依賴
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.coolman.hotel</groupId> <artifactId>hotel-demo</artifactId> <name>hotel-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--es的RestAPI依賴--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
-
-
編寫測試類,驗證是否可以正常連接
-
package com.coolman.hotel.test; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.client.RestHighLevelClient; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest @Slf4j public class TestConnectES { @Autowired private RestHighLevelClient restHighLevelClient; @Test public void testConnect() { log.info(restHighLevelClient + ""); } }
-
8.2、創建索引
-
代碼如下所示
-
/** * 創建索引測試 */ @Test public void testCreateIndex() throws IOException { // 1. 獲取索引操作對象 IndicesClient indicesClient = restHighLevelClient.indices(); // 2. 創建索引對象 CreateIndexRequest request = new CreateIndexRequest("hotel");// 相當於DSL語句的 PUT hotel 請求,但是還沒執行 // 3. 執行操作 CreateIndexResponse response = indicesClient.create(request, RequestOptions.DEFAULT); // 4. 獲取結果 log.info(response.isAcknowledged() + ""); }
-
-
運行結果如下所示
-
Kibana驗證
8.3、添加映射
-
代碼如下所示
-
因為創建映射需要DSL語句,所以先在kibana編寫如下語句
-
DELETE hotel GET hotel/_mapping # 分析hotel索引庫的映射結構 # index屬性:是否建立索引,預設值true,如果該欄位不用查詢,則設置false # copy_to: 把指定欄位的值拷貝到另一個欄位上 PUT hotel { "mappings": { "properties": { "id": { "type": "keyword" }, "name": { "type": "text", "analyzer": "ik_smart", "copy_to": "all" }, "address": { "type": "text", "analyzer": "ik_smart" }, "price": { "type": "integer" }, "score": { "type": "integer" }, "brand": { "type": "keyword", "copy_to": "all" }, "city": { "type": "keyword" }, "starName": { "type": "keyword" }, "business": { "type": "keyword", "copy_to": "all" }, "location": { "type": "geo_point" }, "pic": { "type": "keyword", "index": false }, "isAD": { "type": "boolean" }, "all": { "type": "text", "analyzer": "ik_smart" } } } }
-
-
創建成功後,再刪除,將mappings中的欄位複製到Java代碼中
-
/** * 添加映射測試 */ @Test public void testAddMapping() throws IOException { // 1. 創建索引操作對象 IndicesClient indicesClient = restHighLevelClient.indices(); // 2. 創建索引 CreateIndexRequest request = new CreateIndexRequest("hotel"); // 3. 定義mapping語句 String mapping = "{\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_smart\",\n" + " \"copy_to\": \"all\"\n" + " },\n" + " \"address\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_smart\"\n" + " },\n" + " \"price\": {\n" + " \"type\": \"integer\"\n" + " },\n" + " \"score\": {\n" + " \"type\": \"integer\"\n" + " },\n" + " \"brand\": {\n" + " \"type\": \"keyword\",\n" + " \"copy_to\": \"all\"\n" + " },\n" + " \"city\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"starName\": {\n" + " \"type\": \"keyword\"\n" + " },\n" + " \"business\": {\n" + " \"type\": \"keyword\",\n" + " \"copy_to\": \"all\"\n" + " },\n" + " \"location\": {\n" + " \"type\": \"geo_point\"\n" + " },\n" + " \"pic\": {\n" + " \"type\": \"keyword\",\n" + " \"index\": false\n" + " },\n" + " \"isAD\": {\n" + " \"type\": \"boolean\"\n" + " },\n" + " \"all\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_smart\"\n" + " }\n" + " }\n" + " }"; request.mapping(mapping, XContentType.JSON); // 設置mappings欄位,並指定其內容為json格式 // 4. 執行操作 CreateIndexResponse response = indicesClient.create(request, RequestOptions.DEFAULT); // 5. 獲取結果 log.info(response.isAcknowledged() + ""); }
-
-
-
運行結果如下所示
-
Kibana驗證
8.4、刪除索引
-
代碼如下所示
-
/** * 刪除索引測試 */ @Test public void testDeleteIndex() throws IOException { // 1. 獲取索引操作對象 IndicesClient indicesClient = restHighLevelClient.indices(); // 2. 獲取索引對象 DeleteIndexRequest request = new DeleteIndexRequest("hotel"); // DELETE hotel // 3. 執行操作 AcknowledgedResponse response = indicesClient.delete(request, RequestOptions.DEFAULT); // 4. 獲取結果 log.info("" + response.isAcknowledged()); }
-
-
比較簡單,自行驗證即可
8.5、添加文檔
-
代碼如下所示
-
/** * 添加文檔測試 */ // Jackson private ObjectMapper objectMapper = new ObjectMapper(); @Test public void testAddDocument() throws IOException { // 先獲取 指定的 hotel數據 Hotel hotel = hotelMapper.selectById(36934); HotelDoc hotelDoc = new HotelDoc(hotel); // 1. 創建請求對象 IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString()); // 2. 填充文檔內容 String json = objectMapper.writeValueAsString(hotelDoc); request.source(json, XContentType.JSON); // 3. 執行請求體對象 IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT); // 4. 獲取結果 log.info(response.getId() + ""); }
-
-
Kibana驗證
8.6、修改、查詢、刪除文檔
8.6.1、修改文檔
修改文檔和添加文檔操作一樣,需要註意的是修改文檔必須是已經存在的ID
-
代碼如下所示
-
/** * 修改文檔 */ @Test public void testUpdateDocument() throws IOException { // 1. 先獲取 指定的 hotel 數據 Hotel hotel = hotelMapper.selectById(36934L); HotelDoc hotelDoc = new HotelDoc(hotel); // 2. 修改數據,如價格 hotelDoc.setPrice(1999999999); // 3. 將 hotelDoc 對象轉換為 JSON格式的數據 String json = objectMapper.writeValueAsString(hotelDoc); // 4. 創建請求對象 IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString()); request.source(json, XContentType.JSON); // 5. 執行操作 IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT); log.info(response.getId()); }
-
-
Kibana驗證
8.6.2、查詢文檔
-
代碼如下所示
-
/** * 查詢文檔 */ @Test public void testSearchDocument() throws IOException { Long id = 36934L; // 1. 創建請求 GetRequest request = new GetRequest("hotel").id(id.toString()); // 2. 執行請求 GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT); // 3. 取出結果 String hotelDocJson = response.getSourceAsString(); HotelDoc hotelDoc = objectMapper.readValue(hotelDocJson, HotelDoc.class); log.info(hotelDoc.toString()); }
-
-
運行結果如下所示
8.6.3、刪除文檔
-
代碼如下所示
-
/** * 刪除文檔 */ @Test public void testDeleteDocument() throws IOException { Long id = 36934L; // 1. 創建請求 DeleteRequest request = new DeleteRequest("hotel").id(id.toString()); // 2. 執行請求 DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT); log.info(response.getId()); }
-
-
Kibana驗證
8.7、批量添加
-
Bulk
批量操作是將文檔的增刪改查一些列操作,通過一次請求全都做完。減少網路傳輸次數 -
應用場景
- ES索引庫數據初始化的時候,可以將資料庫的數據查詢出來通過批量操作導入到索引庫中
-
代碼如下所示
-
/** * 批量添加文檔 */ @Test public void testBatchAddDocument() throws IOException { // 1. 獲取需要導入的數據 List<Hotel> hotelList = hotelMapper.selectList(null); if(CollectionUtils.isNotEmpty(hotelList)) { // 2. 創建批量操作請求對象 BulkRequest bulkRequest = new BulkRequest(); // 4. 獲取文檔映射的對象數據 for (Hotel hotel : hotelList) { HotelDoc hotelDoc = new HotelDoc(hotel); // 5. 創建請求對象 IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());; // 6. 填充內容 String json = objectMapper.writeValueAsString(hotelDoc); request.source(json, XContentType.JSON); // 7. 將數據添加到批量操作對象中 bulkRequest.add(request); } // 8. 一次性執行批量操作 BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); log.info(response.status().toString()); } }
-
-
運行結果如下所示
-
Kibana驗證