0
votes

I am very new to elastic search concepts. I am trying to build a simple application using elastic search.

public class App 
{
public static void main( String[] args )
{
    try {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        ElasticOperations.addDocToIndex(client, ElasticUtility.getJsonMap("Sandesha", "male", "[email protected]", "Hassan"));
        System.out.println("Doc added");
        client.close();
    } catch (UnknownHostException e) {

        e.printStackTrace();
    }

}
}

But, When tried to run this program, I am getting NoNodeAvailableException.

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{w1bSLAN-TFWUHE1PA3qDeQ}{localhost}{127.0.0.1:9300}]] at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363) at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) at com.sandesha.elasticsearch.Elastic.ElasticOperations.addDocToIndex(ElasticOperations.java:11) at com.sandesha.elasticsearch.Elastic.App.main(App.java:27)

Please help me resolve this issue. My ElasticOperations class is,

public class ElasticOperations {

public static void addDocToIndex(TransportClient client, Map<String,Object> jsonMap)
{
    client.prepareIndex("customers", "personal").setSource(jsonMap).execute().actionGet();
}

}

And my ElasticUtility class looks like,

public class ElasticUtility {

public static Map<String, Object> getJsonMap(String name, String gender, String email, String city)
{
    Map<String,Object> jsonMap = new HashMap<String, Object>();
    jsonMap.put("name", name);
    jsonMap.put("gender", gender);
    jsonMap.put("email", email);
    jsonMap.put("city", city);

    return jsonMap;
}
}

Thank you.

1
Can you connect to the elastic from terminal or using sense ? - sourabh1024
I can connect through terminal. It runs on the port 9200 - Naanavanalla

1 Answers

1
votes

I think you have a cluster name mismatch. If your cluster name is not "elasticsearch" that's likely the problem.

Go to http://localhost:9200 in a browser and grab your cluster name. Then, try adding the cluster name property to the TransportClient as follows:

Settings settings = Settings.builder()
                            .put("cluster.name", "elasticsearch_foo").build();
TransportClient client = new PreBuiltTransportClient(settings)
                         .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));