# client.prepareIndex

**URL:** <https://discuss.elastic.co/t/client-prepareindex/57642>\
**Category:** Elasticsearch\
**Created:** [August 10, 2016, 2:44am UTC](https://discuss.elastic.co/t/client-prepareindex/57642 "2016-08-10T02:44:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 10, 2016, 2:44am UTC](https://discuss.elastic.co/t/client-prepareindex/57642/1 "2016-08-10T02:44:44Z")

</div>

I am using an eclipse IDE and trying to write a small Java program to insert documents to the index on localhost. I have set up the client as follows:

import org.elasticsearch.action.index.IndexResponse;  
import org.elasticsearch.client.transport.TransportClient;  
import org.elasticsearch.common.settings.Settings;  
import org.elasticsearch.common.transport.InetSocketTransportAddress;

Settings settings = Settings.settingsBuilder().put("[cluster.name](http://cluster.name)", "elasticsearch\_dev").build();

Client client = (Client) TransportClient.builder().settings(settings).build();

client = (Client) TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

Then when I try to use client.prepareIndex as follows:  
String documentId="1";  
IndexResponse response = client.prepareIndex("libraryf","books",documentId)....

The Eclipse IDE shows the following error message:  
the method prepareIndex(String, String, String) is undefined for the type Client.

I am using Elastic Search 2.3.5.

Any ideas on why I am getting this message when using client.prepareIndex?

---

<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:** [August 10, 2016, 7:05am UTC](https://discuss.elastic.co/t/client-prepareindex/57642/2 "2016-08-10T07:05:34Z")

</div>

You probably have something wrong in your eclipse configuration as it seems that your code is correct: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html)

Check that you imported the right JARs?

---

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 10, 2016, 12:15pm UTC](https://discuss.elastic.co/t/client-prepareindex/57642/3 "2016-08-10T12:15:17Z")

</div>

I had the correct jar file, but when I used CTRL-SHIFT-O I selected the wrong import package for client. Once I select the correct one, the issue was resolved. Thanks.

---

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 10, 2016, 8:48pm UTC](https://discuss.elastic.co/t/client-prepareindex/57642/4 "2016-08-10T20:48:37Z")

</div>

Ok got past that error. Now have a new one. Have the following code

Settings settings = Settings.settingsBuilder().put("[cluster.name](http://cluster.name)", clusterName).build();  
Client client = (Client) TransportClient.builder().settings(settings).build();  
try {   
client = (Client) TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress (InetAddress.getByName("localhost"), 9300));

} catch (UnknownHostException e){  
System.out.println("Create Client failed: " + e);  
}

And get the error below. : It thinks 127.0.0.1:9300 is not part of the cluster.

16:34:44.735 [main] WARN org.elasticsearch.client.transport - [Grappler] node {#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300} not part of the cluster Cluster [elasticsearch], ignoring...  
Starting Query 1470861285037  
Query Completed and ran for 622 ms  
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300}]]  
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)

---

<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:** [August 10, 2016, 9:53pm UTC](https://discuss.elastic.co/t/client-prepareindex/57642/5 "2016-08-10T21:53:19Z")

</div>

Please format your code using `</>` icon. It will make your post more readable.

You probably changed the cluster name on your node but not in your client. So it tries `elasticsearch` here.

---

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 10, 2016, 10:19pm UTC](https://discuss.elastic.co/t/client-prepareindex/57642/6 "2016-08-10T22:19:27Z")

</div>

clusterName is set to  
final String clusterName = "elasticsearch\_dev";  
which is the same cluster name in my elasticsearch.yml file  
[cluster.name](http://cluster.name): elasticsearch\_dev

---

<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:** [August 11, 2016, 8:22am UTC](https://discuss.elastic.co/t/client-prepareindex/57642/7 "2016-08-11T08:22:46Z")

</div>

Got it.

You built 2 instances of transport client.  
First with a cluster name  
Second with transport addresses.

It's wrong.

---

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 11, 2016, 11:56am UTC](https://discuss.elastic.co/t/client-prepareindex/57642/8 "2016-08-11T11:56:35Z")

</div>

That would explain why it works only when I change the [custer.name](http://custer.name) to elasticsearch in the yml file.  
Doesn't the client need both the cluster name and the transport address? If so how does one set this up in one statement. Can you provide me with an example or a link to an example on how to do this in one step?

---

<div class="post-metadata">

**Author:** ![talluann](https://avatars.discourse-cdn.com/v4/letter/t/a183cd/32.png) [@talluann](https://discuss.elastic.co/u/talluann)\
**Post date:** [August 30, 2016, 2:48am UTC](https://discuss.elastic.co/t/client-prepareindex/57642/9 "2016-08-30T02:48:09Z")

</div>

The correct way to set this up is as follows, at least this is what worked for me.

```
Settings settings = Settings.settingsBuilder().put("cluster.name", "yourclustername").build();
TransportClient transportClient = TransportClient.builder().settings(settings).build();
Client client = (Client) transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
```

---

<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:24pm UTC](https://discuss.elastic.co/t/client-prepareindex/57642/10 "2017-07-05T22:24:18Z")

</div>


