1919
2020import java .util .ArrayList ;
2121import java .util .Collection ;
22+ import java .util .Collections ;
2223import java .util .List ;
2324
2425import org .elasticsearch .action .search .SearchType ;
@@ -58,16 +59,16 @@ public class NativeSearchQueryBuilder {
5859 private final List <AbstractAggregationBuilder <?>> aggregationBuilders = new ArrayList <>();
5960 private final List <PipelineAggregationBuilder > pipelineAggregationBuilders = new ArrayList <>();
6061 @ Nullable private HighlightBuilder highlightBuilder ;
61- @ Nullable private HighlightBuilder .Field [] highlightFields ;
62+ @ Nullable private List < HighlightBuilder .Field > highlightFields = new ArrayList <>() ;
6263 private Pageable pageable = Pageable .unpaged ();
63- @ Nullable private String [] fields ;
64+ @ Nullable private List < String > fields = new ArrayList <>() ;
6465 @ Nullable private SourceFilter sourceFilter ;
6566 @ Nullable private CollapseBuilder collapseBuilder ;
66- @ Nullable private List <IndexBoost > indicesBoost ;
67+ @ Nullable private List <IndexBoost > indicesBoost = new ArrayList <>() ;
6768 @ Nullable private SearchTemplateRequestBuilder searchTemplateBuilder ;
6869 private float minScore ;
6970 private boolean trackScores ;
70- @ Nullable private Collection <String > ids ;
71+ @ Nullable private List <String > ids = new ArrayList <>() ;
7172 @ Nullable private String route ;
7273 @ Nullable private SearchType searchType ;
7374 @ Nullable private IndicesOptions indicesOptions ;
@@ -87,11 +88,31 @@ public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) {
8788 return this ;
8889 }
8990
91+ /**
92+ * @deprecated use {@link #withSorts(SortBuilder...)} instead.
93+ */
94+ @ Deprecated
9095 public NativeSearchQueryBuilder withSort (SortBuilder <?> sortBuilder ) {
9196 this .sortBuilders .add (sortBuilder );
9297 return this ;
9398 }
9499
100+ /**
101+ * @since 4.3
102+ */
103+ public NativeSearchQueryBuilder withSorts (Collection <SortBuilder <?>> sortBuilders ) {
104+ this .sortBuilders .addAll (sortBuilders );
105+ return this ;
106+ }
107+
108+ /**
109+ * @since 4.3
110+ */
111+ public NativeSearchQueryBuilder withSorts (SortBuilder <?>... sortBuilders ) {
112+ Collections .addAll (this .sortBuilders , sortBuilders );
113+ return this ;
114+ }
115+
95116 public NativeSearchQueryBuilder withScriptField (ScriptField scriptField ) {
96117 this .scriptFields .add (scriptField );
97118 return this ;
@@ -110,6 +131,10 @@ public NativeSearchQueryBuilder withCollapseBuilder(@Nullable CollapseBuilder co
110131 return this ;
111132 }
112133
134+ /**
135+ * @deprecated use {@link #withAggregations(AbstractAggregationBuilder...)} instead.
136+ */
137+ @ Deprecated
113138 public NativeSearchQueryBuilder addAggregation (AbstractAggregationBuilder <?> aggregationBuilder ) {
114139 this .aggregationBuilders .add (aggregationBuilder );
115140 return this ;
@@ -118,23 +143,73 @@ public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder<?> agg
118143 /**
119144 * @since 4.3
120145 */
146+ public NativeSearchQueryBuilder withAggregations (Collection <AbstractAggregationBuilder <?>> aggregationBuilders ) {
147+ this .aggregationBuilders .addAll (aggregationBuilders );
148+ return this ;
149+ }
150+
151+ /**
152+ * @since 4.3
153+ */
154+ public NativeSearchQueryBuilder withAggregations (AbstractAggregationBuilder <?>... aggregationBuilders ) {
155+ Collections .addAll (this .aggregationBuilders , aggregationBuilders );
156+ return this ;
157+ }
158+
159+ /**
160+ * @deprecated use {@link #withPipelineAggregations(PipelineAggregationBuilder...)} instead.
161+ */
162+ @ Deprecated
121163 public NativeSearchQueryBuilder addAggregation (PipelineAggregationBuilder pipelineAggregationBuilder ) {
122164 this .pipelineAggregationBuilders .add (pipelineAggregationBuilder );
123165 return this ;
124166 }
125167
168+ /**
169+ * @since 4.3
170+ */
171+ public NativeSearchQueryBuilder withPipelineAggregations (
172+ Collection <PipelineAggregationBuilder > pipelineAggregationBuilders ) {
173+ this .pipelineAggregationBuilders .addAll (pipelineAggregationBuilders );
174+ return this ;
175+ }
176+
177+ /**
178+ * @since 4.3
179+ */
180+ public NativeSearchQueryBuilder withPipelineAggregations (PipelineAggregationBuilder ... pipelineAggregationBuilders ) {
181+ Collections .addAll (this .pipelineAggregationBuilders , pipelineAggregationBuilders );
182+ return this ;
183+ }
184+
126185 public NativeSearchQueryBuilder withHighlightBuilder (HighlightBuilder highlightBuilder ) {
127186 this .highlightBuilder = highlightBuilder ;
128187 return this ;
129188 }
130189
131190 public NativeSearchQueryBuilder withHighlightFields (HighlightBuilder .Field ... highlightFields ) {
132- this .highlightFields = highlightFields ;
191+ Collections .addAll (this .highlightFields , highlightFields );
192+ return this ;
193+ }
194+
195+ /**
196+ * @since 4.3
197+ */
198+ public NativeSearchQueryBuilder withHighlightFields (Collection <HighlightBuilder .Field > highlightFields ) {
199+ this .highlightFields .addAll (highlightFields );
133200 return this ;
134201 }
135202
136- public NativeSearchQueryBuilder withIndicesBoost (List <IndexBoost > indicesBoost ) {
137- this .indicesBoost = indicesBoost ;
203+ public NativeSearchQueryBuilder withIndicesBoost (Collection <IndexBoost > indicesBoost ) {
204+ this .indicesBoost .addAll (indicesBoost );
205+ return this ;
206+ }
207+
208+ /**
209+ * @since 4.3
210+ */
211+ public NativeSearchQueryBuilder withIndicesBoost (IndexBoost ... indicesBoost ) {
212+ Collections .addAll (this .indicesBoost , indicesBoost );
138213 return this ;
139214 }
140215
@@ -148,8 +223,16 @@ public NativeSearchQueryBuilder withPageable(Pageable pageable) {
148223 return this ;
149224 }
150225
226+ /**
227+ * @since 4.3
228+ */
229+ public NativeSearchQueryBuilder withFields (Collection <String > fields ) {
230+ this .fields .addAll (fields );
231+ return this ;
232+ }
233+
151234 public NativeSearchQueryBuilder withFields (String ... fields ) {
152- this .fields = fields ;
235+ Collections . addAll ( this .fields , fields ) ;
153236 return this ;
154237 }
155238
@@ -174,7 +257,15 @@ public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
174257 }
175258
176259 public NativeSearchQueryBuilder withIds (Collection <String > ids ) {
177- this .ids = ids ;
260+ this .ids .addAll (ids );
261+ return this ;
262+ }
263+
264+ /**
265+ * @since 4.3
266+ */
267+ public NativeSearchQueryBuilder withIds (String ... ids ) {
268+ Collections .addAll (this .ids , ids );
178269 return this ;
179270 }
180271
@@ -223,14 +314,18 @@ public NativeSearchQueryBuilder withRescorerQuery(RescorerQuery rescorerQuery) {
223314
224315 public NativeSearchQuery build () {
225316
226- NativeSearchQuery nativeSearchQuery = new NativeSearchQuery (queryBuilder , filterBuilder , sortBuilders ,
227- highlightBuilder , highlightFields );
317+ NativeSearchQuery nativeSearchQuery = new NativeSearchQuery ( //
318+ queryBuilder , //
319+ filterBuilder , //
320+ sortBuilders , //
321+ highlightBuilder , //
322+ highlightFields .toArray (new HighlightBuilder .Field [highlightFields .size ()]));
228323
229324 nativeSearchQuery .setPageable (pageable );
230325 nativeSearchQuery .setTrackScores (trackScores );
231326
232327 if (fields != null ) {
233- nativeSearchQuery .addFields (fields );
328+ nativeSearchQuery .setFields (fields );
234329 }
235330
236331 if (sourceFilter != null ) {
0 commit comments