33
33
import org .elasticsearch .action .delete .DeleteRequest ;
34
34
import org .elasticsearch .action .get .GetRequest ;
35
35
import org .elasticsearch .action .index .IndexRequest ;
36
+ import org .elasticsearch .action .search .SearchRequest ;
36
37
import org .elasticsearch .action .support .ActiveShardCount ;
38
+ import org .elasticsearch .action .support .IndicesOptions ;
37
39
import org .elasticsearch .action .support .WriteRequest ;
38
40
import org .elasticsearch .action .update .UpdateRequest ;
39
41
import org .elasticsearch .common .Nullable ;
42
44
import org .elasticsearch .common .lucene .uid .Versions ;
43
45
import org .elasticsearch .common .unit .TimeValue ;
44
46
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
47
+ import org .elasticsearch .common .xcontent .ToXContent ;
45
48
import org .elasticsearch .common .xcontent .XContentBuilder ;
46
49
import org .elasticsearch .common .xcontent .XContentHelper ;
47
50
import org .elasticsearch .common .xcontent .XContentParser ;
48
51
import org .elasticsearch .common .xcontent .XContentType ;
49
52
import org .elasticsearch .index .VersionType ;
53
+ import org .elasticsearch .rest .action .search .RestSearchAction ;
50
54
import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
51
55
52
56
import java .io .ByteArrayOutputStream ;
59
63
60
64
final class Request {
61
65
62
- private static final String DELIMITER = "/" ;
66
+ private static final XContentType REQUEST_BODY_CONTENT_TYPE = XContentType . JSON ;
63
67
64
68
final String method ;
65
69
final String endpoint ;
@@ -79,6 +83,7 @@ public String toString() {
79
83
"method='" + method + '\'' +
80
84
", endpoint='" + endpoint + '\'' +
81
85
", params=" + params +
86
+ ", hasBody=" + (entity != null ) +
82
87
'}' ;
83
88
}
84
89
@@ -307,23 +312,48 @@ static Request update(UpdateRequest updateRequest) throws IOException {
307
312
xContentType = Requests .INDEX_CONTENT_TYPE ;
308
313
}
309
314
310
- BytesRef source = XContentHelper .toXContent (updateRequest , xContentType , false ).toBytesRef ();
311
- HttpEntity entity = new ByteArrayEntity (source .bytes , source .offset , source .length , ContentType .create (xContentType .mediaType ()));
312
-
315
+ HttpEntity entity = createEntity (updateRequest , xContentType );
313
316
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), entity );
314
317
}
315
318
319
+ static Request search (SearchRequest searchRequest ) throws IOException {
320
+ String endpoint = endpoint (searchRequest .indices (), searchRequest .types (), "_search" );
321
+ Params params = Params .builder ();
322
+ params .putParam (RestSearchAction .TYPED_KEYS_PARAM , "true" );
323
+ params .withRouting (searchRequest .routing ());
324
+ params .withPreference (searchRequest .preference ());
325
+ params .withIndicesOptions (searchRequest .indicesOptions ());
326
+ params .putParam ("search_type" , searchRequest .searchType ().name ().toLowerCase (Locale .ROOT ));
327
+ if (searchRequest .requestCache () != null ) {
328
+ params .putParam ("request_cache" , Boolean .toString (searchRequest .requestCache ()));
329
+ }
330
+ params .putParam ("batched_reduce_size" , Integer .toString (searchRequest .getBatchedReduceSize ()));
331
+ if (searchRequest .scroll () != null ) {
332
+ params .putParam ("scroll" , searchRequest .scroll ().keepAlive ());
333
+ }
334
+ HttpEntity entity = null ;
335
+ if (searchRequest .source () != null ) {
336
+ entity = createEntity (searchRequest .source (), REQUEST_BODY_CONTENT_TYPE );
337
+ }
338
+ return new Request (HttpGet .METHOD_NAME , endpoint , params .getParams (), entity );
339
+ }
340
+
341
+ private static HttpEntity createEntity (ToXContent toXContent , XContentType xContentType ) throws IOException {
342
+ BytesRef source = XContentHelper .toXContent (toXContent , xContentType , false ).toBytesRef ();
343
+ return new ByteArrayEntity (source .bytes , source .offset , source .length , ContentType .create (xContentType .mediaType ()));
344
+ }
345
+
346
+ static String endpoint (String [] indices , String [] types , String endpoint ) {
347
+ return endpoint (String .join ("," , indices ), String .join ("," , types ), endpoint );
348
+ }
349
+
316
350
/**
317
351
* Utility method to build request's endpoint.
318
352
*/
319
353
static String endpoint (String ... parts ) {
320
- if (parts == null || parts .length == 0 ) {
321
- return DELIMITER ;
322
- }
323
-
324
- StringJoiner joiner = new StringJoiner (DELIMITER , DELIMITER , "" );
354
+ StringJoiner joiner = new StringJoiner ("/" , "/" , "" );
325
355
for (String part : parts ) {
326
- if (part != null ) {
356
+ if (Strings . hasLength ( part ) ) {
327
357
joiner .add (part );
328
358
}
329
359
}
@@ -453,6 +483,26 @@ Params withWaitForActiveShards(ActiveShardCount activeShardCount) {
453
483
return this ;
454
484
}
455
485
486
+ Params withIndicesOptions (IndicesOptions indicesOptions ) {
487
+ putParam ("ignore_unavailable" , Boolean .toString (indicesOptions .ignoreUnavailable ()));
488
+ putParam ("allow_no_indices" , Boolean .toString (indicesOptions .allowNoIndices ()));
489
+ String expandWildcards ;
490
+ if (indicesOptions .expandWildcardsOpen () == false && indicesOptions .expandWildcardsClosed () == false ) {
491
+ expandWildcards = "none" ;
492
+ } else {
493
+ StringJoiner joiner = new StringJoiner ("," );
494
+ if (indicesOptions .expandWildcardsOpen ()) {
495
+ joiner .add ("open" );
496
+ }
497
+ if (indicesOptions .expandWildcardsClosed ()) {
498
+ joiner .add ("closed" );
499
+ }
500
+ expandWildcards = joiner .toString ();
501
+ }
502
+ putParam ("expand_wildcards" , expandWildcards );
503
+ return this ;
504
+ }
505
+
456
506
Map <String , String > getParams () {
457
507
return Collections .unmodifiableMap (params );
458
508
}
0 commit comments