1818 */
1919package models ;
2020
21+ import com .google .common .base .Joiner ;
2122import com .google .common .net .MediaType ;
2223import com .google .inject .assistedinject .Assisted ;
2324import com .google .inject .assistedinject .AssistedInject ;
2425import controllers .routes ;
2526import lib .APIException ;
2627import lib .ApiClient ;
28+ import lib .ApiRequestBuilder ;
2729import lib .Tools ;
2830import lib .timeranges .TimeRange ;
2931import models .api .responses .*;
3638
3739import javax .annotation .Nullable ;
3840import java .io .IOException ;
41+ import java .util .Set ;
3942import java .util .concurrent .TimeUnit ;
4043
4144import static lib .Configuration .apiTimeout ;
@@ -102,23 +105,28 @@ private UniversalSearch(ApiClient api, FieldMapper fieldMapper, @Assisted TimeRa
102105 }
103106 }
104107
105- private <T > T doSearch (Class <T > clazz , MediaType mediaType , int pageSize ) throws APIException , IOException {
106- return api .get (clazz )
108+ private <T > T doSearch (Class <T > clazz , MediaType mediaType , int pageSize , Set < String > selectedFields ) throws APIException , IOException {
109+ final ApiRequestBuilder < T > builder = api .get (clazz )
107110 .path ("/search/universal/{0}" , timeRange .getType ().toString ().toLowerCase ())
108111 .queryParams (timeRange .getQueryParams ())
109112 .queryParam ("query" , query )
110113 .queryParam ("limit" , pageSize )
111114 .queryParam ("offset" , page * pageSize )
112115 .queryParam ("filter" , (filter == null ? "*" : filter ))
113- .queryParam ("sort" , order .toApiParam ())
116+ .queryParam ("sort" , order .toApiParam ());
117+ if (selectedFields != null && !selectedFields .isEmpty ()) {
118+ builder .queryParam ("fields" , Joiner .on (',' ).skipNulls ().join (selectedFields ));
119+ }
120+ final T result = builder
114121 .accept (mediaType )
115122 .timeout (KEITH , TimeUnit .SECONDS )
116123 .expect (200 , 400 )
117124 .execute ();
125+ return result ;
118126 }
119127
120128 public SearchResult search () throws IOException , APIException {
121- SearchResultResponse response = doSearch (SearchResultResponse .class , MediaType .JSON_UTF_8 , PER_PAGE );
129+ SearchResultResponse response = doSearch (SearchResultResponse .class , MediaType .JSON_UTF_8 , PER_PAGE , null );
122130 if (response == null ) {
123131 log .error ("We should never get an empty result without throwing an IOException." );
124132 throw new APIException (null , null , new RuntimeException ("Empty search response, this is likely a bug in exception handling." ));
@@ -140,8 +148,8 @@ public SearchResult search() throws IOException, APIException {
140148 return result ;
141149 }
142150
143- public String searchAsCsv () throws IOException , APIException {
144- return doSearch (String .class , MediaType .CSV_UTF_8 , 100000 ); // TODO fix huge page size by using scroll searches and streaming results
151+ public String searchAsCsv (Set < String > selectedFields ) throws IOException , APIException {
152+ return doSearch (String .class , MediaType .CSV_UTF_8 , 10_000 , selectedFields ); // TODO make use of streaming support in the server.
145153 }
146154
147155 public DateHistogramResult dateHistogram (String interval ) throws IOException , APIException {
@@ -243,6 +251,7 @@ public Call getCsvRoute(Request request, Stream stream) {
243251 String from = Tools .stringSearchParamOrEmpty (request , "from" );
244252 String to = Tools .stringSearchParamOrEmpty (request , "to" );
245253 String keyword = Tools .stringSearchParamOrEmpty (request , "keyword" );
254+ String fields = Tools .stringSearchParamOrEmpty (request , "fields" );
246255 if (stream == null ) {
247256 return routes .SearchController .exportAsCsv (
248257 query ,
@@ -251,7 +260,8 @@ public Call getCsvRoute(Request request, Stream stream) {
251260 relative ,
252261 from ,
253262 to ,
254- keyword
263+ keyword ,
264+ fields
255265 );
256266 } else {
257267 return routes .StreamSearchController .exportAsCsv (
@@ -261,7 +271,8 @@ public Call getCsvRoute(Request request, Stream stream) {
261271 relative ,
262272 from ,
263273 to ,
264- keyword
274+ keyword ,
275+ fields
265276 );
266277 }
267278 }
0 commit comments