Why can't I connect to ElasticSearch through Java API? Why can't I connect to ElasticSearch through Java API? elasticsearch elasticsearch

Why can't I connect to ElasticSearch through Java API?


The TransportClient default port is 9300. You have to use it instead of 9200 in your Java code. This is probably why the connection fails.


import java.net.InetAddress;import java.net.UnknownHostException;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;public class ElasticsearchTest {      public static void main(String[] argv) throws UnknownHostException{         /* //Set new cluester        Settings settings = Settings.builder()                .put("cluster.name", "newCluster")                .put("node.name","newNode").build();*/        //create cliet !!!Make sure keep settings empty if your cluster is the         //same as the one you defined in your elasticsearch.yml file        //Plus, port here(9300)must be different from your http port(9200)        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));        //get data        GetResponse response = client.prepareGet("twitter", "tweet", "1").execute().actionGet();       //output        System.out.println(response.getSourceAsString());        client.close();         }     }