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

**URL:** <https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 25, 2022, 2:51pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622 "2022-05-25T14:51:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [May 25, 2022, 2:51pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/1 "2022-05-25T14:51:54Z")

</div>

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:

```auto
            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!

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [May 27, 2022, 12:46pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/2 "2022-05-27T12:46:00Z")

</div>

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

```auto
            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

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [May 31, 2022, 12:13pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/3 "2022-05-31T12:13:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [May 31, 2022, 1:05pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/4 "2022-05-31T13:05:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [May 31, 2022, 4:18pm UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/5 "2022-05-31T16:18:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vmassol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vmassol/32/17398_2.png) [@vmassol](https://discuss.elastic.co/u/vmassol)\
**Post date:** [June 1, 2022, 11:42am UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/6 "2022-06-01T11:42:25Z")

</div>

Thanks @swallez .

I'm now trying to configure security for anonymous users: [Configuring and debugging anonmyous access to Elasticsearch](https://discuss.elastic.co/t/configuring-and-debugging-anonmyous-access-to-elasticsearch/306116) Doesn't seem that easy to know what's wrong! 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 29, 2022, 11:42am UTC](https://discuss.elastic.co/t/use-the-java-api-client-to-connect-to-an-es-instance-at-a-given-url/305622/7 "2022-06-29T11:42:30Z")

</div>

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