Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to remote server using Elasticsearch Node Client Java

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());
like image 327
Arnav Sengupta Avatar asked Dec 01 '14 18:12

Arnav Sengupta


People also ask

What is Elasticsearch client node?

Any node which is neither a master ( node. master is set to false) nor data ( node. data is set to false) becomes the client node. Being the client node, it behaves as a load balancer that routes the requests and search to the right nodes.

How do I connect to elastic search?

There are two ways to connect to your Elasticsearch cluster: Through the RESTful API or through the Java transport client. Both ways use an endpoint URL that includes a port, such as https://ec47fc4d2c53414e1307e85726d4b9bb.us-east-1.aws.found.io:9243 .

Is Elasticsearch in Java?

Elasticsearch is a NoSQL Database, which is developed in Java programming language. It is a real-time, distributed, and analysis engine that is designed for storing logs. It is a highly scalable document storage engine. Similar to the MongoDB, it stores the data in document format.


1 Answers

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));
like image 200
evanwong Avatar answered Sep 23 '22 07:09

evanwong