99import com .park .utmstack .service .elasticsearch .SearchUtil ;
1010import com .park .utmstack .util .TimezoneUtil ;
1111import com .park .utmstack .util .exceptions .UtmElasticsearchException ;
12+ import com .park .utmstack .util .exceptions .UtmSerializationException ;
1213import org .opensearch .client .opensearch ._types .SortOrder ;
1314import org .opensearch .client .opensearch ._types .aggregations .*;
1415import org .opensearch .client .opensearch .core .SearchRequest ;
1516import org .opensearch .client .opensearch .core .search .TrackHits ;
17+ import org .springframework .data .domain .Pageable ;
1618import org .springframework .util .Assert ;
1719import org .springframework .util .CollectionUtils ;
1820
@@ -34,29 +36,48 @@ public RequestDsl(UtmVisualization visualization) {
3436 }
3537
3638 public SearchRequest .Builder getSearchSourceBuilder () throws UtmElasticsearchException {
39+ return getSearchSourceBuilder (null , 10000 );
40+ }
41+
42+ public SearchRequest .Builder getSearchSourceBuilder (Pageable pageable , int top ) throws UtmElasticsearchException {
3743 final String ctx = CLASSNAME + ".getSearchSourceBuilder" ;
3844 try {
39- List <FilterType > filters = visualization .getFilterType ();
40-
41- if (CollectionUtils .isEmpty (filters ))
42- filters = new ArrayList <>();
43-
44- searchRequestBuilder .query (SearchUtil .toQuery (filters ));
45+ prepareSearchRequest ();
4546
46- if (visualization .getChartType () != ChartType .LIST_CHART &&
47- visualization .getChartType () != ChartType .TEXT_CHART ) {
47+ if (isAggregatedChart ()) {
4848 buildAggregation ();
4949 searchRequestBuilder .size (0 );
5050 searchRequestBuilder .trackTotalHits (TrackHits .of (t -> t .enabled (true )));
5151 } else {
52- searchRequestBuilder . size ( 10000 );
52+ applyPagination ( pageable , top );
5353 }
5454 return searchRequestBuilder ;
5555 } catch (Exception e ) {
5656 throw new UtmElasticsearchException (ctx + ": " + e .getMessage ());
5757 }
5858 }
5959
60+ private void prepareSearchRequest () throws UtmSerializationException {
61+ List <FilterType > filters = visualization .getFilterType ();
62+ if (CollectionUtils .isEmpty (filters )) {
63+ filters = new ArrayList <>();
64+ }
65+ searchRequestBuilder .query (SearchUtil .toQuery (filters ));
66+ }
67+
68+ private boolean isAggregatedChart () {
69+ return visualization .getChartType () != ChartType .LIST_CHART &&
70+ visualization .getChartType () != ChartType .TEXT_CHART ;
71+ }
72+
73+ private void applyPagination (Pageable pageable , int top ) {
74+ if (pageable != null && pageable .isPaged ()) {
75+ SearchUtil .applyPaginationAndSort (searchRequestBuilder , pageable , top );
76+ } else {
77+ searchRequestBuilder .size (10000 );
78+ }
79+ }
80+
6081 public SearchRequest .Builder getSearchSourceBuilderForCount () throws UtmElasticsearchException {
6182 final String ctx = CLASSNAME + ".getSearchSourceBuilderForCount" ;
6283 try {
0 commit comments