@@ -52,18 +52,21 @@ public class DataFormatPluginTest {
5252 private static final File csvTempFile ;
5353 private static final File xlsTempFile ;
5454 private static final File jsonTempFile ;
55+ private static final File jsonListTempFile ;
5556 private static final String path ;
5657
5758 private final Map <String , String > paramsCsv = new HashMap <>();
5859 private final Map <String , String > paramsXls = new HashMap <>();
5960 private final Map <String , String > paramsJson = new HashMap <>();
61+ private final Map <String , String > paramsJsonList = new HashMap <>();
6062
6163 static {
6264 docNumber = 20 ;
6365
6466 csvTempFile = createTempFile ("csvtest" , ".csv" );
6567 xlsTempFile = createTempFile ("xlstest" , ".xls" );
6668 jsonTempFile = createTempFile ("jsontest" , ".json" );
69+ jsonListTempFile = createTempFile ("jsonlisttest" , ".json" );
6770 path = "/dataset0/_data" ;
6871 }
6972
@@ -106,13 +109,15 @@ public void prepareParams() {
106109 paramsCsv .put ("format" , "csv" );
107110 paramsXls .put ("format" , "xls" );
108111 paramsJson .put ("format" , "json" );
112+ paramsJsonList .put ("format" , "jsonlist" );
109113 }
110114
111115 @ After
112116 public void clearParams () {
113117 paramsCsv .clear ();
114118 paramsXls .clear ();
115119 paramsJson .clear ();
120+ paramsJsonList .clear ();
116121 }
117122
118123 @ Test
@@ -421,6 +426,106 @@ public void dumpJsonInFile() throws IOException {
421426 }
422427 }
423428
429+ @ Test
430+ public void dumpJsonList () throws IOException {
431+
432+ // Download All as JSON
433+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
434+ .header ("Content-Type" , "application/json" )
435+ .param ("format" , "jsonlist" ).execute ()) {
436+ final String content = curlResponse .getContentAsString ();
437+ final String [] lines = content .split ("\n " );
438+ assertEquals (docNumber + 2 , lines .length );
439+ assertTrue (lines [0 ].equals ("[" ));
440+ assertTrue (lines [1 ].startsWith ("{" + "\" aaa\" :\" test" ));
441+ assertTrue (lines [docNumber + 1 ].equals ("]" ));
442+ }
443+
444+ final String query = "{\" query\" :{\" bool\" :{\" must\" :[{\" range\" :{\" bbb\" :{\" from\" :\" 1\" ,\" to\" :\" 10\" }}}],\" must_not\" :[],\" should\" :[]}},\" sort\" :[\" bbb\" ]}" ;
445+
446+ // Download 10 docs as JSON with Query
447+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
448+ .header ("Content-Type" , "application/json" )
449+ .param ("format" , "jsonlist" )
450+ .param ("search_type" , "query_then_fetch" ).body (query )
451+ .execute ()) {
452+ final String content = curlResponse .getContentAsString ();
453+ final String [] lines = content .split ("\n " );
454+ assertEquals (10 + 2 , lines .length );
455+ assertTrue (lines [0 ].startsWith ("[" ));
456+ assertTrue (lines [1 ].startsWith ("{" + "\" aaa\" :\" test" ));
457+ }
458+
459+ // Download 10 docs as JSON
460+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
461+ .header ("Content-Type" , "application/json" ).param ("q" , "*:*" )
462+ .param ("format" , "jsonlist" ).param ("from" , "5" ).execute ()) {
463+ final String content = curlResponse .getContentAsString ();
464+ final String [] lines = content .split ("\n " );
465+ assertEquals (15 + 2 , lines .length );
466+ }
467+
468+ // Download all the docs from the 5th as JSON
469+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
470+ .header ("Content-Type" , "application/json" ).param ("q" , "*:*" )
471+ .param ("format" , "jsonlist" ).param ("from" , "5" )
472+ .param ("size" , String .valueOf (docNumber )).execute ()) {
473+ final String content = curlResponse .getContentAsString ();
474+ final String [] lines = content .split ("\n " );
475+ assertEquals ((docNumber - 5 ) + 2 , lines .length );
476+ }
477+
478+ final String queryWithFrom = "{\" query\" :{\" match_all\" :{}},\" from\" :5,\" size\" :" + String .valueOf (docNumber ) + ",\" sort\" :[\" bbb\" ]}" ;
479+
480+ // Download All as JSON with Query and from
481+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
482+ .header ("Content-Type" , "application/json" )
483+ .param ("format" , "jsonlist" ).body (queryWithFrom ).execute ()) {
484+ final String content = curlResponse .getContentAsString ();
485+ final String [] lines = content .split ("\n " );
486+ assertEquals ((docNumber - 5 ) + 2 , lines .length );
487+ }
488+
489+ // Download All as JSON with Query and from
490+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
491+ .header ("Content-Type" , "application/json" )
492+ .param ("format" , "jsonlist" ).param ("source" , queryWithFrom )
493+ .param ("source_content_type" , "application/json" )
494+ .execute ()) {
495+ final String content = curlResponse .getContentAsString ();
496+ final String [] lines = content .split ("\n " );
497+ assertEquals ((docNumber - 5 ) + 2 , lines .length );
498+ }
499+
500+ // Download All as JSON with search_type
501+ try (CurlResponse curlResponse = EcrCurl .get (node , "/dataset0/_data" )
502+ .header ("Content-Type" , "application/json" )
503+ .param ("search_type" , "query_then_fetch" )
504+ .param ("format" , "jsonlist" ).execute ()) {
505+ final String content = curlResponse .getContentAsString ();
506+ final String [] lines = content .split ("\n " );
507+ assertEquals (docNumber + 2 , lines .length );
508+ assertTrue (lines [0 ].equals ("[" ));
509+ assertTrue (lines [1 ].startsWith ("{" + "\" aaa\" :\" test" ));
510+ assertTrue (lines [docNumber + 1 ].equals ("]" ));
511+ }
512+ }
513+
514+ @ Test
515+ public void dumpJsonListInFile () throws IOException {
516+ paramsJsonList .put ("file" , jsonListTempFile .getAbsolutePath ());
517+
518+ try (CurlResponse curlResponse = createRequest (node , path , paramsJsonList ).execute ()) {
519+ assertAcknowledged (curlResponse , jsonListTempFile );
520+ final List <String > lines = Files .readAllLines (jsonListTempFile .toPath (), Charsets .UTF_8 );
521+ assertEquals (docNumber + 2 , lines .size ());
522+ assertTrue (lines .get (0 ).equals ("[" ));
523+ assertTrue (lines .get (1 ).startsWith ("{" + "\" aaa\" :\" test" ));
524+ assertTrue (lines .get (docNumber ).startsWith ("{" + "\" aaa\" :\" test" ));
525+ assertTrue (lines .get (docNumber + 1 ).equals ("]" ));
526+ }
527+ }
528+
424529 @ Test
425530 public void dumpSizeLimit () throws IOException {
426531
0 commit comments