public void ESClientInit() { public static RestHighLevelClient client = null; //不需要用户名和密码的认证 //client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", "9300", "http"))); //需要用户名和密码的认证 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("userName", "password")); RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("127.0.0.1", "9300", "http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); client = new RestHighLevelClient(restClientBuilder); }