# Connecting to Elasticsearch with Java

**URL:** https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489
**Category:** Elasticsearch
**Created:** [June 11, 2015, 3:17pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489 "2015-06-11T15:17:49Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)
#### Post date: [June 11, 2015, 3:17pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/1 "2015-06-11T15:17:49Z")

</div>

I’m using the Java API, but I keep getting this error:

> org.elasticsearch.client.transport.NoNodeAvailableException: None of the  
> configured nodes are available:

Even if I run my code on the same machine as Elasticsearch I get  
this error.

Any ideas why this is? I was able to make contact once, then it  
stopped.

Here is my TransportClient implementation. I decided to place  
the code on the server to be sure, still no good.

I’ve tried several implementations with the put method. With and  
without cluster.name or network.host:

```
public class ClientTransport {
    
public Client client;

@SuppressWarnings("resource")
public ClientTransport(){
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "store_cluster").put("network.host","127.0.0.1").build();
             
this.client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("http://127.0.0.1", 9300));
    
           }
 
    }

```

Here is usage of the ClientTransport class:

```
public static void main(String[]
args){
Map<String, Object> json = new
HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");

IndexResponse response = null;
ClientTransport c = new
ClientTransport();

             
for(int x = 0;x<10;x++){

response = c.client.prepareIndex("twitter2", "tweet").setSource(json).execute().actionGet();
                  
System.out.println("Index: "+response.getIndex());              
System.out.println("Type: "+response.getType());                
System.out.println("Id: "+response.getId());                 
System.out.println("Is Created: "+response.isCreated());        
}

          
//do I need to fluch data?      
//client.admin().indices().flush(new
FlushRequest('location').refresh(true)).actionGet()
c.client.close();
}

```

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [June 11, 2015, 3:43pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/2 "2015-06-11T15:43:36Z")

</div>

Make sure the Elasticsearch version in your client application is the same version as the Elasticsearch instance you're running.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 11, 2015, 3:56pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/3 "2015-06-11T15:56:12Z")

</div>

Change this

> [@mc1392](#):
>
> this.client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("[http://127.0.0.1](http://127.0.0.1)", 9300));

```auto
this.client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));

```

---

<div class="post-metadata">

### Author: ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)
#### Post date: [June 11, 2015, 7:23pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/4 "2015-06-11T19:23:24Z")

</div>

Same error:

> org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available:

---

<div class="post-metadata">

### Author: ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)
#### Post date: [June 11, 2015, 7:28pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/5 "2015-06-11T19:28:37Z")

</div>

using 1.4.4 on both the server and my code.  
Here are my libraries on the server and my code.

 ![](https://us1.discourse-cdn.com/elastic/original/1X/5c411e50c3af60ab5114ccaaa850c2450b676a03.gif) ![](https://us1.discourse-cdn.com/elastic/original/1X/0bd37e06b9878265f61e420bd29e47e412a00dab.gif)

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [June 11, 2015, 10:41pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/6 "2015-06-11T22:41:40Z")

</div>

Your settings don't look quite right remove the "network.host" property:

```
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
```

---

<div class="post-metadata">

### Author: ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)
#### Post date: [June 12, 2015, 5:00pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/7 "2015-06-12T17:00:22Z")

</div>

I removed network.host.  
Still no good.

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [June 12, 2015, 7:40pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/8 "2015-06-12T19:40:04Z")

</div>

Is that the correct cluster name?

---

<div class="post-metadata">

### Author: ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)
#### Post date: [June 15, 2015, 2:57pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/9 "2015-06-15T14:57:43Z")

</div>

Yes, names are all correctly matched.

---

<div class="post-metadata">

### Author: ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)
#### Post date: [June 15, 2015, 3:41pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/10 "2015-06-15T15:41:09Z")

</div>

Here is a simplified version of your code that works. Copy, compile and run it (you'll need to change the cluster name). Hopefully from this example your can figure out what is wrong with your original code.

```
public static void main(String args[]) {
	Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
	Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
	
	Map<String, Object> json = new HashMap<String, Object>();
	json.put("user","kimchy");
	json.put("postDate",new Date());
	json.put("message","trying out Elasticsearch");

	IndexResponse response = null;
				 
	for(int x = 0;x<10;x++){
	  response = client.prepareIndex("test", "tweet").setSource(json).execute().actionGet();
						
	  System.out.println("Index: "+response.getIndex());              
	  System.out.println("Type: "+response.getType());                
	  System.out.println("Id: "+response.getId());                 
	  System.out.println("Is Created: "+response.isCreated());        
	}  
	
	//do I need to fluch data?      
	//client.admin().indices().flush(new
	//FlushRequest("location").refresh(true).actionGet();
	client.close();
}
```

---

<div class="post-metadata">

### Author: ![rmswalsh1](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@rmswalsh1](https://discuss.elastic.co/u/rmswalsh1)
#### Post date: [December 13, 2015, 7:46pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/11 "2015-12-13T19:46:51Z")

</div>

I have downloaded a default ES, ran it on a MAC and then tried to connect with the above code copied and run with no changes and still get this.

NoNodeAvailableException: None of the configured nodes are available.

I am not sure if I am missing something or if the code is out of date vs most recent versions.

I am running ES 2.1.0.

---

<div class="post-metadata">

### Author: ![rmswalsh1](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@rmswalsh1](https://discuss.elastic.co/u/rmswalsh1)
#### Post date: [December 13, 2015, 8:44pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/12 "2015-12-13T20:44:56Z")

</div>

I went back to version 1.7.1 of ES and the same code worked out of the box. No issues. I have read this is a version issue with v 2 of ES. Is that the case, if so is there an issue open about this?

---

<div class="post-metadata">

### Author: ![rmswalsh1](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@rmswalsh1](https://discuss.elastic.co/u/rmswalsh1)
#### Post date: [December 13, 2015, 9:06pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/13 "2015-12-13T21:06:12Z")

</div>

Ok. I have worked this out.

Both the Cleint JAR and the ES version need to be the same.

Spring-Data-Elasticsearch (1.3.1) only works on version 1.5.2 of ES Server and bundles with it theES 1.5.2 Java API.

So thats why the error is happening.

Currently Spring are looking into this but dont have a timeline on support.

[https://jira.spring.io/browse/DATAES-211](https://jira.spring.io/browse/DATAES-211)

Hope this helps anyone looking for the details on this.

So its a case of coding directly with the ES API or sticking to ES 1.5.2 for now.

---

<div class="post-metadata">

### Author: ![tirupathi](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tirupathi](https://discuss.elastic.co/u/tirupathi)
#### Post date: [February 24, 2016, 7:38pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/14 "2016-02-24T19:38:07Z")

</div>

> [@rmswalsh1](#):
>
> sticking

I have the similar issue, I am running both client and server using ES 2.1.2 version, this is interesting when I run Java client program on ES server it works fine, if move client to another server I get this exception.  
Any solution

---

<div class="post-metadata">

### Author: ![sandeep\_vuppula](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sandeep\_vuppula](https://discuss.elastic.co/u/sandeep_vuppula)
#### Post date: [July 11, 2016, 6:34pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/15 "2016-07-11T18:34:25Z")

</div>

I have the same version of ES in my local and server. But still i get the no node exception.

---

<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: [July 5, 2017, 10:36pm UTC](https://discuss.elastic.co/t/connecting-to-elasticsearch-with-java/2489/16 "2017-07-05T22:36:29Z")

</div>


