client.prepareIndex

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", "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?

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

Check that you imported the right JARs?

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.

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

Settings settings = Settings.settingsBuilder().put("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)

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.

clusterName is set to
final String clusterName = "elasticsearch_dev";
which is the same cluster name in my elasticsearch.yml file
cluster.name: elasticsearch_dev

Got it.

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

It's wrong.

That would explain why it works only when I change the 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?

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));