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.


(David Kyle) #2


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

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("")).setHttpClientConfigCallback(new HttpClientConfigCallback() {
    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
        	return httpClientBuilder;
client = new RestHighLevelClient(restClientBuilder);
try {
    Response response = client.getLowLevelClient().performRequest("GET", "_xpack/ml/anomaly_detectors");
} catch (Exception e) {


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!

(David Kyle) #5

Great news, thanks for the update

(Mark Walkom) #6