# Elasticsearch java client for search API not querying all ES Nodes

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 23, 2022, 6:21am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673 "2022-08-23T06:21:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 23, 2022, 6:21am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/1 "2022-08-23T06:21:31Z")

</div>

Hello

Environment detail -

We are having Elasticsearch cluster with version 8.3 with 3 masternode, also we have search API setup with elasticsearch java client to query the data from Elasticsearch using rest client.

Search API java client is setup to send the query request on all three master nodes, e.g. as below  
RestClient restClient = RestClient.builder(  
new HttpHost("localhost", 9200, "http"),  
new HttpHost("localhost", 9201, "http"),  
new HttpHost("localhost", 9202, "http")).build();

Problem Statement -

The problem what we have here is while querying the data from elasticsearch cluster search API java client send the request to only one elasticsearch node instead all three node and that one is the first node from above list ("localhost", 9200, "http") and once this node goes down then only it will send the query request to second node

ANy suggesstion will be helpful

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 23, 2022, 7:59am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/2 "2022-08-23T07:59:26Z")

</div>

You mean that for every request it goes only to the first one?

Or do you expect that each request is sent at the same to time to all nodes?

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 23, 2022, 12:46pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/3 "2022-08-23T12:46:01Z")

</div>

Thank you @dadoonet

Yes we expect for all queries it should go to all nodes so that we have load distribution on all nodes, to server better for bulk queries from java client

Pratiksha

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 23, 2022, 4:35pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/4 "2022-08-23T16:35:39Z")

</div>

That's not the way it works in elasticsearch. One query goes to one node only.

Note that you can create a coordinating node which juste route the query or the data to the right shards.

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 23, 2022, 6:35pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/5 "2022-08-23T18:35:49Z")

</div>

hi @dadoonet

Thank you again for your responce, checking the coordinating node part.

Also want to check about the multiple heavy query request coming from search API e.g. 100k a day, as in present situation all 100k queries are requesting to one node only i.e. the first node from the list.

this situation may impact on that server with heavy request & network load with all those 100k search from search API

We are expecting approx 1million query per day

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 23, 2022, 8:12pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/6 "2022-08-23T20:12:28Z")

</div>

May be it's a bug.

The documentation says:

> By default the client sends requests to every configured node.

@swallez do you know?

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 24, 2022, 2:13pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/7 "2022-08-24T14:13:04Z")

</div>

Hello @dadoonet & @swallez

Checking if any update on this query as we are stuck on the situation loading one node.

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [August 24, 2022, 4:51pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/8 "2022-08-24T16:51:35Z")

</div>

`RestClient` sends requests to all nodes in succession, and there are extensive tests for this.

A common cause for the problem you describe is to have a new `RestClient` created for each request. Every new client starts with the first node, causing the issue. It also slows down the application a lot since `RestClient` is a heavyweight object.

Can you check this? The `RestClient` should be a singleton object that is reused for _all_ requests, and not created anew every time.

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 25, 2022, 8:44am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/9 "2022-08-25T08:44:13Z")

</div>

Hello @swallez,

Thank you for your response we have tried the singleton configuration in our sprinboot application but still we are facing the issue with connection distribution towards ES hosts

Please check the blow code, this is how we are creating singleton restclient object and the same object is being injected across all the handlers and using the same restclient we are creating the transport layer and elasticsearchclient to search the rest templates on elastic side.

Now the problem is closing the connection

Problem #1 -  
If we close the transport layer and shutdown the elasticsearchclient then for the next sequence request we are getting null pointer exception because we have closed two of the layers i.e. transport & elasticsearchclient

Problem #2 -  
Now if we dont close the connection we have the memory issue on client (requestor) server side

* * *

**"Configuration of elastic client as below"**

public class ElasticConfig {  
**@Bean**  
**public RestClient getESRestClientBuilder() {**  
String eshostnames = env.getProperty("eshostnames");  
Integer port = Integer.parseInt(env.getProperty("port"));  
String instance\_id = env.getProperty("instance\_id");  
String keystore = env.getProperty("keystore");  
String key\_pass = env.getProperty("keypass");  
.  
.  
.  
.  
.  
**return restClientBuilder.build();**  
}  
}

* * *

**Handler code as below**

transport = new RestClientTransport(restClient,  
new JacksonJsonpMapper().withAttribute(JsonpMapperFeatures.SERIALIZE\_TYPED\_KEYS, false));  
client = new ElasticsearchClient(transport);  
response = elasticsearchclient.searchTemplate(searchTemplateRequest, Object.class);

if (transport != null) {  
**transport.close();**  
}  
if (elasticsearchclient != null) {  
**elasticsearchclient.shutdown();**  
}

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [August 25, 2022, 10:30am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/10 "2022-08-25T10:30:48Z")

</div>

The transport is just a thin layer on top of `RestClient` : if you close it, it will also close the `RestClient`, which explains the null pointer exception.

Regarding the memory issue (#2), can you make sure that adding `@Bean` on `getESRestClientBuilder()` is enough to make it a singleton? For example by adding a log statement in that method to trace how often it's called.

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 25, 2022, 10:59am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/11 "2022-08-25T10:59:00Z")

</div>

Hi @swallez

Thanks again for your reply

I will make sure to make the class as singleton object , is it possible for you to advice during which phase the restclient should close the connection and where

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [August 25, 2022, 2:36pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/12 "2022-08-25T14:36:39Z")

</div>

If the rest client is really a singleton, then there is no need to close it, except when the application exits.

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [August 26, 2022, 8:53am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/13 "2022-08-26T08:53:49Z")

</div>

Hi @swallez

We have implemented this earlier, when we are not closing the connection we are getting memory issue

We have below memory configuration on web servers

Total memory - 16 GB

Also we want to understand the connection between restclient & http async, how it will handle if it get many static request how singleton will handle as we expect approx 1 million daily traffic hit

Regards  
Pratiksha

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [August 29, 2022, 9:35am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/14 "2022-08-29T09:35:39Z")

</div>

Please elaborate on "memory issue". Have you inspected the JVM heap to understand where the issue comes from?

Regarding async, restclient accepts both blocking and async requests. Actually, under the hood everything is async and a blocking requests justs blocks the calling thread until the internal async request is finished.

---

<div class="post-metadata">

**Author:** ![pratikshatiwari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratikshatiwari/32/116646_2.png) [@pratikshatiwari](https://discuss.elastic.co/u/pratikshatiwari)\
**Post date:** [September 6, 2022, 8:23am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/15 "2022-09-06T08:23:53Z")

</div>

Sorry for responding late, actually after configuring the singleton correctly so far we are not observing memory issue the way it was earlier, we are in monitoring by peforming certain test.

Will come up if identify any further issue on this.

Thank you for the help

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 4, 2022, 8:24am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-for-search-api-not-querying-all-es-nodes/312673/16 "2022-10-04T08:24:20Z")

</div>

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