Java Transport Client slower than curl request

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 .

Settings settings = Settings.builder().put("cluster.name", "elasticsearch-stage").build();
productsClient = TransportClient.builder().settings(settings).build()
    .addTransportAddress(new InetSocketTransportAddress(
	     InetAddress.getByName(conf.getString("elasticsearch.client")),conf.getInt("elasticsearch.port")));
srb = productsClient.prepareSearch();
srb.setIndices(CustomUtility.readFromConfig("elasticsearch.index.name"));
srb.setTypes(CustomUtility.readFromConfig("elasticsearch.index.type"));
srb.setQuery(orFilterCatId);
srb.setFrom((int) startFrom);
srb.setSize(perPage);
response = srb.execute().actionGet();

Could anybody pls help me out with this.....................

I also tried setExpalin(false)....still no improvement

Can you describe what you have in orFilterCatId, perPage?
May be print with a srb.toString() what does the request looks like?

Also can you give the exact query you are running with curl?

Thanks for the response .

Following is srb.toString():

{
"from" : 0,
"size" : 60,
"query" : {
"bool" : {
"must" : [ {
"bool" : {
"should" : {
"term" : {
"catid" : "100"
}
}
}
}, {
"term" : {
"winning_variant" : 1
}
}, {
"term" : {
"is_in_stock" : 1
}
}, {
"range" : {
"qty" : {
"from" : 0,
"to" : null,
"include_lower" : false,
"include_upper" : true
}
}
}, {
"range" : {
"discounted_price" : {
"from" : 0,
"to" : null,
"include_lower" : false,
"include_upper" : true
}
}
}, {
"range" : {
"price" : {
"from" : 0,
"to" : null,
"include_lower" : false,
"include_upper" : true
}
}
} ]
}
},
"explain" : false,
"sort" : [ {
"relevance_score" : {
"order" : "desc"
}
}, {
"qty" : {
"order" : "desc"
}
} ]
}

and I give the same query to curl request which looks like

curl -XGET http://localhost:9200/indexname/typename/_search -d' '

And also hit the same query in Sense.

Sense and curl were executed in 40-60 ms and java API query took above 1000ms

Please format your code using </> icon. It will make your post more readable.

Can you print the output of response.toString() and the curl full response please?

Thanks for the response .

response.toString is as follows

{
"took" : 36,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
}

From above it is clear that it is taking 36 ms but my following lines of code

long startTime = System.currentTimeMillis();
response = srb.execute().actionGet();
long endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);

shows 1752 ms

I am not able to understand this

So the response here is 36ms. Not 1s. You have something wrong which is happening in your code AFTER that.

May be a proxy?

Thanks :slight_smile: ...will check on this.

The curl took time does not take network latency into account.

You should inspect

 SearchResponse response = srb.execute().actionGet();
 response.getTookInMillis();

and compare it with curl's took.