Querying/Searching a cluster/Multiple elasticsearch nodes

I am using ES 5.1.1. I had only one elasticsearch node. And I was able to search/view data like below:


2) Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getLocalHost(),9300));
String json = "{"query":{"match_all":{}}}";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
try (XContentParser parser = XContentFactory.xContent(jsonDistinctUrls).createParser(json)) {
QueryParseContext context = new QueryParseContext(searchModule.getQueryParserRegistry(), parser,
new ParseFieldMatcher(true));
searchSourceBuilder.parseXContent(context, searchModule.getAggregatorParsers(),
searchModule.getSuggesters(), searchModule.getSearchExtRegistry());

SearchRequestBuilder searchRequestBuilder = newSearchRequestBuilder(client, SearchAction.INSTANCE);
SearchResponse srDistinctUrls = searchRequestBuilder.setIndices(indexName).setSource(searchSourceBilder).execute().actionGet();

But, now I am using two nodes for elasticsearch.

My Logstash config file output looks like below:
output {
elasticsearch {
action => "index"
hosts => ["",""]
index => "myIndex"

What code changes I need to do now?

You can call again addTransportAddress and add the new address.

That said it's not mandatory. Elasticsearch will load balance the requests within the cluster even if you declare only one node in the transport client.

The nodes you use from the transport client are named coordinating nodes.

Hi David, Thanks for the response.

So, I changed the code to this now adding two addresses.
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hostname1"),9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hostname2"),9300));

But I'm getting resulting from the 1st node only, i.e. hostname1.

What changes do I need to do in the elasticsearch.yml files in both the nodes? Plz suggest.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.