NoNodeException Error when trying to create a transport client for Elasticsearch

I am running Elasticsearch 2.1.0 on localhost:9200. What I need to do is read from an index using Java for ES, without having to create a node, because I care about the speed of my application. The following is my code:

try (Client client = TransportClient.builder().build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9200))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9200))) {
            QueryBuilder qb = matchQuery(
                    ...
            );  
            CountResponse response;
            response = client.prepareCount(indexName)
                    .setTypes(spammerType).setQuery(qb)
                    .execute()
                    .actionGet();
        }

However, I am getting the following error:

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9200}]]

But I'm trying to avoid creating a node because as I mentioned before, I need my application to be as fast as possible in reading from the index. According to ES:

There are uses-cases for both clients:

The transport client is ideal if you want to decouple your application
from the cluster. For example, if your application quickly creates and
destroys connections to the cluster, a transport client is much
"lighter" than a node client, since it is not part of a cluster.

Similarly, if you need to create thousands of connections, you don’t
want to have thousands of node clients join the cluster. The TC will
be a better choice.

On the flipside, if you need only a few long-lived, persistent
connection objects to the cluster, a node client can be a bit more
efficient since it knows the cluster layout. But it ties your
application into the cluster, so it may pose problems from a firewall
perspective.

How can I fix the error?? Thanks.

Use port 9300 instead.

Can I perform a search query using the transport client? Because when I tried port 9300 without the search query, it finally returned no errors (thank you). However, when I added my search query and wanted the result to be returned, I got the NoNodeExceptionerror again.

Of course you can. Look at the Java guide: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html

Okay, but the following is my search and yet I am receiving the NoNode error:

try (Client client = TransportClient.builder().build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9300))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9300))) {
    QueryBuilder qb = matchQuery(
            "user_name",
            Username
    );  
    
    CountResponse response = client.prepareCount(indexName)
            .setTypes(spammerType).setQuery(qb)
            .execute()
            .actionGet();
    return response;
}

And the following is the error I am receiving.

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{**.**.**.**}{***/**.**.**.**:9300}]]

When I tried the same query for the node client, it worked for me with no errors. Why is it now showing me that error?

Thanks.

Hard to say. First guess: cluster name?
Second guess: network.host not properly set in your node?

I've removed the comment of the cluster.name in my elasticsearch.yml configuration file and set it to "elasticsearch". Then, I added the following to my application:

Settings.Builder st = Settings.builder()
                .put("cluster.name", "elasticsearch");

try (Client client = TransportClient.builder().settings(st).build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(), 9300)))

But I am still receiving the same error. How can I fix the network.host if it was indeed the problem?? I'm running ES on localhost:9200 and the configurations of that in the elasticsearch.yml are all commented out.

This? https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html

Define it to your ip address for example.

You use InetAddress.getLocalHost(), this may return an IPv6 address. Is Elasticsearch configured for IPv6?

In the message Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{**.**.**.**}{***/**.**.**.**:9300}]] you can see the correct IP and Elasticsearch must be configured to this IP.

1 Like

OKay, I got it to work. I defined the network.host in the ES configs to localhost and only then it worked. thanks.