8

I am trying to connect to my server using Elasticsearch Java NodeBuilder Client. However I do not see any option to specify my server address and port (like we can do in Transport Client, using addNewTransportAddress("serveraddress", port)). How do I make Node Client connect to my server? Code is below, where do I mention the server address to connect to?

//On Startup
Node node = nodeBuilder()
        .clusterName("elasticsearch")
        .data(false) //No shards allocated; or can set client to true
        .client(true) //No shards allocated; or can set data to false
        .node();

//Node Client
Client client = node.client();

//Get API       
GetResponse response = client.prepareGet("indexname", "type", "id")
        .execute()
        .actionGet();

System.out.println("----------------Index Output Begin----------------");
System.out.println("Index Name: " + response.getIndex());
System.out.println("Type: " + response.getType());
System.out.println("Document ID: " + response.getId());
System.out.println("Document Version: " + response.getVersion());
System.out.println("Source: " + response.getSourceAsString());
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
Arnav Sengupta
  • 423
  • 1
  • 7
  • 19

2 Answers2

8

The node client is base on multicast. The network between your clients and the nodes have to be in the network that has multicast enable. And then the client will "discover" the nodes base on the cluster name.

If you need to connect to the remote servers (by specifying the ip addresses), you have to use the transport client.

Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "myClusterName").build();
Client client = new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));
evanwong
  • 5,054
  • 3
  • 31
  • 44
0

I got "ImmutableSettings cannot be resolved" when I used ImmutableSettings.

My code is:

Node node =nodeBuilder()
           .settings(ImmutableSettings.settingsBuilder().put("path.home",   "/home/amit/elasticsearch-2.1.0/bin"))
            .client(true)
            .node();
        Client client = node.client(); 
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
  • I had same issue. It seems ImmutableSettings is replaced by **Settings** in 2.2.0 version – Seid.M Feb 21 '16 at 15:12
  • Node node= NodeBuilder.nodeBuilder().settings(Settings.builder() .put("path.home", "/media/rajnish/7F58-E3202/elasticsearch-2.1.0")) .client(false) .local(true) .node(); //Create Client Object. Client client = node.client(); – Rajnikant Baflipara Jul 20 '17 at 06:45