1818import org .springframework .util .Assert ;
1919import org .springframework .util .CollectionUtils ;
2020
21- import java .util .ArrayList ;
22- import java .util .LinkedHashMap ;
23- import java .util .List ;
24- import java .util .Map ;
21+ import java .util .*;
2522import java .util .stream .Collectors ;
2623
2724public class RequestDsl {
@@ -74,7 +71,25 @@ private void applyPagination(Pageable pageable, int top) {
7471 if (pageable != null && pageable .isPaged ()) {
7572 SearchUtil .applyPaginationAndSort (searchRequestBuilder , pageable , top );
7673 } else {
77- searchRequestBuilder .size (10000 );
74+ if (visualization .getChartType () == ChartType .LIST_CHART ) {
75+
76+ searchRequestBuilder .size (10000 );
77+
78+ Set <String > fields = new HashSet <>();
79+ AggregationType agg = visualization .getAggregationType ();
80+ if (agg != null && agg .getBucket () != null ) {
81+ collectBucketFields (agg .getBucket (), fields );
82+ }
83+
84+ List <String > includeList = new ArrayList <>(fields )
85+ .stream ()
86+ .map (this ::normalizeField )
87+ .toList ();
88+
89+ searchRequestBuilder .source (s -> s .filter (f -> f .excludes ("*" )));
90+ searchRequestBuilder .source (s -> s .filter (f
91+ -> f .includes (includeList )));
92+ }
7893 }
7994 }
8095
@@ -98,7 +113,6 @@ public SearchRequest.Builder getSearchSourceBuilderForCount() throws UtmElastics
98113 }
99114
100115
101-
102116 /**
103117 * Build an aggregation section for an elasticsearch dsl request
104118 *
@@ -157,19 +171,19 @@ private Map<String, Aggregation.Builder.ContainerBuilder> buildBucketAggregation
157171 switch (bucket .getAggregation ()) {
158172 case TERMS :
159173 TermsAggregation term = new TermsAggregation .Builder ().field (bucket .getField ())
160- .size (bucket .getTerms ().getSize ()).order (List .of (Map .of (bucket .getTerms ().getSortBy (),
161- bucket .getTerms ().getAsc () ? SortOrder .Asc : SortOrder .Desc ))).build ();
174+ .size (bucket .getTerms ().getSize ()).order (List .of (Map .of (bucket .getTerms ().getSortBy (),
175+ bucket .getTerms ().getAsc () ? SortOrder .Asc : SortOrder .Desc ))).build ();
162176 bucketAggregations .put (bucket .toString (), new Aggregation .Builder ().terms (term ));
163177 break ;
164178 case RANGE :
165179 RangeAggregation range = new RangeAggregation .Builder ().field (bucket .getField ())
166- .ranges (bucket .getRanges ().stream ().map (r -> AggregationRange .of (a -> a .from (String .valueOf (r .getFrom ()))
167- .to (String .valueOf (r .getTo ())))).collect (Collectors .toList ())).build ();
180+ .ranges (bucket .getRanges ().stream ().map (r -> AggregationRange .of (a -> a .from (String .valueOf (r .getFrom ()))
181+ .to (String .valueOf (r .getTo ())))).collect (Collectors .toList ())).build ();
168182 bucketAggregations .put (bucket .toString (), new Aggregation .Builder ().range (range ));
169183 break ;
170184 case DATE_HISTOGRAM :
171185 DateHistogramAggregation .Builder histogram = new DateHistogramAggregation .Builder ().field (bucket .getField ())
172- .timeZone (TimezoneUtil .getAppTimezone ().toString ());
186+ .timeZone (TimezoneUtil .getAppTimezone ().toString ());
173187 String interval = bucket .getDateHistogram ().getInterval ();
174188 if (bucket .getDateHistogram ().isFixedInterval ())
175189 histogram .fixedInterval (i -> i .time (interval ));
@@ -204,24 +218,24 @@ private Map<String, Aggregation> buildMetricAggregation(List<Metric> metrics) {
204218 switch (metric .getAggregation ()) {
205219 case AVERAGE :
206220 metricAggregations .put (metric .getId (), Aggregation .of (agg ->
207- agg .avg (avg -> avg .field (metric .getField ()))));
221+ agg .avg (avg -> avg .field (metric .getField ()))));
208222 break ;
209223 case MAX :
210224 metricAggregations .put (metric .getId (), Aggregation .of (agg ->
211- agg .max (max -> max .field (metric .getField ()))));
225+ agg .max (max -> max .field (metric .getField ()))));
212226 break ;
213227 case MIN :
214228 metricAggregations .put (metric .getId (), Aggregation .of (agg ->
215- agg .min (min -> min .field (metric .getField ()))));
229+ agg .min (min -> min .field (metric .getField ()))));
216230 break ;
217231 case SUM :
218232 metricAggregations .put (metric .getId (), Aggregation .of (agg ->
219- agg .sum (sum -> sum .field (metric .getField ()))));
233+ agg .sum (sum -> sum .field (metric .getField ()))));
220234 break ;
221235 case MEDIAN :
222236 metricAggregations .put (metric .getId (), Aggregation .of (agg ->
223- agg .percentiles (percentile -> percentile .field (metric .getField ())
224- .keyed (false ).percents (50D ))));
237+ agg .percentiles (percentile -> percentile .field (metric .getField ())
238+ .keyed (false ).percents (50D ))));
225239 break ;
226240 }
227241 }
@@ -230,4 +244,35 @@ private Map<String, Aggregation> buildMetricAggregation(List<Metric> metrics) {
230244 throw new RuntimeException (ctx + ": " + e .getMessage ());
231245 }
232246 }
247+
248+ private void collectBucketFields (Bucket bucket , Set <String > fields ) {
249+ if (bucket == null ) return ;
250+
251+ // 1. Field of this bucket (covers terms, ranges, date histogram)
252+ if (bucket .getField () != null && !bucket .getField ().isEmpty ()) {
253+ fields .add (bucket .getField ());
254+ }
255+
256+ // 2. Date histogram (field is in the bucket, not in DateHistogramBucket)
257+ if (bucket .getDateHistogram () != null &&
258+ bucket .getField () != null &&
259+ !bucket .getField ().isEmpty ()) {
260+ fields .add (bucket .getField ());
261+ }
262+
263+ // 3. Recurse into subBucket
264+ if (bucket .getSubBucket () != null ) {
265+ collectBucketFields (bucket .getSubBucket (), fields );
266+ }
267+ }
268+
269+
270+ private String normalizeField (String field ) {
271+ if (field .endsWith (".keyword" )) {
272+ return field .substring (0 , field .length () - ".keyword" .length ());
273+ }
274+ return field ;
275+ }
276+
277+
233278}
0 commit comments