Localhost:9200: nodename nor servname provided, or not known

I am running elastic search version 6.4 locally and my dependency for the spring boot application is implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.4.0' . I am getting java.io.IOException: localhost:9200: nodename nor servname provided, or not known
at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:948)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:227)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:272)
.I tried localhost only also but still getting same problem. But when I am using same code but server elastic search I am getting having problem. Previously I am using elastic search transport. at that time there was no problem while connection but i upgrade to rest-high-level client ..I am facing problem ... Can some one help me.....

Please share your code and format it using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

image

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

This is my elastic configuration class:

@Bean(destroyMethod = "close")
    public RestHighLevelClient client() {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(elasticHost)));
        return client;

    }

and in my service class I am trying to check index is already exist or not using

public boolean checkIfIndexExists(String indexName) throws IOException {
        Response response = client.getLowLevelClient().performRequest("HEAD", "/" + indexName);
        int statusCode = response.getStatusLine().getStatusCode();
        return (statusCode != 404);
    }

Where same code is working on the server but not in the local.. where in local I am using elastic 6.4.2 and maven also the same version.

Can you print debug the content of elasticHost?

Hi Dadoonet,
My elasticHost is localhost:9200.. which I am reading from YML

Can you replace it with an IP address like 127.0.0.1?

Yes I tried but still having same problem. previously when i use elastic transport (with transport code) it was working perfectly on the localhost:9200 port. But after changing with the above configuration for highlevel client I am facing problem on my local machine.

Could you try this?

I mean manually set the IP like:

new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));

Thank you for the response dodoonet. Still having the same problem. .. when I use the hardcoded value(HttpHost.create("http://127.0.0.1:9200")) in configuration file getting this image error.

But when I use restclient in the inline (as you suggested) I am able to connect to the localhost. that means it is successfully why ? But for the server connection I dont have to do that..

client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
        Response response = client.getLowLevelClient().performRequest("HEAD", "/" + indexName);
        int statusCode = response.getStatusLine().getStatusCode();

So when you read from a config file it does not work right?

Did you try using below while creating RestClient:

new HttpHost("localhost", 9200, "http")

instead of

HttpHost.create("http://127.0.0.1:9200")

This works for us in local and on server and everywhere.

Actually No @dadoonet , If that happens it should not get the value in the above screenshot. You see in the above image the value is coming from the config file.

But you said you are successful in one case. Which one is it?

I said When I create a bean and @autowired that bean is not working for localhost/9200. But the same code with server elastic(AWS) search is working. And for the localhost if I write the code inside the same method like this

client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));

it is working. I am confuse why the localhost is not working while autowire the configuration class.

my configuration class is

@Configuration
public class ElasticsearchConfig {

@Bean(destroyMethod = "close")
public RestHighLevelClient client() {

    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("http://127.0.0.1:9200")));
    return client;

}

}

Thank you This way its working for localhost also!!!!

1 Like

So is it solved?

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