Java API - trying to fetch data from Index

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:

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:

 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();
            }

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

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:

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?

You can use:

  • the size and from parameters 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 feature to do deep pagination.
  • the Scroll API if you want to extract a resultset to be consumed by another tool later.

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