-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
SearchRequest.java
555 lines (478 loc) · 16.8 KB
/
SearchRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.action.search;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import static org.elasticsearch.search.Scroll.readScroll;
/**
* A request to execute search against one or more indices (or all). Best created using
* {@link org.elasticsearch.client.Requests#searchRequest(String...)}.
* <p/>
* <p>Note, the search {@link #source(org.elasticsearch.search.builder.SearchSourceBuilder)}
* is required. The search source is the different search options, including facets and such.
* <p/>
* <p>There is an option to specify an addition search source using the {@link #extraSource(org.elasticsearch.search.builder.SearchSourceBuilder)}.
*
* @see org.elasticsearch.client.Requests#searchRequest(String...)
* @see org.elasticsearch.client.Client#search(SearchRequest)
* @see SearchResponse
*/
public class SearchRequest extends ActionRequest<SearchRequest> {
private SearchType searchType = SearchType.DEFAULT;
private String[] indices;
@Nullable
private String routing;
@Nullable
private String preference;
private BytesReference templateSource;
private boolean templateSourceUnsafe;
private String templateName;
private Map<String, String> templateParams = Collections.emptyMap();
private BytesReference source;
private boolean sourceUnsafe;
private BytesReference extraSource;
private boolean extraSourceUnsafe;
private Scroll scroll;
private String[] types = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = IndicesOptions.strict();
public SearchRequest() {
}
/**
* Constructs a new search request against the indices. No indices provided here means that search
* will run against all indices.
*/
public SearchRequest(String... indices) {
indices(indices);
}
/**
* Constructs a new search request against the provided indices with the given search source.
*/
public SearchRequest(String[] indices, byte[] source) {
indices(indices);
this.source = new BytesArray(source);
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
// no need to check, we resolve to match all query
// if (source == null && extraSource == null) {
// validationException = addValidationError("search source is missing", validationException);
// }
return validationException;
}
public void beforeStart() {
// we always copy over if needed, the reason is that a request might fail while being search remotely
// and then we need to keep the buffer around
if (source != null && sourceUnsafe) {
source = source.copyBytesArray();
sourceUnsafe = false;
}
if (extraSource != null && extraSourceUnsafe) {
extraSource = extraSource.copyBytesArray();
extraSourceUnsafe = false;
}
if (templateSource != null && templateSourceUnsafe) {
templateSource = templateSource.copyBytesArray();
templateSourceUnsafe = false;
}
}
/**
* Sets the indices the search will be executed on.
*/
public SearchRequest indices(String... indices) {
if (indices == null) {
throw new ElasticsearchIllegalArgumentException("indices must not be null");
} else {
for (int i = 0; i < indices.length; i++) {
if (indices[i] == null) {
throw new ElasticsearchIllegalArgumentException("indices[" + i +"] must not be null");
}
}
}
this.indices = indices;
return this;
}
public IndicesOptions indicesOptions() {
return indicesOptions;
}
public SearchRequest indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
return this;
}
/**
* The document types to execute the search against. Defaults to be executed against
* all types.
*/
public String[] types() {
return types;
}
/**
* The document types to execute the search against. Defaults to be executed against
* all types.
*/
public SearchRequest types(String... types) {
this.types = types;
return this;
}
/**
* A comma separated list of routing values to control the shards the search will be executed on.
*/
public String routing() {
return this.routing;
}
/**
* A comma separated list of routing values to control the shards the search will be executed on.
*/
public SearchRequest routing(String routing) {
this.routing = routing;
return this;
}
/**
* The routing values to control the shards that the search will be executed on.
*/
public SearchRequest routing(String... routings) {
this.routing = Strings.arrayToCommaDelimitedString(routings);
return this;
}
/**
* Sets the preference to execute the search. Defaults to randomize across shards. Can be set to
* <tt>_local</tt> to prefer local shards, <tt>_primary</tt> to execute only on primary shards, or
* a custom value, which guarantees that the same order will be used across different requests.
*/
public SearchRequest preference(String preference) {
this.preference = preference;
return this;
}
public String preference() {
return this.preference;
}
/**
* The search type to execute, defaults to {@link SearchType#DEFAULT}.
*/
public SearchRequest searchType(SearchType searchType) {
this.searchType = searchType;
return this;
}
/**
* The a string representation search type to execute, defaults to {@link SearchType#DEFAULT}. Can be
* one of "dfs_query_then_fetch"/"dfsQueryThenFetch", "dfs_query_and_fetch"/"dfsQueryAndFetch",
* "query_then_fetch"/"queryThenFetch", and "query_and_fetch"/"queryAndFetch".
*/
public SearchRequest searchType(String searchType) throws ElasticsearchIllegalArgumentException {
return searchType(SearchType.fromString(searchType));
}
/**
* The source of the search request.
*/
public SearchRequest source(SearchSourceBuilder sourceBuilder) {
this.source = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.sourceUnsafe = false;
return this;
}
/**
* The source of the search request. Consider using either {@link #source(byte[])} or
* {@link #source(org.elasticsearch.search.builder.SearchSourceBuilder)}.
*/
public SearchRequest source(String source) {
this.source = new BytesArray(source);
this.sourceUnsafe = false;
return this;
}
/**
* The source of the search request in the form of a map.
*/
public SearchRequest source(Map source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(source);
return source(builder);
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}
public SearchRequest source(XContentBuilder builder) {
this.source = builder.bytes();
this.sourceUnsafe = false;
return this;
}
/**
* The search source to execute.
*/
public SearchRequest source(byte[] source) {
return source(source, 0, source.length, false);
}
/**
* The search source to execute.
*/
public SearchRequest source(byte[] source, int offset, int length) {
return source(source, offset, length, false);
}
/**
* The search source to execute.
*/
public SearchRequest source(byte[] source, int offset, int length, boolean unsafe) {
return source(new BytesArray(source, offset, length), unsafe);
}
/**
* The search source to execute.
*/
public SearchRequest source(BytesReference source, boolean unsafe) {
this.source = source;
this.sourceUnsafe = unsafe;
return this;
}
/**
* The search source to execute.
*/
public BytesReference source() {
return source;
}
/**
* The search source template to execute.
*/
public BytesReference templateSource() {
return templateSource;
}
/**
* Allows to provide additional source that will be used as well.
*/
public SearchRequest extraSource(SearchSourceBuilder sourceBuilder) {
if (sourceBuilder == null) {
extraSource = null;
return this;
}
this.extraSource = sourceBuilder.buildAsBytes(Requests.CONTENT_TYPE);
this.extraSourceUnsafe = false;
return this;
}
public SearchRequest extraSource(Map extraSource) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
builder.map(extraSource);
return extraSource(builder);
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}
public SearchRequest extraSource(XContentBuilder builder) {
this.extraSource = builder.bytes();
this.extraSourceUnsafe = false;
return this;
}
/**
* Allows to provide additional source that will use used as well.
*/
public SearchRequest extraSource(String source) {
this.extraSource = new BytesArray(source);
this.extraSourceUnsafe = false;
return this;
}
/**
* Allows to provide additional source that will be used as well.
*/
public SearchRequest extraSource(byte[] source) {
return extraSource(source, 0, source.length, false);
}
/**
* Allows to provide additional source that will be used as well.
*/
public SearchRequest extraSource(byte[] source, int offset, int length) {
return extraSource(source, offset, length, false);
}
/**
* Allows to provide additional source that will be used as well.
*/
public SearchRequest extraSource(byte[] source, int offset, int length, boolean unsafe) {
return extraSource(new BytesArray(source, offset, length), unsafe);
}
/**
* Allows to provide additional source that will be used as well.
*/
public SearchRequest extraSource(BytesReference source, boolean unsafe) {
this.extraSource = source;
this.extraSourceUnsafe = unsafe;
return this;
}
/**
* Allows to provide template as source.
*/
public SearchRequest templateSource(BytesReference template, boolean unsafe) {
this.templateSource = template;
this.templateSourceUnsafe = unsafe;
return this;
}
/**
* The template of the search request.
*/
public SearchRequest templateSource(String template) {
this.templateSource = new BytesArray(template);
this.templateSourceUnsafe = false;
return this;
}
/**
* The name of the stored template
*/
public void templateName(String name) {
this.templateName = name;
}
/**
* Template parameters used for rendering
*/
public void templateParams(Map<String, String> params) {
this.templateParams = params;
}
/**
* The name of the stored template
*/
public String templateName() {
return templateName;
}
/**
* Template parameters used for rendering
*/
public Map<String, String> templateParams() {
return templateParams;
}
/**
* Additional search source to execute.
*/
public BytesReference extraSource() {
return this.extraSource;
}
/**
* The tye of search to execute.
*/
public SearchType searchType() {
return searchType;
}
/**
* The indices
*/
public String[] indices() {
return indices;
}
/**
* If set, will enable scrolling of the search request.
*/
public Scroll scroll() {
return scroll;
}
/**
* If set, will enable scrolling of the search request.
*/
public SearchRequest scroll(Scroll scroll) {
this.scroll = scroll;
return this;
}
/**
* If set, will enable scrolling of the search request for the specified timeout.
*/
public SearchRequest scroll(TimeValue keepAlive) {
return scroll(new Scroll(keepAlive));
}
/**
* If set, will enable scrolling of the search request for the specified timeout.
*/
public SearchRequest scroll(String keepAlive) {
return scroll(new Scroll(TimeValue.parseTimeValue(keepAlive, null)));
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.getVersion().before(Version.V_1_2_0)) {
in.readByte(); // backward comp. for operation threading
}
searchType = SearchType.fromId(in.readByte());
indices = new String[in.readVInt()];
for (int i = 0; i < indices.length; i++) {
indices[i] = in.readString();
}
routing = in.readOptionalString();
preference = in.readOptionalString();
if (in.readBoolean()) {
scroll = readScroll(in);
}
sourceUnsafe = false;
source = in.readBytesReference();
extraSourceUnsafe = false;
extraSource = in.readBytesReference();
types = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
if (in.getVersion().onOrAfter(Version.V_1_1_0)) {
templateSourceUnsafe = false;
templateSource = in.readBytesReference();
templateName = in.readOptionalString();
if (in.readBoolean()) {
templateParams = (Map<String, String>) in.readGenericValue();
}
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (out.getVersion().before(Version.V_1_2_0)) {
out.writeByte((byte) 2); // operation threading
}
out.writeByte(searchType.id());
out.writeVInt(indices.length);
for (String index : indices) {
out.writeString(index);
}
out.writeOptionalString(routing);
out.writeOptionalString(preference);
if (scroll == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
scroll.writeTo(out);
}
out.writeBytesReference(source);
out.writeBytesReference(extraSource);
out.writeStringArray(types);
indicesOptions.writeIndicesOptions(out);
if (out.getVersion().onOrAfter(Version.V_1_1_0)) {
out.writeBytesReference(templateSource);
out.writeOptionalString(templateName);
boolean existTemplateParams = templateParams != null;
out.writeBoolean(existTemplateParams);
if (existTemplateParams) {
out.writeGenericValue(templateParams);
}
}
}
}