19
19
20
20
import java .util .ArrayList ;
21
21
import java .util .Collection ;
22
+ import java .util .Collections ;
22
23
import java .util .List ;
23
24
24
25
import org .elasticsearch .action .search .SearchType ;
@@ -58,16 +59,16 @@ public class NativeSearchQueryBuilder {
58
59
private final List <AbstractAggregationBuilder <?>> aggregationBuilders = new ArrayList <>();
59
60
private final List <PipelineAggregationBuilder > pipelineAggregationBuilders = new ArrayList <>();
60
61
@ Nullable private HighlightBuilder highlightBuilder ;
61
- @ Nullable private HighlightBuilder .Field [] highlightFields ;
62
+ @ Nullable private List < HighlightBuilder .Field > highlightFields = new ArrayList <>() ;
62
63
private Pageable pageable = Pageable .unpaged ();
63
- @ Nullable private String [] fields ;
64
+ @ Nullable private List < String > fields = new ArrayList <>() ;
64
65
@ Nullable private SourceFilter sourceFilter ;
65
66
@ Nullable private CollapseBuilder collapseBuilder ;
66
- @ Nullable private List <IndexBoost > indicesBoost ;
67
+ @ Nullable private List <IndexBoost > indicesBoost = new ArrayList <>() ;
67
68
@ Nullable private SearchTemplateRequestBuilder searchTemplateBuilder ;
68
69
private float minScore ;
69
70
private boolean trackScores ;
70
- @ Nullable private Collection <String > ids ;
71
+ @ Nullable private List <String > ids = new ArrayList <>() ;
71
72
@ Nullable private String route ;
72
73
@ Nullable private SearchType searchType ;
73
74
@ Nullable private IndicesOptions indicesOptions ;
@@ -87,11 +88,31 @@ public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) {
87
88
return this ;
88
89
}
89
90
91
+ /**
92
+ * @deprecated use {@link #withSorts(SortBuilder...)} instead.
93
+ */
94
+ @ Deprecated
90
95
public NativeSearchQueryBuilder withSort (SortBuilder <?> sortBuilder ) {
91
96
this .sortBuilders .add (sortBuilder );
92
97
return this ;
93
98
}
94
99
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
+
95
116
public NativeSearchQueryBuilder withScriptField (ScriptField scriptField ) {
96
117
this .scriptFields .add (scriptField );
97
118
return this ;
@@ -110,6 +131,10 @@ public NativeSearchQueryBuilder withCollapseBuilder(@Nullable CollapseBuilder co
110
131
return this ;
111
132
}
112
133
134
+ /**
135
+ * @deprecated use {@link #withAggregations(AbstractAggregationBuilder...)} instead.
136
+ */
137
+ @ Deprecated
113
138
public NativeSearchQueryBuilder addAggregation (AbstractAggregationBuilder <?> aggregationBuilder ) {
114
139
this .aggregationBuilders .add (aggregationBuilder );
115
140
return this ;
@@ -118,23 +143,73 @@ public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder<?> agg
118
143
/**
119
144
* @since 4.3
120
145
*/
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
121
163
public NativeSearchQueryBuilder addAggregation (PipelineAggregationBuilder pipelineAggregationBuilder ) {
122
164
this .pipelineAggregationBuilders .add (pipelineAggregationBuilder );
123
165
return this ;
124
166
}
125
167
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
+
126
185
public NativeSearchQueryBuilder withHighlightBuilder (HighlightBuilder highlightBuilder ) {
127
186
this .highlightBuilder = highlightBuilder ;
128
187
return this ;
129
188
}
130
189
131
190
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 );
133
200
return this ;
134
201
}
135
202
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 );
138
213
return this ;
139
214
}
140
215
@@ -148,8 +223,16 @@ public NativeSearchQueryBuilder withPageable(Pageable pageable) {
148
223
return this ;
149
224
}
150
225
226
+ /**
227
+ * @since 4.3
228
+ */
229
+ public NativeSearchQueryBuilder withFields (Collection <String > fields ) {
230
+ this .fields .addAll (fields );
231
+ return this ;
232
+ }
233
+
151
234
public NativeSearchQueryBuilder withFields (String ... fields ) {
152
- this .fields = fields ;
235
+ Collections . addAll ( this .fields , fields ) ;
153
236
return this ;
154
237
}
155
238
@@ -174,7 +257,15 @@ public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
174
257
}
175
258
176
259
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 );
178
269
return this ;
179
270
}
180
271
@@ -223,14 +314,18 @@ public NativeSearchQueryBuilder withRescorerQuery(RescorerQuery rescorerQuery) {
223
314
224
315
public NativeSearchQuery build () {
225
316
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 ()]));
228
323
229
324
nativeSearchQuery .setPageable (pageable );
230
325
nativeSearchQuery .setTrackScores (trackScores );
231
326
232
327
if (fields != null ) {
233
- nativeSearchQuery .addFields (fields );
328
+ nativeSearchQuery .setFields (fields );
234
329
}
235
330
236
331
if (sourceFilter != null ) {
0 commit comments