# Fetching data from elastic search using java code but performance is slow

**URL:** <https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120>\
**Category:** Elasticsearch\
**Created:** [September 20, 2017, 7:22am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120 "2017-09-20T07:22:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pravin1](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pravin1](https://discuss.elastic.co/u/pravin1)\
**Post date:** [September 20, 2017, 7:22am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/1 "2017-09-20T07:22:19Z")

</div>

Hi,

I implement below code for fetching data from Elastic Search, but performance is very slow.  
Please check below code and please improve the solution

```auto
package com.cemciq.test;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.lucene.search.Query;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cemciq.Constents;
import com.cemciq.solr.client.CemSolrClient;
import com.cemciq.util.CemException;

public class TestClass {
	public static void main(String[] args) throws CemException, IOException, JSONException {

	
		TestClass.getCIQMatchedCompanyListForSorted1();
		
	}
	
public static void getCIQMatchedCompanyListForSorted1() throws CemException, IOException, JSONException {
		
	
		Map<String, String> paramMap = new HashMap<String, String>();
		List testList=new ArrayList();
		SearchResponse response1;
		//Client client; 
		JSONArray hitsArray;
		Query query;
		RestClient restClient = RestClient.builder(new HttpHost("172.21.153.176", 9200, "http")).build();
		String coalitionId="";
		
		
		String abc="12 WEST CAPITAL MANAGEMENT LP";
		
		//paramMap.put("minimum_should_match", "90");
		//paramMap.put("pretty", "true");
		
		HttpEntity entity = new NStringEntity(
				"{\n" +
		
						" \"query\" : {\n" +
							
						"	\"bool\" : {\n"+
								"	\"minimum_should_match\" : \"10%\","+
								
								" \"must\": [ \n"+
										"{ \n"+
										
											" \"match_phrase\" : { \n"+
											" \"bank_client_entity_name.keyword\" : { \n"
											+ " \"query\" : \"12 WEST CAPITAL MANAGEMENT LP\" } \n"+
											"} \n"+
								
											"} \n"+
										"] \n"+	
						
						          "} \n"+
						
						       "}, \n"+ 
						" \"size\" : 10 \n"+
						"}", ContentType.APPLICATION_JSON);
		
	
		Response response = restClient.performRequest("GET", "/hist_latest_5.5.0/_search",Collections.<String, String>emptyMap(),
				entity);
		BufferedInputStream br = new BufferedInputStream(response.getEntity().getContent());
		
		String res = "";
		while (br.available()>0) {
	  		res += (char)br.read();
	  	}
		
		 br.close();
		 
		
		 
	    JSONObject json = new JSONObject(res);
		JSONObject hits = json.getJSONObject("hits");
		 hitsArray = hits.getJSONArray("hits");
		ArrayList<Object> coalitionList = new ArrayList<Object>();
		for (int i=0; i<hitsArray.length(); i++) {
			ArrayList<String> fileNameIdArrList = new ArrayList<String>();
		JSONObject h = hitsArray.getJSONObject(i);
		JSONObject sourceJObj = h.getJSONObject("_source");
		
		System.out.println("sourceJObj :"+i+" "+sourceJObj);
		
		
	}
		
		
	
}
	
	
	
	
}

```

---

<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:** [September 20, 2017, 12:32pm UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/2 "2017-09-20T12:32:16Z")

</div>

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

I'm editing your code...

How do you know it's "slow"?

Can you add a trace before and after this line and print current time:

```auto
Response response = restClient.performRequest("GET", "/hist_latest_5.5.0/_search",Collections.<String, String>emptyMap(),
			entity);

```

---

<div class="post-metadata">

**Author:** ![pravin1](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pravin1](https://discuss.elastic.co/u/pravin1)\
**Post date:** [September 21, 2017, 6:05am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/3 "2017-09-21T06:05:36Z")

</div>

Hi,  
Please check below code, we get data in 790 millisec, but i want in 300 to 400 millisec  
So, please tell me possible or not and any another solution

Long startTimeForSolrSearch=0l;  
Long endTimeForSolrSearch=0l;  
startTimeForSolrSearch=0l;  
endTimeForSolrSearch=0l;  
startTimeForSolrSearch=Calendar.getInstance().getTimeInMillis();  
TestClass.getCIQMatchedCompanyListForSorted1();

```
	endTimeForSolrSearch=Calendar.getInstance().getTimeInMillis();
	
    System.out.println("Time Taken for HIS by bankClientEntityId from Solr: "+(endTimeForSolrSearch-startTimeForSolrSearch) +" ms.");
```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 21, 2017, 6:25am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/4 "2017-09-21T06:25:09Z")

</div>

Why are you creating the client within the code you are benchmarking? Typically you would reuse an existing client and not do this per request.

---

<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:** [September 21, 2017, 7:07am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/5 "2017-09-21T07:07:27Z")

</div>

Please again, format your code.  
Also can you print what is the JSON response? I'd like to see the `took` value.

---

<div class="post-metadata">

**Author:** ![pravin1](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pravin1](https://discuss.elastic.co/u/pravin1)\
**Post date:** [September 21, 2017, 7:58am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/6 "2017-09-21T07:58:15Z")

</div>

Hi,

the above code is for demo purpose.  
In actually i am creating one client for all record

---

<div class="post-metadata">

**Author:** ![pravin1](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pravin1](https://discuss.elastic.co/u/pravin1)\
**Post date:** [September 21, 2017, 8:07am UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/7 "2017-09-21T08:07:54Z")

</div>

Hi,  
Please check below output in json

OUTPUT:

sourceJObj :0 {"parent2\_entity\_sector":null,"period\_name":"FY16","parent4\_entity\_region":null,"coalition\_id":"342938","parent1\_entity\_country":null,"user\_confirmation":"Verified","bank\_id":12,"product\_id":null,"cbcdid":null,"bank\_client\_entity\_sector":null,"parent4\_entity\_country":null,"id":136008771,"qc\_source":null,"parent2\_entity\_country":null,"parent3\_entity\_sector":null,"parent1\_entity\_region":null,"parent4\_client\_entity\_id":null,"hdid":null,"region\_id":null,"ra\_comments":"this is RA","bank\_client\_entity\_country":null,"listsource":"INST","parent1\_client\_entity\_id":null,"parent3\_entity\_country":null,"coalition\_name":"J. Goldman & Co","parent5\_entity\_region":null,"ultimate\_id":"158358908","ciq\_name":"12 West Capital Management LP","parent2\_entity\_region":null,"parent2\_client\_entity\_id":null,"parent1\_entity\_name":null,"parent3\_entity\_name":null,"parent5\_entity\_country":null,"parent4\_entity\_name":null,"bank\_client\_entity\_region":null,"parent5\_entity\_name":null,"parent4\_entity\_sector":null,"parent5\_entity\_sector":null,"user\_verified\_date":"2017-09-18T10:24:13.000Z","parent3\_client\_entity\_id":null,"qc\_comments":"test qc comment","bank\_name":null,"@version":"1","parent2\_entity\_name":null,"ra\_source":null,"ciq\_id":"158358908","date\_status":"2017-09-17T22:24:13.000Z","ultimate\_parent":"12 West Capital Management LP","bank\_client\_entity\_name":"12 WEST CAPITAL MANAGEMENT LP","parent5\_client\_entity\_id":null,"bank\_client\_entity\_id":null,"record\_type":null,"parent3\_entity\_region":null,"@timestamp":"2017-09-20T10:53:10.068Z","timeperiod\_id":16,"parent1\_entity\_sector":null,"ciq\_status":"Perfect - UP Shortlisted - 1H17","ultimatepercent":null,"companycountry":null}  
Time Taken for HIS by bankClientEntityId from Solr: 720 ms.

---

<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:** [September 21, 2017, 4:38pm UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/8 "2017-09-21T16:38:51Z")

</div>

Not a valid search response. Sorry.

Can you print the full content of the response please?  
took field should be there.

And please format your code

---

<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 19, 2017, 4:39pm UTC](https://discuss.elastic.co/t/fetching-data-from-elastic-search-using-java-code-but-performance-is-slow/101120/9 "2017-10-19T16:39:08Z")

</div>

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