# Java API - trying to fetch data from Index

**URL:** <https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971>\
**Category:** Elasticsearch\
**Created:** [June 27, 2020, 8:21pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971 "2020-06-27T20:21:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Venkat\_Raj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkat_raj/32/51823_2.png) [@Venkat\_Raj](https://discuss.elastic.co/u/Venkat_Raj)\
**Post date:** [June 27, 2020, 8:21pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971/1 "2020-06-27T20:21:29Z")

</div>

I am trying to fetch data from index using java API to generate a report using jasper reports.  
I have added the maven dependency and able to make connection to ES.  
While trying to fetch the data corresponding to the particular index, I am able to get the response when i pass the Index value, \_type and \_id in the request.  
But I need to get all the data pertaining to that idex, so I am passing the Index value alone in the request, but I getting the below error:

```auto
Exception in thread "main" org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: id is missing;
	at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:26)
	at org.elasticsearch.action.get.GetRequest.validate(GetRequest.java:133)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1420)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
	at org.elasticsearch.client.RestHighLevelClient.get(RestHighLevelClient.java:714)

```

Below is my piece of code:

```auto
 GetRequest getPersonRequest = new GetRequest("INDEX");`
 RequestOptions requestOptions = RequestOptions.DEFAULT;
    GetResponse getResponse = null;

    try {
                getResponse = client.get(getPersonRequest,requestOptions);
                System.out.println("Returned value = " + getResponse.getSourceAsMap`Preformatted text`());
            } catch (java.io.IOException e){
                e.getLocalizedMessage();
            }

```

---

<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:** [June 27, 2020, 9:55pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971/2 "2020-06-27T21:55:17Z")

</div>

Have a look at the search api and may be the scroll api if you have more than 10000 results.

---

<div class="post-metadata">

**Author:** ![Venkat\_Raj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/venkat_raj/32/51823_2.png) [@Venkat\_Raj](https://discuss.elastic.co/u/Venkat_Raj)\
**Post date:** [June 30, 2020, 6:59pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971/3 "2020-06-30T18:59:41Z")

</div>

Hi David,

Thanks for the wonderful information, I am able to get the response based on the index as required using the search API.  
But the thing is if I pass the index value along with a querystringquery in the request for a particular timestamp, it always provides the response with only 10 hits. please find the code below:

```auto
SearchRequest searchRequest = new SearchRequest("index-*");
SearchSourceBuilder scb = new SearchSourceBuilder();
scb.timeout(new TimeValue(240, TimeUnit.SECONDS));
scb.query(QueryBuilders.queryStringQuery("@timestamp:[2020-05-25 TO 2020-05-27]"));
searchRequest.source(scb);

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit: searchHits){
            System.out.println("Source response = " + hit.getSourceAsString());
            responseMap = hit.getSourceAsMap();
            System.out.println("Returned Response = " + responseMap);
        }
        System.out.println("Returned Response = " + searchHits);

```

This query will always return me results with timestamp pertaining to 2020-05-25T13:25:33.566Z with slight variation in the seconds. So i could see only 10 records returned in the response.

Do I need to add any kind of filter or any such parameters in the request to actually get the filedname as per my requirement?

---

<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:** [June 30, 2020, 8:34pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971/4 "2020-06-30T20:34:25Z")

</div>

You can use:

- the [`size` and `from` parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html) to display by default up to 10000 records to your users. If you want to change this limit, you can change `index.max_result_window` setting but be aware of the consequences (ie memory).
- the [search after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html) feature to do deep pagination.
- the [Scroll API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html) if you want to extract a resultset to be consumed by another tool later.

---

<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 28, 2020, 8:34pm UTC](https://discuss.elastic.co/t/java-api-trying-to-fetch-data-from-index/238971/5 "2020-07-28T20:34:32Z")

</div>

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