The use of elasticsearch7.x lowLevelClient to obtain the cluster monitoring status of es

Execute elasticsearch api

Direct execution of elastic api cannot be executed directly in RestHighLevelClient, so you need to obtain RestClient from RestHighLevelClient and perform the operation

Request request = new Request(
    "GET",  
    "/");   
Response response = restClient.performRequest(request);

Get the result of response

After execution, you need to get the result, generally you only need to know whether it is executed correctly, but sometimes you still need to get the method body, for example, to get the cluster monitoring status, you need to get the method body

Response response = restClient.performRequest(new Request("GET", "/"));
RequestLine requestLine = response.getRequestLine(); 
HttpHost host = response.getHost(); 
int statusCode = response.getStatusLine().getStatusCode(); 
Header[] headers = response.getHeaders(); 
//获取方法体
String responseBody = EntityUtils.toString(response.getEntity()); 

For example, view the health status of the es cluster

//先注入@Autowired private RestHighLevelClient restHighLevelClient;

RestClient lowLevelClient = restHighLevelClient.getLowLevelClient();

Request scriptRequest = new Request("GET", "/_cluster/health?pretty");

Response response = lowLevelClient.performRequest(scriptRequest);

//获取 es集群健康状态 的json
String responseBody = EntityUtils.toString(response.getEntity());

Execute elasticsearch's api asynchronously

Request request = new Request(
    "GET",  
    "/");   
Cancellable cancellable = restClient.performRequestAsync(request,
    new ResponseListener() {
    
    
        @Override
        public void onSuccess(Response response) {
    
    
            
        }

        @Override
        public void onFailure(Exception exception) {
    
    
            
        }
});

Guess you like

Origin blog.csdn.net/qq_34168515/article/details/112242215