RestHighLevelClient作用
时间: 2023-12-22 10:26:46 浏览: 92
ElasticSearch RestHighLevelClient 关于索引库、文档的基础操作
RestHighLevelClient是一个Java高级客户端,用于与Elasticsearch集群进行通信。它对增删改查等操作进行了封装,不需要处理编解码,类似于之前的TransportClient。相比于低级客户端RestClient,RestHighLevelClient的兼容性较差,对客户端和集群版本要求较高。但是它支持异步请求,可以做到负载均衡、故障转移、持久化链接、自动发现集群节点等功能。一般情况下,我们使用RestHighLevelClient来进行Elasticsearch的操作。
以下是一个使用RestHighLevelClient进行查询的Java代码示例:
```java
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("posts");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("title", "Elasticsearch"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsString);
}
client.close();
```
阅读全文