# Native Java API is slower than the REST one

**URL:** <https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060>\
**Category:** Elasticsearch\
**Created:** [July 22, 2015, 9:05am UTC](https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060 "2015-07-22T09:05:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asaf\_Elias](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Asaf\_Elias](https://discuss.elastic.co/u/Asaf_Elias)\
**Post date:** [July 22, 2015, 9:05am UTC](https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060/1 "2015-07-22T09:05:30Z")

</div>

Continuing the discussion from [Java Client slower than http](https://discuss.elastic.co/t/java-client-slower-than-http/2517):

Hey guys  
I'm having the same issue where a query using the native JAVA client takes much longer than in the REST api (instead of 15 ms, it takes 2500 ms).

I made some checks and it looks like the Execute() part is taking about 40 ms and the get() or actionGet() parts take the rest of the time.

my query (which is derived from the "SearchRequestBuilder.ToString() is":  
POST common/performance/\_search  
{  
"from" : 0,  
"size" : 1400,  
"query" : {  
"query\_string" : {  
"query" : "7",  
"default\_field" : "cluster"  
}  
},  
"post\_filter" : {  
"range" : {  
"timestamp" : {  
"from" : 0,  
"to" : 1538486562000,  
"include\_lower" : true,  
"include\_upper" : true  
}  
}  
},  
"explain" : false,  
"sort" : [ {  
"timestamp" : {  
"order" : "desc"  
}  
} ]  
}

any help will be appreciated.  
Thanks  
Asaf

> [@Java Client slower than http](https://discuss.elastic.co/t/java-client-slower-than-http/2517/1):
>
> We are currently using TransportClient and observing that query response times are considerably slower than using http (through sense plugin). Below is code snippet for initializing TransportClient:
> 
> ```
> > void createClient(Map<String, String> props) {
> Settings settings = ImmutableSettings.settingsBuilder().put(props).build();
> transportClient = new TransportClient(settings);
> String[] hosts = getTransportHosts().split(",");
> for (String host : hosts) {
> int index = host.indexOf(COLON);
> String hostname = host.substring(0, index);
> String port = host.substring(index + 1);
> transportClient.addTransportAddress(new InetSocketTransportAddress(hostname.trim(), Integer.parseInt(port.trim())));
> }
> }
> 
> Map<String, String> createClientConfigSettings() {
> Map<String, String> clientProperties = new HashMap<>();
> clientProperties.put(CLUSTER_NAME_PROPERTY, getClusterName());
> clientProperties.put("client.transport.ping_timeout", "10s");
> clientProperties.put("client.transport.nodes_sampler_interval", "10s");
> String nodeName = processInstanceName();
> clientProperties.put("node.name", nodeName);
> return clientProperties;
> }
> 
> ```
> 
> Please advise.
> 
> Thanks,  
> Prateek

---

<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:** [July 22, 2015, 9:23am UTC](https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060/2 "2015-07-22T09:23:31Z")

</div>

`execute()` launches the query (asynchronous) and `get()` waits for the answer.

What is your exact REST query?

---

<div class="post-metadata">

**Author:** ![Asaf\_Elias](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@Asaf\_Elias](https://discuss.elastic.co/u/Asaf_Elias)\
**Post date:** [July 22, 2015, 9:39am UTC](https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060/3 "2015-07-22T09:39:13Z")

</div>

> [@Asaf\_Elias](#):
>
> POST common/performance/\_search{ "from" : 0, "size" : 1400, "query" : { "query\_string" : { "query" : "7", "default\_field" : "cluster" } }, "post\_filter" : { "range" : { "timestamp" : { "from" : 0, "to" : 1538486562000, "include\_lower" : true, "include\_upper" : true } } }, "explain" : false, "sort" : [{ "timestamp" : { "order" : "desc" } }]}

Hey  
The query i'm sending using Sense is:  
POST common/performance/\_search  
{  
"from" : 0,  
"size" : 1400,  
"query" : {  
"query\_string" : {  
"query" : "7",  
"default\_field" : "cluster"  
}  
},  
"post\_filter" : {  
"range" : {  
"timestamp" : {  
"from" : 0,  
"to" : 1538486562000,  
"include\_lower" : true,  
"include\_upper" : true  
}  
}  
},  
"explain" : false,  
"sort" : [ {  
"timestamp" : {  
"order" : "desc"  
}  
} ]  
}

i've just copied the builder.tostring() to Sense and ran it.

Asaf

---

<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:** [July 5, 2017, 11:59pm UTC](https://discuss.elastic.co/t/native-java-api-is-slower-than-the-rest-one/26060/4 "2017-07-05T23:59:59Z")

</div>


