1. 程式人生 > 其它 >HM-SpringCloud微服務系列5.4【RestClient操作索引庫|RestAPI】

HM-SpringCloud微服務系列5.4【RestClient操作索引庫|RestAPI】

1 什麼是RestClient

  1. ES官方提供了各種不同語言的客戶端,用來操作ES。這些客戶端的本質就是組裝DSL語句,通過http請求傳送給ES。官方文件地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html
  2. 其中的Java Rest Client又包括兩種:
    • Java Low Level Rest Client
    • Java High Level Rest Client(所學)



2 案例

2.1 匯入Demo專案

2.1.1 匯入資料庫和專案


CREATETABLE`tb_hotel`(
`id`bigint(20)NOTNULLCOMMENT'酒店id',
`name`varchar(255)NOTNULLCOMMENT'酒店名稱;例:7天酒店',
`address`varchar(255)NOTNULLCOMMENT'酒店地址;例:航頭路',
`price`int(10)NOTNULLCOMMENT'酒店價格;例:329',
`score`int(2)NOTNULLCOMMENT'酒店評分;例:45,就是4.5分',
`brand`varchar(32)NOTNULLCOMMENT'酒店品牌;例:如家',
`city`varchar(32)NOTNULLCOMMENT'所在城市;例:上海',
`star_name`varchar(16)DEFAULTNULLCOMMENT'酒店星級,從低到高分別是:1星到5星,1鑽到5鑽',
`business`varchar(255)DEFAULTNULLCOMMENT'商圈;例:虹橋',
`latitude`varchar(32)NOTNULLCOMMENT'緯度;例:31.2497',
`longitude`varchar(32)NOTNULLCOMMENT'經度;例:120.3925',
`pic`varchar(255)DEFAULTNULLCOMMENT'酒店圖片;例:/img/1.jpg',
PRIMARYKEY(`id`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8mb4;



2.2.2 mapping對映分析(分析資料結構)

  1. 建立索引庫,最關鍵的是mapping對映,而mapping對映要考慮的資訊包括:
    • 欄位名
    • 欄位資料型別
    • 是否參與搜尋
    • 是否需要分詞
    • 如果分詞,分詞器是什麼?
  2. 其中:
    • 欄位名、欄位資料型別,可以參考資料表結構的名稱和型別
    • 是否參與搜尋要分析業務來判斷,例如圖片地址,就無需參與搜尋
    • 是否分詞呢要看內容,內容如果是一個整體就無需分詞,反之則要分詞
    • 分詞器,我們可以統一使用ik_max_word
  3. 來看下酒店資料的索引庫結構:
PUT /hotel
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword",
        "copy_to": "all"
      },
      "starName":{
        "type": "keyword"
      },
      "business":{
        "type": "keyword"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
  1. 幾個特殊欄位說明:
    • location:地理座標,裡面包含精度、緯度
    • all:一個組合欄位,其目的是將多欄位的值 利用copy_to合併,提供給使用者搜尋
  2. 地理座標說明:
  3. copy_to說明:

2.2.3 初始化RestClient

在elasticsearch提供的API中,與elasticsearch一切互動都封裝在一個名為RestHighLevelClient的類中,必須先完成這個物件的初始化,建立與elasticsearch的連線。分為三步:

  1. 引入es的RestHighLevelClient依賴:
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
  1. 因為SpringBoot預設的ES版本是7.6.2,所以我們需要覆蓋預設的ES版本:
<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
  1. 初始化RestHighLevelClient:

初始化的程式碼如下:

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
        HttpHost.create("http://10.193.193.141:9200")
));

這裡為了單元測試方便,我們建立一個測試類HotelIndexTest,然後將初始化的程式碼編寫在@BeforeEach方法中:

package cn.itcast.hotel;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

public class HotelIndexTest {
    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.150.101:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

2.2 建立索引庫

2.3 刪除索引庫

2.4 判斷索引庫是否存在

2.5 小結

  1. JavaRestClient操作elasticsearch的流程基本類似。核心是client.indices()方法來獲取索引庫的操作物件。
  2. 索引庫操作的基本步驟:
    • 初始化RestHighLevelClient
    • 建立XxxIndexRequest。XXX是Create、Get、Delete
    • 準備DSL( Create時需要,其它是無參)
    • 傳送請求。呼叫RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete