UnknownHostException while connecting to Elasticsearch

Hi,
while doing my first steps with the tutorial

i face much problems even executing the first lines of code.
I get an java.net.UnknownHostException that says "Der angegebene Host ist unbekannt (https://xxx.yyyzzz.com)" which translates to "The specified host is unknown (https://xxx.yyyzzz.com)".
I replaced our companies url with xxx.yyyzzz.

But actually the server is reachable in the browser, and ping is okay, too:

How to solve?

Thanks - Enomine

Welcome!

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with </> icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

Here a first guess is that you might have a firewall or a proxy?

I can't find any edit-Button, but here we go:

public class ConnectingTest {

	public void createClient() throws Exception {
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html

		// Create the low-level client
		RestClient restClient = RestClient.builder(new HttpHost("https://gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243)).build();

		// Create the transport with a Jackson mapper
		ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

		// And create the API client
		ElasticsearchClient client = new ElasticsearchClient(transport);

		SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);

		// SearchResponse<Product> search2 = client.search

		for (Hit<Product> hit : search.hits().hits()) {
			processProduct(hit.source());
		}

	}

	private void processProduct(Product p) {
		System.out.println("ConnectingText -> process Product -> " + p.getName() + " " + p.getPrice() + " " + p.getSku() + " " + p.getClass());
	}

}
/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package usage;

public class Product {
	private String	sku;
	private String	name;
	private double	price;

	public Product() {
	}

	public Product(String sku, String name, double price) {
		this.sku = sku;
		this.name = name;
		this.price = price;
	}

	public String getSku() {
		return sku;
	}

	public void setSku(String sku) {
		this.sku = sku;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return this.price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
}
Exception in thread "main" java.io.IOException: Der angegebene Host ist unbekannt (https://gattinger-second.es.europe-west1.gcp.cloud.es.io)
	at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:923)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:299)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:287)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:146)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1502)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1519)
	at getting_started.ConnectingTest.createClient(ConnectingTest.java:53)
	at main.Main.main(Main.java:9)
Caused by: java.net.UnknownHostException: Der angegebene Host ist unbekannt (https://gattinger-second.es.europe-west1.gcp.cloud.es.io)
	at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
	at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933)
	at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519)
	at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852)
	at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367)
	at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301)
	at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:664)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:635)
	at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
	at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
	at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
	at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:377)
	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:129)
	at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:295)
	... 6 more

Thanks - Enomine

Solution:

RestClient restClient = RestClient.builder(new HttpHost("gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243, "https")).build();

Putting https as a parameter.

Then we come 1 line ahead:

Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [security_exception] missing authentication credentials for REST request [/products/_search?typed_keys=true]
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:281)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1502)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1519)
	at getting_started.ConnectingTest.createClient(ConnectingTest.java:53)
	at main.Main.main(Main.java:9)

Failure in Line 147 instead of 146 :wink:

Thanks - Enomine

I'd recommend using:

RestClient restClient = RestClient.builder(HttpHost.create("https://gattinger-second.es.europe-west1.gcp.cloud.es.io:9243")).build();

That could make your life easier :wink:

The next problem is that you need to provide some credentials.
Like:

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));

RestClient restClient = RestClient.builder(HttpHost.create("https://gattinger-second.es.europe-west1.gcp.cloud.es.io:9243"))
                .setHttpClientConfigCallback(hcb -> hcb.setDefaultCredentialsProvider(credentialsProvider))
                .build();

Something like this.
have a look at Basic authentication | Elasticsearch Java API Client [8.2] | Elastic

Hi,
thanks for your help. Got it to work right now:

public void createClient() throws Exception {
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
		// https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html

		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
		HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigCallback() {
			@Override
			public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
				httpClientBuilder.disableAuthCaching();
				return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
			}
		};

		// Create the low-level client
		RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("gattinger-second.es.europe-west1.gcp.cloud.es.io", 9243, "https"));
		restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
		RestClient restClient = restClientBuilder.build();
		

		// Create the transport with a Jackson mapper
		ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

		// And create the API client
		ElasticsearchClient client = new ElasticsearchClient(transport);

		SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
		System.out.println("SearchResponse erfolgreich. Anzahl: " + search.hits().hits().size());

Thanks - Enomine

Do you actually know why the program is not terminating after this code? In eclipse it stays as running until i hit the stop button.

Thanks - Enomine

Probably because you did not close the client?

Oh! right, thanks!

Enomine

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