高級客戶端的主要目的是公開API特定的方法,這些方法接受請求對象作為參數並返回響應對象,以便請求編組和響應解編組由客戶端本身處理。
每個API可以同步或異步調用。同步方法返回一個響應對象,而名稱以async后綴結尾的異步方法則需要一個偵聽器參數,一旦收到響應或錯誤,該參數將被通知(在低級客戶端管理的線程池上)。
Java高級REST客戶端取決於Elasticsearch核心項目。它接受與相同的請求參數,TransportClient並返回相同的響應對象。
添加依賴: <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency> 由於我使用的springboot版本對應的是6.8.5,所以maven中還要添加 <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<elasticsearch.version>7.6.2</elasticsearch.version>
</properties>
初始化:
@Configuration
public class ESClient {
@Bean
public RestHighLevelClient client(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
// new HttpHost("321.321.321.321", 9201, "http"),
new HttpHost("123.123.123.123", 9200, "http")));
return client;
}
}