I am using elasticsearch (ES version 5) scroll api to retrieve all the documents and then write into a csv file. My code is as below. This is working, however there is a little problem. It takes more than 5 minutes to download the file.
try
{
TransportClient client = getTransportClient( NoSQLConnectionPool.ELASTIC_STAT );
if( client != null )
{
SearchResponse scrollResp = client.prepareSearch( Constants.INDEX )
.addSort( fieldSort( Constants.PRICE ).order( ASC ) )
.setScroll( new TimeValue( 60000 ) )
.setQuery( buildBoolQuery( request ) )
.setSize( 100 ).get(); //max of 100 hits will be returned for each scroll
//Scroll until no hits are returned
do
{
List<JsonElement> list = getAllElement( scrollResp.getHits().getHits() );
// resultsList.addAll( results );
buildReportContent( sb, list ); //iterate list and append data to string builder(sb)
scrollResp = client.prepareSearchScroll( scrollResp.getScrollId() ).setScroll( new TimeValue( 60000 ) ).execute().actionGet();
}
while( scrollResp.getHits().getHits().length != 0 ); // Zero hits mark the end of the scroll and the while loop.
}
return CsvFileWriter.csvFileWrite( sb );
}
catch ( Exception e )
{
e.printStackTrace();
}
Any suggestions to do this more efficiently?
Thank You!