Getting ML job with Java API on Elastic Cloud

Hi there, I noticed that running a simple GET _xpack/ml/anomaly_detectors/job-that-does-not-exist through the Java low-level REST client against a trial Elastic Cloud deployment returns a 400 BAD_REQUEST instead of a 404. Have tested this against my local ES cluster and also on the cloud trial through the Dev Tools and the API console and it correctly returns a 404 with "resource_not_found_exception". I suspect none of the ML API calls are working but can do further testing to verify.

Is this some restriction pertaining to the Java API or maybe because it is a trial deployment? ES is 6.3.2 on both clusters.

Thanks.

Hi,

I have not been able to recreate this with the low-level client or using curl. You should use the URL of the Elasticsearch API endpoint, is it possible you are accessing the cluster via a different URL? I believe cloud use a proxy and I wonder if you are hitting this rather than directly to the cluster

Hello, I have tried with curl and it does work, this returns a 200 as expected:

curl -i -u user:pass -X GET https://8a0570efd6b54daf955f87b8e17c8cf7.eu-central-1.aws.cloud.es.io:9243/_xpack/ml/anomaly_detectors

but using the same endpoint and credentials through Java I still get the 400 bad request error. Here's the code:

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));
        
restClientBuilder = RestClient.builder(HttpHost.create("https://8a0570efd6b54daf955f87b8e17c8cf7.eu-central-1.aws.cloud.es.io:9243")).setHttpClientConfigCallback(new HttpClientConfigCallback() {
    @Override
    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        	return httpClientBuilder;
        }
    });
        
client = new RestHighLevelClient(restClientBuilder);
        
try {
    Response response = client.getLowLevelClient().performRequest("GET", "_xpack/ml/anomaly_detectors");
    System.out.println(response);
    System.out.println(response.getEntity());
} catch (Exception e) {
    e.printStackTrace();
}

OK I got it, I was actually not able to do any requests using the low-level client, I was missing a forward slash at the beginning of the URI:

client.getLowLevelClient().performRequest("GET", "/_xpack/ml/anomaly_detectors");

It used to work in 6.2.2 without the slash so must be a change in the 6.3.2 library. Thanks for the help!

1 Like

Great news, thanks for the update