# Elasticsearch Java client much slower than rest call

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703>\
**Category:** Elasticsearch\
**Created:** [November 13, 2012, 7:30pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703 "2012-11-13T19:30:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DMAC](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DMAC](https://discuss.elastic.co/u/DMAC)\
**Post date:** [November 13, 2012, 7:30pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/1 "2012-11-13T19:30:53Z")

</div>

Hi

We are starting to use the Java API in ElasticSearch. The only problem is  
that the queries seem to take much longer to retrieve data than simply  
using curl.

Our development server(19.08) has very small index (2000 documents, with 8  
fields)

When making a call to retrieve ~1200 documents it takes 17 seconds to run a  
query  
versus \< 1 second to get the same result using curl

Here is the code I am using to test ES

LOG.info(String.format("Initializing connection to ElasticSearch %s/%s on  
%d", host, clusterName, port));  
settings = ImmutableSettings.settingsBuilder()  
.put("client.transport.sniff",true).build();  
client = new TransportClient(settings).addTransportAddress(new  
InetSocketTransportAddress(host, port));  
searchRequest = client.prepareSearch(clusterName);  
mapper = new SerObjectMapper();

BoolQueryBuilder query = boolQuery();  
for(String term : keywordList)  
query.should(fieldQuery("body", term));

long start = System.currentTimeMillis();  
/\*\* From here _/  
SearchResponse response = client.prepareSearch(clusterName)  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(query).setFrom(0).setSize(limit).setExplain(true)  
.execute()  
.actionGet();  
/_\* To Here takes 17 seconds in Java. \*/  
SearchHit[] docs = response.getHits().getHits();  
System.err.println("Query took " + (System.currentTimeMillis()-start));  
for(SearchHit doc : docs)  
urlList.add((String)doc.getSource().get("url"));  
LOG.info(String.format("Returning %d results", urlList.size()));

I wonder if you could point out what I am doing wrong?

Thanks in advance

D.

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [November 13, 2012, 7:53pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/2 "2012-11-13T19:53:38Z")

</div>

Switch off explain, setExplain(false)

Unfortunately, this is in the  
docs [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/search.html) but  
it's not the default, only an optional setting.

Best regards,

Jörg

On Tuesday, November 13, 2012 8:30:53 PM UTC+1, DMAC wrote:

> Hi
> 
> We are starting to use the Java API in Elasticsearch. The only problem is  
> that the queries seem to take much longer to retrieve data than simply  
> using curl.
> 
> Our development server(19.08) has very small index (2000 documents, with 8  
> fields)
> 
> When making a call to retrieve ~1200 documents it takes 17 seconds to run  
> a query  
> versus \< 1 second to get the same result using curl
> 
> Here is the code I am using to test ES
> 
> LOG.info(String.format("Initializing connection to Elasticsearch %s/%s on  
> %d", host, clusterName, port));  
> settings = ImmutableSettings.settingsBuilder()  
> .put("client.transport.sniff",true).build();  
> client = new TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress(host, port));  
> searchRequest = client.prepareSearch(clusterName);  
> mapper = new SerObjectMapper();
> 
> BoolQueryBuilder query = boolQuery();  
> for(String term : keywordList)  
> query.should(fieldQuery("body", term));
> 
> long start = System.currentTimeMillis();  
> /\*\* From here _/  
> SearchResponse response = client.prepareSearch(clusterName)  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(query).setFrom(0).setSize(limit).setExplain(true)  
> .execute()  
> .actionGet();  
> /_\* To Here takes 17 seconds in Java. \*/  
> SearchHit docs = response.getHits().getHits();  
> System.err.println("Query took " + (System.currentTimeMillis()-start));  
> for(SearchHit doc : docs)  
> urlList.add((String)doc.getSource().get("url"));  
> LOG.info(String.format("Returning %d results", urlList.size()));
> 
> I wonder if you could point out what I am doing wrong?
> 
> Thanks in advance
> 
> D.

--

---

<div class="post-metadata">

**Author:** ![Derry\_O\_Sullivan](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@Derry\_O\_Sullivan](https://discuss.elastic.co/u/Derry_O_Sullivan)\
**Post date:** [November 14, 2012, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/3 "2012-11-14T08:41:05Z")

</div>

2 other points on this.

1. I'm not sure what limit is (1200)? but returning that many values  
(versus returning the default of 10) makes a big difference
2. Are you doing exactly the same search in the REST call (e.g.  
DFS\_QUERY\_THEN\_SEARCH search type, num results etc)?

We have done lots of testing with both http/rest with lots of search  
types/limits and i don't think i've every seen such a difference (or  
anything near that) in terms of timings. (using ES 0.19.9 over a multi-node  
cluster with millions of docs)

On Tuesday, 13 November 2012 19:30:53 UTC, DMAC wrote:

> Hi
> 
> We are starting to use the Java API in Elasticsearch. The only problem is  
> that the queries seem to take much longer to retrieve data than simply  
> using curl.
> 
> Our development server(19.08) has very small index (2000 documents, with 8  
> fields)
> 
> When making a call to retrieve ~1200 documents it takes 17 seconds to run  
> a query  
> versus \< 1 second to get the same result using curl
> 
> Here is the code I am using to test ES
> 
> LOG.info(String.format("Initializing connection to Elasticsearch %s/%s on  
> %d", host, clusterName, port));  
> settings = ImmutableSettings.settingsBuilder()  
> .put("client.transport.sniff",true).build();  
> client = new TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress(host, port));  
> searchRequest = client.prepareSearch(clusterName);  
> mapper = new SerObjectMapper();
> 
> BoolQueryBuilder query = boolQuery();  
> for(String term : keywordList)  
> query.should(fieldQuery("body", term));
> 
> long start = System.currentTimeMillis();  
> /\*\* From here _/  
> SearchResponse response = client.prepareSearch(clusterName)  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(query).setFrom(0).setSize(limit).setExplain(true)  
> .execute()  
> .actionGet();  
> /_\* To Here takes 17 seconds in Java. \*/  
> SearchHit docs = response.getHits().getHits();  
> System.err.println("Query took " + (System.currentTimeMillis()-start));  
> for(SearchHit doc : docs)  
> urlList.add((String)doc.getSource().get("url"));  
> LOG.info(String.format("Returning %d results", urlList.size()));
> 
> I wonder if you could point out what I am doing wrong?
> 
> Thanks in advance
> 
> D.

--

---

<div class="post-metadata">

**Author:** ![DMAC](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@DMAC](https://discuss.elastic.co/u/DMAC)\
**Post date:** [December 4, 2012, 10:30am UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/4 "2012-12-04T10:30:11Z")

</div>

Hi,

Thanks. Sorry for the slow response. It turns out that it was my fault, it was the way I was serialising the data.

Regards

D.

On 14 Nov 2012, at 08:41, Derry O' Sullivan wrote:

> 2 other points on this.
> 
> 1. I'm not sure what limit is (1200)? but returning that many values (versus returning the default of 10) makes a big difference
> 2. Are you doing exactly the same search in the REST call (e.g. DFS\_QUERY\_THEN\_SEARCH search type, num results etc)?
> 
> We have done lots of testing with both http/rest with lots of search types/limits and i don't think i've every seen such a difference (or anything near that) in terms of timings. (using ES 0.19.9 over a multi-node cluster with millions of docs)
> 
> On Tuesday, 13 November 2012 19:30:53 UTC, DMAC wrote:  
> Hi
> 
> We are starting to use the Java API in Elasticsearch. The only problem is that the queries seem to take much longer to retrieve data than simply using curl.
> 
> Our development server(19.08) has very small index (2000 documents, with 8 fields)
> 
> When making a call to retrieve ~1200 documents it takes 17 seconds to run a query  
> versus \< 1 second to get the same result using curl
> 
> Here is the code I am using to test ES
> 
> LOG.info(String.format("Initializing connection to Elasticsearch %s/%s on %d", host, clusterName, port));  
> settings = ImmutableSettings.settingsBuilder()  
> .put("client.transport.sniff",true).build();  
> client = new TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress(host, port));  
> searchRequest = client.prepareSearch(clusterName);  
> mapper = new SerObjectMapper();
> 
> BoolQueryBuilder query = boolQuery();  
> for(String term : keywordList)  
> query.should(fieldQuery("body", term));
> 
> long start = System.currentTimeMillis();  
> /\*\* From here _/  
> SearchResponse response = client.prepareSearch(clusterName)  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(query).setFrom(0).setSize(limit).setExplain(true)  
> .execute()  
> .actionGet();  
> /_\* To Here takes 17 seconds in Java. \*/  
> SearchHit docs = response.getHits().getHits();  
> System.err.println("Query took " + (System.currentTimeMillis()-start));  
> for(SearchHit doc : docs)  
> urlList.add((String)doc.getSource().get("url"));  
> LOG.info(String.format("Returning %d results", urlList.size()));
> 
> I wonder if you could point out what I am doing wrong?
> 
> Thanks in advance
> 
> D.
> 
> --

--

---

<div class="post-metadata">

**Author:** ![NRS](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@NRS](https://discuss.elastic.co/u/NRS)\
**Post date:** [October 3, 2016, 12:31pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/5 "2016-10-03T12:31:09Z")

</div>

I am facing the same issue . When I use curl to get response , it takes around 40-50 ms . But When same query is executed using execute().actionGet() using TransportClient , it takes around 1000 ms. Trying to figure out the root cause and the solution of this problem . Could anybody pls help me out with this

---

<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:** [October 3, 2016, 1:15pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/6 "2016-10-03T13:15:08Z")

</div>

You should better open a new thread and describe exactly what you are doing and in which context (version).  
I'm fairly sure you are not doing the exact same thing.

---

<div class="post-metadata">

**Author:** ![NRS](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@NRS](https://discuss.elastic.co/u/NRS)\
**Post date:** [October 3, 2016, 3:05pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/7 "2016-10-03T15:05:27Z")

</div>

Thanks for the reply.  
Open new thread as [Java Transport Client slower than curl request](https://discuss.elastic.co/t/java-transport-client-slower-than-curl-request/62067)

---

<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, 10:15pm UTC](https://discuss.elastic.co/t/elasticsearch-java-client-much-slower-than-rest-call/9703/8 "2017-07-05T22:15:23Z")

</div>


