# Error with elasticsearch and java

**URL:** <https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115>\
**Category:** Elasticsearch\
**Created:** [May 25, 2017, 11:32am UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115 "2017-05-25T11:32:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zegdene](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zegdene](https://discuss.elastic.co/u/zegdene)\
**Post date:** [May 25, 2017, 11:32am UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115/1 "2017-05-25T11:32:19Z")

</div>

Hi,  
I need to extract data from elasticsearch with java but this return an main exception

```
while(true){
				TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
						   .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
				SearchRequestBuilder srb2 = client
					    .prepareSearch().setQuery(QueryBuilders.matchQuery("Time",0)).setSize(1);
				
				MultiSearchResponse sr = client.prepareMultiSearch().add(srb2).get();
				
				
				for (MultiSearchResponse.Item item : sr.getResponses()) {
					
					SearchResponse response = item.getResponse();
					for(int i = 0; i < response.getHits().getTotalHits(); i ++){
					SearchHit sHit = response.getHits().getAt(i);
					SearchHits sHits = response.getHits();
					SearchHit[] sHitList = response.getHits().getHits();
					for(SearchHit sHitItem : sHitList){
						System.out.println("sHitItem ==> "+sHitItem.getSourceAsString());
						Data data = ConvertConfig.ConvertToData(sHitItem.getSourceAsString());
						data.setId(sHitItem.getId());
						RowData row = Prediction.predictionData(data);
						System.out.print("\t "+row.get("Classe").toString());
					}
					}
				}

				
				client.close();
				
			}

```

error:

> Exception in thread "main" Class probabilities: 0java.lang.ArrayIndexOutOfBoundsException: 1  
> at org.elasticsearch.search.SearchHits.getAt(SearchHits.java:117)  
> at com.backend.project.BackendProject.main(BackendProject.java:57)

any help please  
thank you

---

<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:** [May 25, 2017, 11:48am UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115/2 "2017-05-25T11:48:18Z")

</div>

```auto
while(true){
}

```

OMG! Why are you doing this?  
At least create your client outside of the loop.  
And close it after.

Then be aware that even though elasticsearch tells you you have 1 000 000 of documents which are matching (`getTotalHits()`) , it will send you back only the 10 first most relevant documents.

So you should not iterate like this:

```auto
for(int i = 0; i < response.getHits().getTotalHits(); i ++)

```

---

<div class="post-metadata">

**Author:** ![zegdene](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zegdene](https://discuss.elastic.co/u/zegdene)\
**Post date:** [May 25, 2017, 1:50pm UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115/3 "2017-05-25T13:50:41Z")

</div>

Hi David ,  
I use a "getTotalHits" because i need to find all id by field\_time for example when time = 0 the return is total hits when time=0

---

<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:** [May 25, 2017, 3:48pm UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115/4 "2017-05-25T15:48:53Z")

</div>

You need to use the scroll API as I said.

See [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html)

---

<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:** [June 22, 2017, 3:49pm UTC](https://discuss.elastic.co/t/error-with-elasticsearch-and-java/87115/5 "2017-06-22T15:49:05Z")

</div>

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