Use the java API client to connect to an ES instance at a given URL

Hi,

I'm migrating XWiki to use the new Java API client with ES 8.2.0 and I can't find a way to tell the API to connect to a URL (https://domainname/path).

If I use RestClient restClient = RestClient.builder(HttpHost.create(pingURL)) then I get a UnknownHostException: domainname/path: nodename nor servname provided, or not known.

I cannot use RestClient.builder(new HttpHost(...)) since that doesn't allow to pass a path.

FWIW, I was previously using the following with ES 1.1:

            HttpClientConfig clientConfig = new HttpClientConfig.Builder("https://domainname/path").multiThreaded(true).build();
            JestClientFactory factory = new XWikiJestClientFactory(this.configuration);
            factory.setHttpClientConfig(clientConfig);
            JestClient client = factory.getObject();

Thanks!

ok I think I have the found the solution with the use of setPathPrefix():

            RestClientBuilder restClientBuilder = RestClient.builder(getPingHost(pingURL));
            String path = getPingPath(pingURL);
            if (StringUtils.isNotEmpty(path)) {
                restClientBuilder = restClientBuilder.setPathPrefix(path);
            }
            RestClient restClient = restClientBuilder
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                    .useSystemProperties()
                    .setUserAgent(this.configuration.getUserAgent()))
                .build();

Thx

1 Like

hmm, actually it's not working for some reason. I get:

ResponseException: method [PUT], host [http://xxx:8080], URI [/activeinstalls2/_ingest/pipeline/set-timestamp], status line [HTTP/1.1 400 Bad Request

when the path prefix is set to /activeinstalls2.

It works fine when there's no path prefix (and I configure ES to be available at xxx:8080 directly).

Any idea? Thx

It's actually working, there was a problem with an nginx server in front...

1 Like

I'm glad you found the answer! When it comes to connecting to the Elasticsearch cluster, there is no difference between the old High Level Rest Client and the Java API client since they both use the low level RestClient. And you can actually use that low level client directly to more easily debug connection/network related issues.

Thanks @swallez .

I'm now trying to configure security for anonymous users: Configuring and debugging anonmyous access to Elasticsearch Doesn't seem that easy to know what's wrong! :wink:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.