32
32
import org .elasticsearch .action .admin .cluster .settings .ClusterUpdateSettingsRequest ;
33
33
import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
34
34
import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
35
+ import org .elasticsearch .action .admin .indices .cache .clear .ClearIndicesCacheRequest ;
35
36
import org .elasticsearch .action .admin .indices .close .CloseIndexRequest ;
36
37
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
37
38
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
@@ -170,13 +171,10 @@ static Request openIndex(OpenIndexRequest openIndexRequest) {
170
171
171
172
static Request closeIndex (CloseIndexRequest closeIndexRequest ) {
172
173
String endpoint = endpoint (closeIndexRequest .indices (), "_close" );
173
-
174
174
Params parameters = Params .builder ();
175
-
176
175
parameters .withTimeout (closeIndexRequest .timeout ());
177
176
parameters .withMasterTimeout (closeIndexRequest .masterNodeTimeout ());
178
177
parameters .withIndicesOptions (closeIndexRequest .indicesOptions ());
179
-
180
178
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
181
179
}
182
180
@@ -220,21 +218,35 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
220
218
}
221
219
222
220
static Request refresh (RefreshRequest refreshRequest ) {
223
- String endpoint = endpoint (refreshRequest .indices (), "_refresh" );
221
+ String [] indices = refreshRequest .indices () == null ? Strings .EMPTY_ARRAY : refreshRequest .indices ();
222
+ String endpoint = endpoint (indices , "_refresh" );
224
223
Params parameters = Params .builder ();
225
224
parameters .withIndicesOptions (refreshRequest .indicesOptions ());
226
225
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
227
226
}
228
227
229
228
static Request flush (FlushRequest flushRequest ) {
230
- String endpoint = endpoint (flushRequest .indices (), "_flush" );
229
+ String [] indices = flushRequest .indices () == null ? Strings .EMPTY_ARRAY : flushRequest .indices ();
230
+ String endpoint = endpoint (indices , "_flush" );
231
231
Params parameters = Params .builder ();
232
232
parameters .withIndicesOptions (flushRequest .indicesOptions ());
233
233
parameters .putParam ("wait_if_ongoing" , Boolean .toString (flushRequest .waitIfOngoing ()));
234
234
parameters .putParam ("force" , Boolean .toString (flushRequest .force ()));
235
235
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
236
236
}
237
237
238
+ static Request clearCache (ClearIndicesCacheRequest clearIndicesCacheRequest ) {
239
+ String [] indices = clearIndicesCacheRequest .indices () == null ? Strings .EMPTY_ARRAY :clearIndicesCacheRequest .indices ();
240
+ String endpoint = endpoint (indices , "_cache/clear" );
241
+ Params parameters = Params .builder ();
242
+ parameters .withIndicesOptions (clearIndicesCacheRequest .indicesOptions ());
243
+ parameters .putParam ("query" , Boolean .toString (clearIndicesCacheRequest .queryCache ()));
244
+ parameters .putParam ("fielddata" , Boolean .toString (clearIndicesCacheRequest .fieldDataCache ()));
245
+ parameters .putParam ("request" , Boolean .toString (clearIndicesCacheRequest .requestCache ()));
246
+ parameters .putParam ("fields" , String .join ("," , clearIndicesCacheRequest .fields ()));
247
+ return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
248
+ }
249
+
238
250
static Request info () {
239
251
return new Request (HttpGet .METHOD_NAME , "/" , Collections .emptyMap (), null );
240
252
}
@@ -510,10 +522,13 @@ static Request existsAlias(GetAliasesRequest getAliasesRequest) {
510
522
Params params = Params .builder ();
511
523
params .withIndicesOptions (getAliasesRequest .indicesOptions ());
512
524
params .withLocal (getAliasesRequest .local ());
513
- if (getAliasesRequest .indices ().length == 0 && getAliasesRequest .aliases ().length == 0 ) {
525
+ if ((getAliasesRequest .indices () == null || getAliasesRequest .indices ().length == 0 ) &&
526
+ (getAliasesRequest .aliases () == null || getAliasesRequest .aliases ().length == 0 )) {
514
527
throw new IllegalArgumentException ("existsAlias requires at least an alias or an index" );
515
528
}
516
- String endpoint = endpoint (getAliasesRequest .indices (), "_alias" , getAliasesRequest .aliases ());
529
+ String [] indices = getAliasesRequest .indices () == null ? Strings .EMPTY_ARRAY : getAliasesRequest .indices ();
530
+ String [] aliases = getAliasesRequest .aliases () == null ? Strings .EMPTY_ARRAY : getAliasesRequest .aliases ();
531
+ String endpoint = endpoint (indices , "_alias" , aliases );
517
532
return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
518
533
}
519
534
@@ -542,8 +557,9 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
542
557
params .withTimeout (resizeRequest .timeout ());
543
558
params .withMasterTimeout (resizeRequest .masterNodeTimeout ());
544
559
params .withWaitForActiveShards (resizeRequest .getTargetIndexRequest ().waitForActiveShards (), ActiveShardCount .DEFAULT );
545
- String endpoint = buildEndpoint (resizeRequest .getSourceIndex (), "_" + resizeRequest .getResizeType ().name ().toLowerCase (Locale .ROOT ),
546
- resizeRequest .getTargetIndexRequest ().index ());
560
+ String endpoint = new EndpointBuilder ().addPathPart (resizeRequest .getSourceIndex ())
561
+ .addPathPartAsIs ("_" + resizeRequest .getResizeType ().name ().toLowerCase (Locale .ROOT ))
562
+ .addPathPart (resizeRequest .getTargetIndexRequest ().index ()).build ();
547
563
HttpEntity entity = createEntity (resizeRequest , REQUEST_BODY_CONTENT_TYPE );
548
564
return new Request (HttpPut .METHOD_NAME , endpoint , params .getParams (), entity );
549
565
}
@@ -553,10 +569,8 @@ static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSett
553
569
parameters .withFlatSettings (clusterUpdateSettingsRequest .flatSettings ());
554
570
parameters .withTimeout (clusterUpdateSettingsRequest .timeout ());
555
571
parameters .withMasterTimeout (clusterUpdateSettingsRequest .masterNodeTimeout ());
556
-
557
- String endpoint = buildEndpoint ("_cluster" , "settings" );
558
572
HttpEntity entity = createEntity (clusterUpdateSettingsRequest , REQUEST_BODY_CONTENT_TYPE );
559
- return new Request (HttpPut .METHOD_NAME , endpoint , parameters .getParams (), entity );
573
+ return new Request (HttpPut .METHOD_NAME , "/_cluster/settings" , parameters .getParams (), entity );
560
574
}
561
575
562
576
static Request rollover (RolloverRequest rolloverRequest ) throws IOException {
@@ -567,64 +581,60 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
567
581
if (rolloverRequest .isDryRun ()) {
568
582
params .putParam ("dry_run" , Boolean .TRUE .toString ());
569
583
}
570
- String endpoint = buildEndpoint (rolloverRequest .getAlias (), "_rollover" , rolloverRequest .getNewIndexName ());
584
+ String endpoint = new EndpointBuilder ().addPathPart (rolloverRequest .getAlias ()).addPathPartAsIs ("_rollover" )
585
+ .addPathPart (rolloverRequest .getNewIndexName ()).build ();
571
586
HttpEntity entity = createEntity (rolloverRequest , REQUEST_BODY_CONTENT_TYPE );
572
587
return new Request (HttpPost .METHOD_NAME , endpoint , params .getParams (), entity );
573
588
}
574
589
590
+ static Request indicesExist (GetIndexRequest request ) {
591
+ //this can be called with no indices as argument by transport client, not via REST though
592
+ if (request .indices () == null || request .indices ().length == 0 ) {
593
+ throw new IllegalArgumentException ("indices are mandatory" );
594
+ }
595
+ String endpoint = endpoint (request .indices (), "" );
596
+ Params params = Params .builder ();
597
+ params .withLocal (request .local ());
598
+ params .withHuman (request .humanReadable ());
599
+ params .withIndicesOptions (request .indicesOptions ());
600
+ params .withFlatSettings (request .flatSettings ());
601
+ params .withIncludeDefaults (request .includeDefaults ());
602
+ return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
603
+ }
604
+
575
605
private static HttpEntity createEntity (ToXContent toXContent , XContentType xContentType ) throws IOException {
576
606
BytesRef source = XContentHelper .toXContent (toXContent , xContentType , false ).toBytesRef ();
577
607
return new ByteArrayEntity (source .bytes , source .offset , source .length , createContentType (xContentType ));
578
608
}
579
609
580
610
static String endpoint (String index , String type , String id ) {
581
- return buildEndpoint ( index , type , id );
611
+ return new EndpointBuilder (). addPathPart ( index , type , id ). build ( );
582
612
}
583
613
584
614
static String endpoint (String index , String type , String id , String endpoint ) {
585
- return buildEndpoint ( index , type , id , endpoint );
615
+ return new EndpointBuilder (). addPathPart ( index , type , id ). addPathPartAsIs ( endpoint ). build ( );
586
616
}
587
617
588
618
static String endpoint (String [] indices ) {
589
- return buildEndpoint ( String . join ( "," , indices ));
619
+ return new EndpointBuilder (). addCommaSeparatedPathParts ( indices ). build ( );
590
620
}
591
621
592
622
static String endpoint (String [] indices , String endpoint ) {
593
- return buildEndpoint ( String . join ( "," , indices ), endpoint );
623
+ return new EndpointBuilder (). addCommaSeparatedPathParts ( indices ). addPathPartAsIs ( endpoint ). build ( );
594
624
}
595
625
596
626
static String endpoint (String [] indices , String [] types , String endpoint ) {
597
- return buildEndpoint (String .join ("," , indices ), String .join ("," , types ), endpoint );
627
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addCommaSeparatedPathParts (types )
628
+ .addPathPartAsIs (endpoint ).build ();
598
629
}
599
630
600
631
static String endpoint (String [] indices , String endpoint , String [] suffixes ) {
601
- return buildEndpoint (String .join ("," , indices ), endpoint , String .join ("," , suffixes ));
632
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addPathPartAsIs (endpoint )
633
+ .addCommaSeparatedPathParts (suffixes ).build ();
602
634
}
603
635
604
636
static String endpoint (String [] indices , String endpoint , String type ) {
605
- return endpoint (String .join ("," , indices ), endpoint , type );
606
- }
607
-
608
- /**
609
- * Utility method to build request's endpoint given its parts as strings
610
- */
611
- static String buildEndpoint (String ... parts ) {
612
- StringJoiner joiner = new StringJoiner ("/" , "/" , "" );
613
- for (String part : parts ) {
614
- if (Strings .hasLength (part )) {
615
- try {
616
- //encode each part (e.g. index, type and id) separately before merging them into the path
617
- //we prepend "/" to the path part to make this pate absolute, otherwise there can be issues with
618
- //paths that start with `-` or contain `:`
619
- URI uri = new URI (null , null , null , -1 , "/" + part , null , null );
620
- //manually encode any slash that each part may contain
621
- joiner .add (uri .getRawPath ().substring (1 ).replaceAll ("/" , "%2F" ));
622
- } catch (URISyntaxException e ) {
623
- throw new IllegalArgumentException ("Path part [" + part + "] couldn't be encoded" , e );
624
- }
625
- }
626
- }
627
- return joiner .toString ();
637
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addPathPartAsIs (endpoint ).addPathPart (type ).build ();
628
638
}
629
639
630
640
/**
@@ -638,17 +648,6 @@ public static ContentType createContentType(final XContentType xContentType) {
638
648
return ContentType .create (xContentType .mediaTypeWithoutParameters (), (Charset ) null );
639
649
}
640
650
641
- static Request indicesExist (GetIndexRequest request ) {
642
- String endpoint = endpoint (request .indices (), Strings .EMPTY_ARRAY , "" );
643
- Params params = Params .builder ();
644
- params .withLocal (request .local ());
645
- params .withHuman (request .humanReadable ());
646
- params .withIndicesOptions (request .indicesOptions ());
647
- params .withFlatSettings (request .flatSettings ());
648
- params .withIncludeDefaults (request .includeDefaults ());
649
- return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
650
- }
651
-
652
651
/**
653
652
* Utility class to build request's parameters map and centralize all parameter names.
654
653
*/
@@ -861,4 +860,50 @@ static XContentType enforceSameContentType(IndexRequest indexRequest, @Nullable
861
860
}
862
861
return xContentType ;
863
862
}
863
+
864
+ /**
865
+ * Utility class to build request's endpoint given its parts as strings
866
+ */
867
+ static class EndpointBuilder {
868
+
869
+ private final StringJoiner joiner = new StringJoiner ("/" , "/" , "" );
870
+
871
+ EndpointBuilder addPathPart (String ... parts ) {
872
+ for (String part : parts ) {
873
+ if (Strings .hasLength (part )) {
874
+ joiner .add (encodePart (part ));
875
+ }
876
+ }
877
+ return this ;
878
+ }
879
+
880
+ EndpointBuilder addCommaSeparatedPathParts (String [] parts ) {
881
+ addPathPart (String .join ("," , parts ));
882
+ return this ;
883
+ }
884
+
885
+ EndpointBuilder addPathPartAsIs (String part ) {
886
+ if (Strings .hasLength (part )) {
887
+ joiner .add (part );
888
+ }
889
+ return this ;
890
+ }
891
+
892
+ String build () {
893
+ return joiner .toString ();
894
+ }
895
+
896
+ private static String encodePart (String pathPart ) {
897
+ try {
898
+ //encode each part (e.g. index, type and id) separately before merging them into the path
899
+ //we prepend "/" to the path part to make this pate absolute, otherwise there can be issues with
900
+ //paths that start with `-` or contain `:`
901
+ URI uri = new URI (null , null , null , -1 , "/" + pathPart , null , null );
902
+ //manually encode any slash that each part may contain
903
+ return uri .getRawPath ().substring (1 ).replaceAll ("/" , "%2F" );
904
+ } catch (URISyntaxException e ) {
905
+ throw new IllegalArgumentException ("Path part [" + pathPart + "] couldn't be encoded" , e );
906
+ }
907
+ }
908
+ }
864
909
}
0 commit comments