10

I get the following error when trying to connect to Elasticsearch 2 using the Java API for ES 2. This is the code:

Settings settings = Settings.settingsBuilder().put("cluster.name", Receptor.clusterName).build();
TransportClient transportClient = TransportClient.builder().settings(settings).build();
Client c = null;
try {
     c = transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Receptor.es_ip), 9300));
} catch (UnknownHostException e) {
     System.err.println(Util.getTimestampStr() + "UnknownHostException error.");
     e.printStackTrace();
}

CreateIndexRequestBuilder createIndexRequestBuilder = c.admin().indices().prepareCreate(indexName);
createIndexRequestBuilder.addMapping(documentName, json);
createIndexRequestBuilder.execute().actionGet();

I'm able to get my ES node on the transportClient.connectedNodes() but when I try to add a new mapping I get the NoNodeAvailableException exception. This code worked with previous versions of Elasticsearch. Any idea of what's wrong?

NoNodeAvailableException[None of the configured nodes are available:
[]]     at
org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:280)
    at
org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:197)
    at
org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
    at
org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:272)
    at
org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:347)
    at
org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1177)
    at
org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:85)
    at
org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
Carlos Vega
  • 1,341
  • 2
  • 13
  • 35
  • 2
    If you configure your node in the cluster you are trying to access like [in this post](http://stackoverflow.com/questions/33412549/how-to-bind-elasticsearch-2-0-on-both-loopback-and-non-loopback-interfaces/33438638#33438638) do you see any improvement? Also, I'd be curious to know what IP are you using to connect to the nodes, relative to what IPs the ES nodes report to be bound on at the startup (something like `[INFO ][transport ] [main_node_2_0] publ ish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}, {[::1]:9300}` in logs at the startup). – Andrei Stefan Nov 17 '15 at 12:26
  • Have you played with your configuration in `elasticsearch.yml` file? Is there anything for `minimum_master_nodes`? – Evaldas Buinauskas Nov 18 '15 at 17:31
  • you are trying to communicate the external elasticsearch client which is running in the host[`Receptor.es_ip`] with the port `9300` please ensure that the elasticsearch is running or not – Arun Prakash Nov 19 '15 at 13:31
  • I'm running the Java program on the same host as the Elasticseearch node. I'm running the same Java program I did with previous versions of ES (1.7.2) without problems. I have a python script which works perfectly, but my Java program doesn't. ES is running of course. – Carlos Vega Nov 19 '15 at 15:51
  • @AndreiStefan solved my problem, the solution was in the post he linked. – Carlos Vega Nov 23 '15 at 16:20

3 Answers3

8

@AndreiStefan gave the solution to my problem thanks to the post he linked. The solution was as straightforward as:

network.bind_host: 0

Thank you guys.

Community
  • 1
  • 1
Carlos Vega
  • 1,341
  • 2
  • 13
  • 35
  • execuse me where should I add this ? in the elasticsearch.yml ? – YK mar Dec 28 '17 at 10:18
  • Yes, indeed. As the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/modules-network.html) indicates, and [other posts](https://discuss.elastic.co/t/es-2-0-network-config-options/33396/2). Keep in mind that this is for Elasticsearch 2.x, not sure if it would work with later versions. – Carlos Vega Dec 29 '17 at 11:14
1

The problem might be in the settings that you are using.

Instead of creating the Client in these three steps:

TransportClient transportClient = TransportClient.builder().settings(settings).build();
Client c = null;
try {
 c = transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Receptor.es_ip), 9300));
} catch (UnknownHostException e) {
 System.err.println(Util.getTimestampStr() + "UnknownHostException error.");
 e.printStackTrace();
}

Try creating it like this:

Client client = new TransportClient()
            .addTransportAddress(new InetSocketTransportAddress(
                    InetAddress.getByName(Receptor.es_ip),
                    9300));

If you want more data check this old answer to a similar question: Elastic search problems

Community
  • 1
  • 1
crigore
  • 400
  • 5
  • 23
  • What is the problem with my solution, I just provide the cluster name. I been using that code with previous versions of Elasticsearch and its API. – Carlos Vega Nov 23 '15 at 15:30
1

This problem could due to the network problem, if you are using network.host: _site_ in elasticsearch.yml AND sniffed TransportClient connection, i.e. multiple site-local addresses are available within the machine hosting es node.

If you disable the sniff configuration, and the NoNodeAvailableException disappears, then you should double check the network configuration.

Get the nodes stat

GET /_nodes

and check in the result to find transport configuration, i.e.

"transport": {
    "bound_address": [
      "192.168.1.84:9300",
      "172.29.0.1:9300"
    ],
    "publish_address": "172.29.0.1:9300",
    "profiles": {}
  },

If there are multiple site-local addresses, the network.publish_host that sniffed TransportClient will connect to, might be an unexpected address, because

If not specified, this defaults to the “best” address from network.host, sorted by IPv4/IPv6 stack preference, then by reachability.

To solve it, simple specify the network.publish_host

network.publish_host: $DESIRED_IP_ADDRESS

in elasticsearch.yml.

Torrence
  • 448
  • 3
  • 20