@@ -380,32 +380,38 @@ private void paginateJSON(SearchConfig c, HttpServletResponse response) throws I
380380 writer .flush ();
381381 }
382382
383- private void exportCSV (SearchConfig c , HttpServletResponse response ) throws IOException
384- {
383+ private void exportCSV (SearchConfig c , HttpServletResponse response ) throws IOException {
385384 PrintWriter writer = response .getWriter ();
386385 IndexSearcher searcher = c .cacheEntry .indexSearcher ;
387- TopFieldDocs topDocs = searcher .search (c .query , Integer .MAX_VALUE , c .sort );
388-
389386 writer .println (String .join ("," , c .fields ));
390387
391- for (ScoreDoc scoreDoc : topDocs .scoreDocs )
392- {
393- Document doc = searcher .storedFields ().document (scoreDoc .doc );
394- List <String > rowValues = new ArrayList <>();
388+ ScoreDoc lastDoc = null ;
389+ int batchSize = 1000 ;
395390
396- for (String fieldName : c .fields )
397- {
398- String [] values = doc .getValues (fieldName );
399- String value = values .length > 0
400- ? String .join ("," , values )
401- : "" ;
402-
403- // Escape strings
404- value = "\" " + value .replace ("\" " , "\" \" " ) + "\" " ;
405- rowValues .add (value );
391+ while (true ) {
392+ TopDocs topDocs = searcher .searchAfter (lastDoc , c .query , batchSize , c .sort );
393+ ScoreDoc [] hits = topDocs .scoreDocs ;
394+
395+ if (hits .length == 0 ) {
396+ break ;
406397 }
407398
408- writer .println (String .join ("," , rowValues ));
399+ for (ScoreDoc scoreDoc : hits ) {
400+ Document doc = searcher .storedFields ().document (scoreDoc .doc );
401+ List <String > rowValues = new ArrayList <>();
402+
403+ for (String fieldName : c .fields ) {
404+ String [] values = doc .getValues (fieldName );
405+ String value = values .length > 0
406+ ? String .join ("," , values )
407+ : "" ;
408+ value = "\" " + value .replace ("\" " , "\" \" " ) + "\" " ;
409+ rowValues .add (value );
410+ }
411+
412+ writer .println (String .join ("," , rowValues ));
413+ }
414+ lastDoc = hits [hits .length - 1 ];
409415 }
410416
411417 writer .flush ();
0 commit comments